|
|
|
|
|
by eurleif
4865 days ago
|
|
And my point is that you can't watch out and protect yourself from it, because the quirk can be triggered by different code from the code you're actually writing. You can carefully write code that works just fine, and then, unbeknownst to you, someone else can modify a completely different part of the file, and break your code. How do you protect yourself from that? By making sure that every single person with commit access always follows best practices? Good luck. Please don't say something along the lines of, 'If you hire bad coders, what do you expect to happen?' Bad coders are almost inevitable, and their harm should be confined to code they actually write. Their bad code shouldn't have spooky action at a distance on good code someone else wrote. |
|
Of course you can. You simply enclose the block in an immediate function (using "do") and declare your local variables as parameters to the function. Do that, and it is impossible for any outer scope to screw your code up.
If you're working on a project where lots of coders of questionable aptitude are going to have commit access, you make declaring local variables this way a required convention.
If you're working on a project where changes get approved by a small number of capable maintainers, then you:
1. Do not allow commits that create short or common names (i, x, arr, log, etc.) in high scope levels, and
2. Require that short or common names in inner scopes be declared using local scoping via "do".
Now, should CoffeeScript give some nicer sugar to encourage this kind of local variable use? Yes, probably. But it is incorrect to say that CS doesn't give you the tools to protect yourself from outer scope pollution.