Live data from Hacker News

The Problem with Implicit Scoping in CoffeeScript

lucumr.pocoo.org

31–40 of 137 posts

Re: The Problem with Implicit Scoping in CoffeeScript

#31
Why not allow CoffeeScript to use the 'var' keyword, explicitly telling CS that this variable should be scoped locally even though it may be shadowing another variable? This seems consistent with their approach of allowing (although optional) native javascript syntax such as {}, and []. This still allows CS to stick to it's paradigm of forbidding shadowing unless we explicitly state that we know what we're doing.

Re: The Problem with Implicit Scoping in CoffeeScript

#32
post #28

Earlier quoted context omitted.

But he writes coffeescript so unlike you he's a "top programmer". except for times when he writes javascript which probably would be when hes drunk or for whatever reasons his IQ has dropped.

99% of the popular people out there are just above average enough to make useful projects for those lesser experienced. Most truly talented people are not very well known, they're busy hacking on v8, the kernel etc. Tools for average programming just get more of a spotlight. Is Express complex? no, people just use it, is coffeescript complex? no, is boostrap complex? no..

That sounds like a really accurate statistic. So what's your metric for this? Besides the obvious law that low-level code is the only grounds for elite coding, I mean.

Re: The Problem with Implicit Scoping in CoffeeScript

#33
post #20
post #6

@mitsuhiko Not gonna happen ;) Forbidding shadowing altogether is a huge win, and a huge conceptual simplification. How arrogant! You'd think he'd step back for a second and consider the suggestion, but it sounds like he's on autopilot.

I think this is a recurring discussion from the dawn of coffeescript. See for example: https://github.com/jashkenas/coffee-script/issues/238 I really don't understand why this isn't being fixed: doesn't global by default break encapsulation? I'm probably missing something, but this is the main reason I haven't tried coffeescript yet.

Just to be completely clear, variables are only at top level scope if you declare them at top-level scope. The variables "x" below are completely encapsulated within f1 and f2. The variables at top-level scope are intentional in the code below--I really do intend f1, f2, and how_many_times_functions_have_been_called to refer to the same entity throughout the file.

  how_many_times_functions_have_been_called = 0

  f1 = ->
    how_many_times_functions_have_been_called += 1 # refers to top-level scope
    console.log x # undefined
    x = 1
    console.log x # 1

  f2 = ->
    f1() # refers to f1 at top-level scope
    how_many_times_functions_have_been_called += 1 # refers to lop-level scope
    console.log x # undefined
    x = 2
    console.log x # 2

  f1() # you can call f1, it's at top-level scope
  f2() # you can call f2, it's at top-level scope
  console.log how_many_times_functions_have_been_called # 3, refers to top-level scope
  console.log x? # false, x does not exist at top_level scope

Re: The Problem with Implicit Scoping in CoffeeScript

#34
post #28

Earlier quoted context omitted.

But he writes coffeescript so unlike you he's a "top programmer". except for times when he writes javascript which probably would be when hes drunk or for whatever reasons his IQ has dropped.

99% of the popular people out there are just above average enough to make useful projects for those lesser experienced. Most truly talented people are not very well known, they're busy hacking on v8, the kernel etc. Tools for average programming just get more of a spotlight. Is Express complex? no, people just use it, is coffeescript complex? no, is boostrap complex? no..

Like anything else in life there is front facing people that are generally good at marketing and would use other people's hard work to build technically inferior stuff on and be famous for. Not necessary a bad thing. The truly talented most of the time choose to go unnoticed. And there is always trolls like you who I don't know how they find the time to ship code ;p

Re: The Problem with Implicit Scoping in CoffeeScript

#35
post #32

Earlier quoted context omitted.

99% of the popular people out there are just above average enough to make useful projects for those lesser experienced. Most truly talented people are not very well known, they're busy hacking on v8, the kernel etc. Tools for average programming just get more of a spotlight. Is Express complex? no, people just use it, is coffeescript complex? no, is boostrap complex? no..

That sounds like a really accurate statistic. So what's your metric for this? Besides the obvious law that low-level code is the only grounds for elite coding, I mean.

Easy, he hand picked a 100 people of the JS community (including him) and then decided that hes the only competent programmer amongst them.

Re: The Problem with Implicit Scoping in CoffeeScript

#36

FWIW this is how CS works: top_level_variable = null f = -> top_level_variable = "hello" f() console.log top_level_variable # prints hello

The concern is that because Coffeescript automatically scopes variables to the scope of their first reference, it can introduce maintenance issues. Consider the following:

    foo = ->
      bar = "woot!"
      console.log bar
This compiles to:

    var foo;
    foo = function() {
      var bar;
      bar = "woot!";
      return console.log(bar);
    };
bar is locally scoped to foo(). Now, 2 weeks later and 200 lines earlier, you come along and define:

    bar = ->
      alert "Holy crap cheese is awesome!"
Which compiles to:

    var bar, foo;
    bar = function() {
      return alert("Holy crap cheese is awesome!");
    };
    foo = function() {
      bar = "woot!";
      return console.log(bar);
    };
Now, all of a sudden, the "bar" reference in foo isn't scoped to foo() anymore, it's scoped globally, and once you invoke foo(), it'll replace the function bar with a string, potentially breaking your app. It's an ease-of-maintenance issue.

This isn't consistent behavior, though. If you define your top-level bar() function after foo, like so:

    foo = ->
      bar = "woot!"
      console.log bar

    bar = ->
      alert "Holy crap cheese is awesome!"
Then you get "correct" scoping (and the outer bar is shadowed):

    var bar, foo;
    foo = function() {
      var bar;
      bar = "woot!";
      return console.log(bar);
    };
    bar = function() {
      return alert("Holy crap cheese is awesome!");
    };
On one hand, it could be argued that this is a "name things better" problem, but on the other, I have to agree that it'd be nice to be able to explicitly scope things when needed. Given that the behaviors are divergent based on what order the variables appear in, I'd say it's confusing enough that a way to explicitly say "hey, I know what I'm doing, I want to shadow any outer variables and declare local scope here" would be useful.

Re: The Problem with Implicit Scoping in CoffeeScript

#37
Considering we won't see this changed since the author has already closed the issue and expressed his satisfaction with the current rules this article should at least serve as a reminder for errors not to repeat with the next language someone designs.

It's open source. Why not fork it and get some like minded coders to change it with you?

Re: The Problem with Implicit Scoping in CoffeeScript

#38
post #35
post #32

Earlier quoted context omitted.

That sounds like a really accurate statistic. So what's your metric for this? Besides the obvious law that low-level code is the only grounds for elite coding, I mean.

Easy, he hand picked a 100 people of the JS community (including him) and then decided that hes the only competent programmer amongst them.

umm no haha, I'm certainly amongst the average. Trust me writing parsers is very trivial, I've written many. Thinking you're a good programmer is perhaps the most naive thing you can do

Re: The Problem with Implicit Scoping in CoffeeScript

#39
post #2

Scheme got this right in 1970. There is no excuse to design a new language this way.

Scheme dates to about 1975. But there were older languages with this kind of scoping, like John Reynolds' Gedanken and Landin's ISWIM. (I guess I wouldn't count Algol-60 since it was call-by-name.)

Re: The Problem with Implicit Scoping in CoffeeScript

#40
post #35

Earlier quoted context omitted.

Easy, he hand picked a 100 people of the JS community (including him) and then decided that hes the only competent programmer amongst them.

umm no haha, I'm certainly amongst the average. Trust me writing parsers is very trivial, I've written many. Thinking you're a good programmer is perhaps the most naive thing you can do

I know its trivial once demystified specially using parser generators. But language design isn't though. Anyways i think your a pretty decent programmer.
Post reply on HN