Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That article points out a design decision that the author disagrees with. It does not prove that CoffeeScript's scoping is broken.



The whole point of lexical scoping is that code in an inner scope can't mess with an outer scope's variables without meaning to. CoffeeScript doesn't achieve that. The same code in an inner scope means different things depending on whether the outer scope uses the same variable names. It's broken.


Lexical scoping is a term to classify languages. The 'point' is to make it easier to understand how a language behaves.

If you want to have var with the same name as something in an outer scope, there are ways to do it, specifically an IIFE.

Here's an article for you: https://github.com/raganwald/homoiconic/blob/master/2012/09/...


Also, using really common identifier names (like "log") in outer scopes is a bad idea regardless of how your language's scoping works. Writing `{log} = Math` at the top of the file is just polluting your namespace for no good reason. You shouldn't do it, just like you shouldn't use `from math import *` in Python.


But a function in one part a file shouldn't be broken by the use of bad practices in a completely different part of the file. If I paste a function that works in the REPL into a file with other code, that function should always continue to work (unless it intentionally uses global variables, or another feature that is supposed to interact with other code).

Enforcing best practices by throwing errors where other languages wouldn't (like Go does) is one thing. Enforcing them by introducing difficult-to-track bugs into code that happens to be in the same file as other code which uses bad practices is another. This is a bug, not a feature.


It's not a bug, but it is an unfortunate quirk of the language. But find me a useful language without any unfortunate quirks, and I'll give you some sort of medal.

My point is that as unfortunate quirks go, this one isn't actually that big of a problem in practice, especially if you know to watch out for it and take simple steps to protect yourself from it.


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.


>And my point is that you can't watch out and protect yourself from it

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.


Ok, you can protect yourself by doing weird defensive coding gymnastics, but you can't protect yourself by just writing good code. I'd rather use a language that doesn't make me do gymnastics.


Weird defensive gymnastics? It's just using a language feature the way it's meant to be used. How is that any more "gymnastics" than the ":=" syntax recommended by the article you linked?


So the "do" syntax is meant to be used for all local variables? That seems kind of weird, but I'll take your word for it.


It isn't only for that, and as I said, it doesn't have to be used for that. But it's an effective way to do things, and it is one of the purposes of the construct.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: