-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Undesirable to override/overwrite Math.random #20
Comments
As I always check the current issues before I consider using a new lib, this issue seemed easily solved by RTFM, after all, as quoted from the README on the root of the package:
The documented convenience methods of overriding the Math.random() are probably what most lazy developers are looking for though. |
Well I disagree and it has nothing to do with RTFM-ing. It's an architectural consideration. Monkey-patching a global, standard library object is not what most programmers want I think. It is perfectly acceptable to do it in a smaller project where all the consequences are known. At least the library should not do it by default, but instead provide a function to install it into the Math object. For example: seedrandom.monkeyPatchMathObject() |
I think it's rather brilliant to have written the library in such a way that the signature of the library is consistent all the way through and monkey-patching / stand-alone use is simply regulated by wether or not you use the The 'default' in this case is a matter of documentation. As a matter of usability I think it was a well-chosen way of documenting. For a rookie developer it could well be invaluable to have monkey-patching built in and to have it documented well and clear. For a seasoned developer it won't be in the way and documented well enough to understand. |
So a default is always fine and good as long as it is well documented? I still disagree. Edit: and to clarify.. I think that it's undesirable and problematic even to install the seedrandom() function in the Math object. |
Ignore my previous posts, we're getting nowhere fast.
Option 1: And I quote you, 'provide a function to install it into the Math object.' There's nothing here to 'disagree' with, it's just a statement of facts. How do the mentioned code-examples fail to solve your original post? |
The function called seedrandom is monkeypatched into the Math object. It adds a function to a global object that is part of the standard library. The Math.random() patching is the second problem. It's not a big deal, it's just unnecessary and poor style I believe. I'd rather it polluted the global namespace as expected, not the standard library. |
Agreed that it's not the cleanest design, but it's been used this way by a bunch of library users for 5 years, so I'm going to keep it compatible with the way it has always worked since 1.0. Reopen if patching Math.seedrandom is causing functionality problems interacting with some other tool etc. |
I just tracked down a major issue with deterministic array shuffling coming out of our Fisher-Yates shuffle which I've traced to this plugin through the use of the https://github.com/wwwtyro/cryptico/ library. Cryptico did not advertise it was overwriting Math.random() with the 2.0 version of your module. We use it for some encryption in transit on top of our SSL transmission chain. Extremely disappointed by how easily the module encourages monkey patching the standard Math library especially in cases where you can unexpectedly and predictably seed Math.random(). Surprisingly brief code sample that shows how your decisions can impact third-party apps without being properly documented:
|
You know, it's still an optional feature of this library, fix cryptico to use this lib by using |
No. Seriously, it is widely regarded as extremely bad practice to permanently alter the global standard objects, regardless of whether it is done by a library or application code. The overwriting of the random function in the global Math library is not a feature - it is a problem. If this, occasionally, is a desired side-effect for someone using this library, it can be easily done using a one-liner. |
cryptico is a good example of the problem. Though it is in such a weird context that one wonders whether it is an accident or a malicious intentional bug. But it would be good to avoid causing this problem by accident. Maybe v3.0 should break the API and require tests to say Chime in again if you find any other examples of bad usage in bad places. For now I'm assuming it is a rare problem. I have updated the README to include an explicit warning. |
A few things I'd like to add to this discussion First, its considered bad practice to ambiguously alter native instances. Its not about resolving current conflicts but rather future proofing against conflicts. The general rule is to leave native objects pristine and, dependent on environment, add the library to the global object or return it as a module export. Second, the reason for versioning is so that a user-base can choose which version to (continue) using. It is understood by the community that a major version increase is not indicative of new feature sets but rather changes that alter previously defined behaviors. NPM uses major-version bumps to indicate backwards compatibility changes and your cnd requires a version in the URI. As such, I doubt anyone would be disappointed with a v3.x release that contained only a change to the API. |
If it's always considered bad practice to use this library to override |
We can avoid printed warnings, I think, by just fixing the API. How about this, since it's 2019:
Reasonable? The master branch reflects this. My thought is to push a version bump sometime in March. |
Edit: Actually, I would really encourage you to, in version 3.0, remove the helper function that overwrites To be clear, this is what I'm referring to: https://github.com/davidbau/seedrandom/blob/released/seedrandom.js#L95 Removing the monkey-patching will obviously be API breaking for some plainscript users, and I think it would be healthy for those users to explicitly add this to their code when they upgrade to 3.0:
Otherwise looks like a good plan to me! |
I do not want to force people to touch old code for no good reason (from their point of view). On the other hand, I'm 100% in favor of encouraging people to write better future code. Newer usage with |
To encourage future code to use the local prng form, I've updated the readme in master. https://github.com/davidbau/seedrandom/blob/master/README.md |
They would only have to if they upgrade to 3.0 version, however, which means they are already touching the code, as I see it. Whichever path you choose, this is a good change overall. |
Relase 3.0.0 has an updated readme and removes the globals in require-usage. |
As far as I can see, this library overrides or overwrites the Math.random() function.
I don't really see the point. This can very easily be done by the user of this library if desired, but has a lot of potential problems for every other user.
I'd much rather see this library be stand-alone and not monkey-patch the Math object.
The text was updated successfully, but these errors were encountered: