News for hackers. Stuff that matters.
Re:Discourse ditches CoffeeScript by eurleif on Friday February 22, 2013 @08:48PM (#5268145)
Re:Discourse ditches CoffeeScript by snprbob86 on Friday February 22, 2013 @09:05PM (#5268194)
I greatly prefer Scheme-like lexical scoping. However, JavaScript does not implement lexical scoping properly. Given the particularities of JavaScript's inadequacies, I think that the CoffeeScript design is very reasonable. It's certainly not "broken".

Lots more discussion here: https://news.ycombinator.com/item?id=3379962

Re:Discourse ditches CoffeeScript by batgaijin on Friday February 22, 2013 @10:40PM (#5268442)
Um... I believe JS does have proper lexical scoping. Could you source any evidence to the contrary?
Re:Discourse ditches CoffeeScript by snprbob86 on Friday February 22, 2013 @11:31PM (#5268573)
JavaScript's lexical scoping is function-level. There is no block level lexical scoping, so variables are "hoisted" to the containing scope. This is why people use the (function(foo){...})(bar) trick to create lexical scopes, such as when looping (CoffeeScript's `do` keyword embodies this pattern). In my opinion, "proper" lexical scope implies shadowing without hoisting.
Re:Discourse ditches CoffeeScript by dclowd9901 on Saturday February 23, 2013 @11:24AM (#5270166)
If you understand hoisting as a characteristic of JavaScript, its behavior is very predictable, and thus, hardly broken -- merely tricky if you've yet to encounter a hoisting-related issue.

That said, I find almost any issue with writing JavaScript can be easily mitigated by utilizing JSHint, and you get the added benefit of sticking with the base language, which is better because if you're writing raw JS everyday, your skills are more transferable than if you're writing CS every day if only for the fact that you can still write JS in a CS-only stack, but you can't write CS in a JS-only stack.

Re:Discourse ditches CoffeeScript by ataggart on Friday February 22, 2013 @10:18PM (#5268394)
BTW, ClojureScript has proper lexical scope.
Re:Discourse ditches CoffeeScript by snprbob86 on Friday February 22, 2013 @11:36PM (#5268583)
I'm not sure if you're telling me, or telling other folks in this thread, but for the record: I've fixed bugs in the ClojureScript compiler related to preserving the lexical scoping semantics :-) It's actually quite a tricky process and involves shadow tracking, symbol generation, self-calling function wrappers, and a bunch of other nonsense to deal with JavaScript's crummy semantics.
Re:Discourse ditches CoffeeScript by snogglethorpe on Friday February 22, 2013 @09:49PM (#5268330)
Of course, that link author suggests adopting a Python-like design instead, and Python is even more broken (Python has the most insanely awful scoping rules I've ever seen).

Really the problem is implicit declarations of any sort; they're great for one-liners, but they really suck when you're writing real software.

[But if you've gotta have 'em, please, at least don't copy Python...]

Re:Discourse ditches CoffeeScript by jashkenas on Saturday February 23, 2013 @04:16AM (#5269092)
Nope. CoffeeScript's scoping is working the way it's supposed to be ;)

It's actually a funny thing -- out of all of the folks who actually have tried playing around with CoffeeScript in earnest (participated in issues or the mailing list), I can count on the fingers of one hand the number who have actually disliked working with the scoping semantics. There's a far larger number of folks who like to worry about the scope semantics without ever having tried it.

Re:Discourse ditches CoffeeScript by anon1385 on Saturday February 23, 2013 @05:02AM (#5269191)
In other words, No True CoffeeScript User dislikes the scoping rules…
Re:Discourse ditches CoffeeScript by notb on Friday February 22, 2013 @09:06PM (#5268199)
That article points out a design decision that the author disagrees with. It does not prove that CoffeeScript's scoping is broken.
Re:Discourse ditches CoffeeScript by eurleif on Friday February 22, 2013 @09:09PM (#5268211)
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.
Re:Discourse ditches CoffeeScript by notb on Friday February 22, 2013 @09:28PM (#5268267)
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/...

Re:Discourse ditches CoffeeScript by mistercow on Saturday February 23, 2013 @01:19AM (#5268762)
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.
Re:Discourse ditches CoffeeScript by eurleif on Saturday February 23, 2013 @03:37AM (#5269024)
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.

Re:Discourse ditches CoffeeScript by mistercow on Saturday February 23, 2013 @12:52PM (#5270611)
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.

Re:Discourse ditches CoffeeScript by eurleif on Saturday February 23, 2013 @06:32PM (#5271945)
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.

Re:Discourse ditches CoffeeScript by mistercow on Saturday February 23, 2013 @07:48PM (#5272166)
>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.

Re:Discourse ditches CoffeeScript by eurleif on Saturday February 23, 2013 @08:09PM (#5272240)
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.
Re:Discourse ditches CoffeeScript by mistercow on Saturday February 23, 2013 @08:25PM (#5272294)
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?
Re:Discourse ditches CoffeeScript by eurleif on Saturday February 23, 2013 @08:54PM (#5272382)
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.
Re:Discourse ditches CoffeeScript by mistercow on Sunday February 24, 2013 @11:39AM (#5274790)
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.
Re:Discourse ditches CoffeeScript by rst on Friday February 22, 2013 @09:32PM (#5268278)
There are Coffeescript forks that try to deal with issues like this. Here's one that takes a different approach on a bunch of 'em, including the scoping:

https://github.com/satyr/coco