r/programming • • Jul 25 '13

CoffeeScript's Scoping is Madness

http://donatstudios.com/CoffeeScript-Madness
207 Upvotes

315 comments sorted by

14

u/cashto Jul 25 '13

For more discussion, see the ticket on github.

There exists a fork of CoffeeScript (named Coco) in response to this and a handful of other issues. I haven't used it though.

3

u/donatj Jul 25 '13

I was not aware of this, I really hope it gains traction.

28

u/dukerutledge Jul 25 '13

Reading that ticket is maddening! All of the responses amount to:

You are right the language can squash globals and cause bugs, but you should use a better style and use TDD.

How about you just fix it at the language level instead of forcing the language's users to conform to an undocumented style?

21

u/x86_64Ubuntu Jul 25 '13

Because it can't have a place in the JS world if it doesn't at least pay some homage to the weird scoping issues.

6

u/[deleted] Jul 26 '13 edited Jul 26 '13

Shifting responsibility like that is one of my biggest peeves in programming. Everyone wants things to be someone else's problem. Sometime that's good (library reuse means one really good wheel that doesn't get needlessly reinvented) but also means the people best positioned to address things don't have the understanding and/or desire to be bothered.

Yes, better style and testing lead to better code. BUT all three - style, testing and language features - would lead to even better code all around. I'm not saying there isn't good justification for not handling scoping better in cs, but this is not it.

The "use TDD" one really gets under my skin because it leans towards the road where any problems your project faces can be hand-waved away by for not being a true enough Agile Scotsman.

10

u/[deleted] Jul 25 '13

Fanboys. Fanboys everywhere.

2

u/[deleted] Jul 26 '13

It goes both ways. JS Fanboys won't dare use any other language.

0

u/gthank Aug 06 '13

Because it's not a bug. It's not like he's stupid and can't figure out how to change it. It was intentional. He likes it that way. If you don't, use something else.

1

u/dukerutledge Aug 08 '13

Woah, grave digging are we?

I know it is not a bug, but it is a language "feature" that can and does cause bugs. In my eyes that is a bad feature.

1

u/gthank Aug 08 '13

My biggest objection is your use of the word "fix". It's not a bug, since jashkenas has stated repeatedly that it's intentional and in his mind it prevents more bugs (where people shadowed a variable by accident) than it might cause (where people wanted to shadow a variable, and didn't, or accidentally would have shadowed a variable in JS, but didn't in CoffeeScript). Feel free to disagree, but unless you've got some statistically sound study, preferably peer-reviewed, then it's obnoxious to still be clamoring for him to "fix" it. Honestly, even if you phrased it as "change" instead of "fix", it would be slightly annoying, since it's well-documented by now that it's not going to happen, but at least you aren't implying the superiority of your design aesthetic over his when he's got a strong track record of creating technically impressive Open Source projects that thousands of developers find useful. One you almost certainly (unless you're secretly DHH or John Resig or something) can't claim to match.

NOTE: I probably wouldn't have done it the CoffeeScript way (I'd force people to actually declare their variables—shocking, I know—and scope it from declaration to the end of the closest enclosing block, but that's not really relevant to this discussion). That said: I admit he has a point. For the first class of potential bugs: shadowing variable names is just a bad, error-prone idea. For the second class of potential bugs: if your code is nested deeply enough you can't spot the problem, you need to re-organize your code, because you've probably got other, harder-to-see bugs.

5

u/Plorkyeran Jul 25 '13

Coco still has some of its own problems, and I think that exposing Javascript's == operator is a terrible idea.

Other CoffeeScript-like languages that may be better include LiveScript (which is itself based on Coco, but with a lot more features) and GorillaScript. If I start a new project involving a substantial amount of JS I'll probably end up using one of them (or at least try using one of them).

6

u/lakmeer Jul 25 '13

Using Livescript for over a year now, never going back. It uses shadow-by-default with := operator for explicitly assigning to outer scopes. Prevents accidental clobbering and encourages purity and functional thinking. Recommend.

12

u/[deleted] Jul 25 '13

It's interesting to read through the ticket. Obviously the maintainers are opinionated (and that's their right of course), but still reading it, it reminds me of some posts from lolphp. The php apologists in their bug tracker, etc, making crazy excuses for a broken language. "Doctor, it hurts when I do this," and the doc then says "Don't do that." Not that I think cs is anything like php. cs looks quite nice, but I'm also weary of YAL (yet another language).

And I guess this maybe shows an issue with using a language approach such as with coffeescript, which is essentially surface sugar for js. That you still need to maintain rigorous programming methodologies and approaches, not just to aid you in creating the program, but also to guard against the pitfalls of the language, which is really the most laborious and rotten part of browser side javascript today.

Or maybe cs removes the other 90% of pitfalls from js, and it just has this one, I still think I'll stick with straight js (15 years of it tends to get on your backbone), and if I had a choice to make, I'd probably go with typescript or dart.

0

u/MatrixFrog Jul 26 '13

To be fair, CoffeeScript most likely had an effect on the design of Dart, Typescript, and the upcoming versions of JS.

So even if you choose not to use CoffeeScript (and I agree, I have no desire to use it either) it still has a positive effect on your life.

4

u/masklinn Jul 26 '13 edited Jul 26 '13

To be fair, CoffeeScript most likely had an effect on the design of Dart, Typescript, and the upcoming versions of JS.

IIRC, C#'s lambda expression predate CS's by 2 years and they're almost identical (the biggest difference is that CS has two different "arrows" for capturing the current this or not). And Scala — which again has almost identical lambda syntax) predates C# 3.0 by ~4 more years.

And its syntax looks a lot (though it's not identical to) Haskell's, whose 1.0 definition predates Scala by 13 years.

CoffeeScript (2009): (x, y) -> expr(x, y) or (x, y) => expr(x, y)
C# 3.0 (2007): (x, y) => expr(x, y)
Scala (2003): (x, y) => expr(x, y) (also shorthand along the lines of expr(_, _), not verified)
Haskell (1990): \x y -> expr x y

edit: fixed Haskell lambda, uses -> not =>

1

u/oantolin Jul 26 '13

The Haskell one should have -> instead of =>.

1

u/masklinn Jul 26 '13

Indeed, fixed. Sorry about that.

1

u/[deleted] Jul 25 '13

What definition of "scope" is being used here? It seems obvious that an exception should be thrown if a variable that is not in scope is read. And how is err not in scope?

0

u/crusoe Jul 25 '13 edited Jul 25 '13

Ruby has the same damn prob with variable definition and assignment, and its bitten me a few times.

coat = "leather"

caot = "fur"

Why isn't my coat changing???!!!!

All of this to simply avoid declaring variables using "var" or some other system. Conflating variable definition with assignment is gross.

"I'm too much of a l33t web 2.0 brogrammer hopped on redbull to be bothered by having to type three extra characters to declare a variable!!"

2

u/cashto Jul 25 '13

That problem applies to JavaScript too.

6

u/bart2019 Jul 25 '13

But in Javascript, you can fix the problem. In Coffeescript, you can't.

Plus, Javascript has the "use strict" pragma (inherited from Perl)

-1

u/virtyx Jul 25 '13

From Coco:

Principles:

  • Perl over Ruby.

shudder

My first impression is that they are departing a bit too much from CS syntax (e.g. making () -> invalid). But it does seem like an interesting project.

13

u/virtyx Jul 25 '13

From the Coco examples:

mixin = (...objects) ->
  for objects => @ <<< &
  this

And

!=x

Uh. Yeah. On second thought I think I'll stay away.

3

u/seruus Jul 25 '13

Well, he did a good job trying to make the language unreadable.

1

u/Plorkyeran Jul 26 '13

Making () -> invalid solves the foo () -> ... vs foo() -> ... problem, but I'm not convinced that's a problem that desperately needs to be solved.

3

u/kungfufrog Jul 26 '13 edited Jul 26 '13

As someone who programs in Javascript for a living but has never written a single line of CoffeeScript, why is this a problem? I don't get what the actual complaint is.

Is the complaint that it is non-obvious that the closure around the test function includes the var y defined outside the function's scope?

I use outside scoped variables all the time to pass information down through async chains. I also don't use generic variable names like 'y'.

5

u/donatj Jul 26 '13

The problem is that there is no way to not outside scope variables.

1

u/kungfufrog Jul 26 '13

Thank you for the succinct and informative reply.

29

u/brandjon Jul 25 '13

So it's basically unannotated declarations like Python, but everything is nonlocal or global by default, with no "local" keyword to override?

Yeah, that sounds like madness to me.

2

u/[deleted] Sep 21 '13 edited Sep 21 '13

The "local" in coffeescript is a "do" function. For example:

x = 1
y = 2

g = ->
  x + y

do (y = null) ->
  h = ->
    x + y

compiles to...

var g, x, y;

x = 1;

y = 2;

g = function() {
  return x + y;
};

(function(y) {
  var h;
  return h = function() {
    return x + y;
  };
})(null);

If I had left out the y argument for the do block (or forgotten to assign it something), it would have used the global version. Take it or leave it, it is possible to handle scoping this way.

The most important thing is that all variable declarations are scoped to the file. There are no global scoped variables; if you want to export something, you need to attach it to the window/exports/whatever global object you have available. The idea is that if you have a file with multiple scopes using different variables with the same name, your file is too big and you should separate concerns. Do blocks should very rarely be necessary.

A similar argument comes up with coffeescript's use/non-use of parentheses, which can be confusing when trying to figure out whether you need them or not. The problem usually stems from people writing confusing one liners, which end up being hard to read. The solution is to write coffee in such a way that every single line is concise and simple. Other than chaining calls, I've never written a line where I absolutely needed parentheses. If I did, it made sense to split the line in two.

Basically, all of the issues with coffeescript are solved by writing short, concise module files with short, concise lines of code. Devs who want to write sprawling files with big one liners won't like coffeescript.

-8

u/tiglionabbit Jul 26 '13

No. It's exactly like Ruby.

That is, it forbids shadowing except for function arguments.

23

u/mernen Jul 26 '13

With the extremely important caveat that in Ruby standard variables can never cross method boundaries. You need a constant, a global or a class variable for that, which are all syntactically different and live in their own namespaces.

0

u/tiglionabbit Jul 26 '13

The CoffeeScript developers seem to have modeled all functions in JavaScript after blocks in Ruby. There was no need for additional syntactic distinctions.

10

u/[deleted] Jul 26 '13

Generally "at work" I avoid CoffeeScript, primarily for the reason I still work with a lot of folks that don't understand JavaScript... but in general, I find it's good to familiarize yourself with the JS output of CS, then just treat CS like shorthand for JS.

... in otherwords, when I'm using CS I'm still thinking in JS.

I don't at all recommend CoffeeScript to anyone that isn't already relatively skilled in JavaScript.

2

u/[deleted] Sep 06 '13

I don't at all recommend CoffeeScript to anyone that isn't already relatively highly skilled in JavaScript.

56

u/Plorkyeran Jul 25 '13

While I agree with the title of this post, in the process of writing ~20k lines of CoffeeScript it hasn't actually ever bitten me, unlike some other problems with the language. Avoiding deeply nested scopes (and having too many things in scope in general) makes it easy to avoid issues, and IMO that's a good idea even in languages with sane scoping anyway.

24

u/[deleted] Jul 25 '13

it bit me all the time before I figured out what was happening. also it's bad design because it makes it way too easy to smash globals like module and function names. you do learn to avoid it though, and otherwise, CoffeeScript is great. The function syntax is a must for a language like JS too.

8

u/ElvishJerricco Jul 25 '13

I've never really liked the syntax used in coffeescript for functions. Why am I wrong?

10

u/tmetler Jul 25 '13

In Javascript you pass functions around constantly. Being able to write:

[1,2,3].map (x) -> x*2

takes so much of the pain out of callbacks.

26

u/flying-sheep Jul 25 '13

ECMA 6:

[1,2,3].map(x => x*2)

try it in a firefox near you!

21

u/tmetler Jul 25 '13

Absolutely, but harmony was actually influenced by C# and coffeescript. It'll be great when it's widely supported. Definitely a big step in the right direction.

2

u/flying-sheep Jul 25 '13

and python, i know :)

they are refreshingly blunt on where they took the features from.

6

u/[deleted] Jul 26 '13

And Scala.

4

u/nachsicht Jul 27 '13

To be truly inspired by scala it should be [1,2,3].map(_ * 2)

→ More replies (2)
→ More replies (1)

4

u/[deleted] Jul 25 '13

See the difference is we can start writing shorter code now rather than waiting for Half Life 3 to be announced.

6

u/elder_george Jul 25 '13

Same syntax works in Typescript (along with many other niceties from ECMA 6), so it's possible to start using it right now.

2

u/flying-sheep Jul 25 '13

if you’re writing firefox extensions, you can have that right now.

but i getcha. i was just giving perspective.

→ More replies (1)

4

u/passwordstillWANKER Jul 25 '13

I don't understand the value in not having a function call operator in a language without partial application.

2

u/tmetler Jul 25 '13

I'm not sure what you mean? Do you mean that in coffeescript the () after a function are optional? I find that it helps with readability with nested functions and callbacks, although it has bitten me before, but for me the readability outweighs the cons.

Also, javascript does have partial application with the bind method, or you can use function scoping to do it yourself:

var foo = function (x, y) {...}

var bar = function (y) {
  return foo(5, y);
}

1

u/[deleted] Jul 26 '13

I never leave out the brackets in function calls anymore. In some cases it looks cool, but in most cases it's just irritating.

8

u/jashkenas Jul 25 '13

unlike some other problems with the language

Feel like sharing 'em?

16

u/Plorkyeran Jul 25 '13

Limiting it to just things that have actually caused problems rather than merely irking me:

The syntax makes it too easy to forget the () on function calls. This is a problem I don't think I've ever had in any other language, but I've done it a few times in CS and seen others do it as well. Bare super being a function call probably contributes to this.

Trying to cram for and map into a single thing, as covered in that thread. Even primarily expression-based languages (Scheme being the one I have the most experience with) still generally keep them separated for a reason.

Deindenting a different distance than you indented is legal and "works". Pops up rarely (usually due to c&p), but it can have really confusing results. Thankfully coffeelint can check for this.

On the whole I'm quite a fan of CoffeeScript as it's one of the few languages I've used that feels like being pleasant to use was actually a goal of the language, but it still definitely frustrates me at times.

7

u/MatrixFrog Jul 26 '13

Thankfully coffeelint can check for this.

Why not build that into the compiler? The whole point of a compiled language, in my opinion, is the guarantee that, if it compiles, it's highly likely to be correct. Why do I need a separate tool to do basic checks like that?

13

u/jashkenas Jul 26 '13

You're right -- the compiler should be doing this. Let's add it.

9

u/jashkenas Jul 25 '13

Thanks, that's helpful.

You're right that the bare super keyword is a bit of an odd-duck ... it exists because the 90% use-case for super is to forward all of the arguments that your overridden function received. Currently, you just write:

myFunc: ->
  super

... but if we didn't have that, you'd have to do something more like this all the time:

myFunc: ->
  super.apply(this, arguments)

Having loops that function as loops when you use them as loops, and function as comprehensions when you try to use their value, is a pretty core feature. It's a nice conceptual simplification, and if we removed it, then the story would be: everything's-an-expression-except-for-loops, which would be a bit sad.

You're right about different-distance dedenting. I think the reason why it's valid is to support use-cases like this:

object.method a, b,
              c, d

... but we could probably do better to detect cases that are obviously incorrect, and flag them as syntax errors. Feel free to open a ticket if you'd like to get this rolling.

4

u/redditthinks Jul 26 '13 edited Jul 26 '13

Another thing, implicit parentheses should be less greedy. For example:

a = myFunc x

Now I realize I want to add a number to it:

a = myFunc x + 1

This looks very ambiguous, and CoffeeScript compiles it to myFunc(x+1) instead of myFunc(x) + 1.

Something similar to this happened to me and led to a subtle bug. This is particularly an issue when you want a default value:

elem.getAttribute 'data-info' or ''

I think it should stop capturing when it meets an operator.

Also, better multiline strings (ala Python):

message = ('Hello'
           ' world!')

This is not possible now in CoffeeScript:

message = "Hello
            world!"

will give:

message = "Hello                world!";

5

u/jashkenas Jul 26 '13

implicit parentheses should be less greedy.

I disagree. The rule that implicit parentheses (generally) follow, is that they extend to the end of the line, or to the end of the block that ends the line. To riff on your examples, it allows for uses like this:

print message or defaultMessage

bank.deposit currentBalance + incomingCheck

... and if you need tighter binding, you simply write the parens:

total = bank.lookup("checking") + bank.lookup("savings")

This is not possible now in CoffeeScript

Oh, but it is ;) If you don't mind the new line, block strings gobble-up all of the left-hand indentation for you: http://coffeescript.org/#strings

1

u/[deleted] Jul 26 '13

The thing I sometimes run into in CS is that defining a lambda as the first argument of a function when there are more than one argument does only work if you put the thing in brackets:

foo((->), bar)

→ More replies (2)

1

u/redditthinks Jul 26 '13 edited Jul 26 '13

The rule that implicit parentheses (generally) follow, is that they extend to the end of the line, or to the end of the block that ends the line.

Hmm, good point. However, that encourages what I see as 'floating arguments'. Extending to the end of the line is a good idea if it doesn't meet an operator, like the example on the front page:

console.log sys.inspect object

Furthermore, I think it would be good to encourage wrapping a long argument with operators in parenthesis.

If you don't mind the new line

That's the problem :)

A common example is a long URL which are sensitive to new lines, extra spaces, etc. I think the current implementation of double quoted, multiline strings is very limited as it only works properly in the outermost scope, otherwise it doesn't respect indentation, making the code look out of place (and interfering with Vim's folding ;) ).

1

u/acdha Jul 26 '13

Is there a way to disable implicit parenthese entirely? I like a lot of what's in CoffeeScript but that class of behaviour has lead to bugs, usually subtle, in every language where I've encountered it.

5

u/Plorkyeran Jul 26 '13

... but if we didn't have that, you'd have to do something more like this all the time:

myFunc: -> super.apply(this, arguments)

That could be solved with more generalized sugar along the lines of Python's super(*arguments), which would eliminate the need for the unusual special-case and be applicable elsewhere.

I'm not sure that super is actually the cause of my issues here though. Probably a bigger factor is that I'm a fan of the following syntax, which makes me overly used to seeing function names with nothing after them for calls:

obj.foo
  name: 'a'
  className: 'btn'

Having loops that function as loops when you use them as loops, and function as comprehensions when you try to use their value, is a pretty core feature. It's a nice conceptual simplification, and if we removed it, then the story would be: everything's-an-expression-except-for-loops, which would be a bit sad.

I disagree about it actually being a simplification, since I see "loop over a list of items and trigger side effects" and "transform a list of values into a different list of values" as fundamentally different algorithms, and deciding which algorithm to use is outside the scope of what a compiler should be doing.

Probably not worth writing more words on this unless this is somehow a novel argument that you haven't seen.

You're right about different-distance dedenting. I think the reason why it's valid is to support use-cases like this:

object.method a, b, c, d

That merely requires variable-width indenting. The case that's problematic is the following:

a = ->
  b
    c: d
   e
  f
 g

I can't really imagine any reason why one would want to do this intentionally, and a, f, and g all being treated as the same indentation level is really weird. Python solves this by requiring that dedents return to a previously used indentation level, which makes e an error without breaking the above sensible formatting.

6

u/munificent Jul 26 '13

everything's-an-expression-except-for-loops, which would be a bit sad.

It could still be an expression, just one that always evaluated to undefined. The return expression doesn't have a meaningful evaluated value either, for that matter. (Which is natural, of course, since there would be no way to capture it.)

12

u/sidfarkus Jul 25 '13

I'm with you on this one; this seems like a big deal until you write some real coffeescript and realize that your scopes are so small most of the time you never have to worry about what's declared in a parent scope.

I don't think I've ever had a bug due to this in the ~12k lines of coffeescript in our app. In fact more often than not I end up with too narrow a scope given that coffeescript inserts closures quite aggressively.

17

u/cashto Jul 25 '13

Recently I wrote a server that totals about ~1k of code. I didn't get bit by this, largely because a) single author and b) I have very few things declared at the top level; most of my code is in classes.

OTOH, the number of bugs I wrote that would have never gotten past a typechecker was ridiculous.

IMHO that's the biggest challenge by far when doing large-scale CoffeeScript programming, and it's the same challenge you have in JavaScript too. In isolation, it might be infuriating that CoffeeScript has this one bugaboo that Javascript doesn't have, but if you step back and look at the big picture, there's so much shit you have to deal with in either language that in comparison this issue is pretty inconsequential indeed.

1

u/AgentME Jul 26 '13

I love Typescript for adding typechecking (and some ES6 goodies) to Javascript. It regularly saves me from a lot of bugs. I'm considering moving some of my existing codebases to Typescript.

1

u/nachsicht Jul 27 '13 edited Jul 27 '13

Dealing with huge javascript codebases of 10k or more lines is what put me in the static,strongly typed camp for life.

→ More replies (2)

6

u/xofy Jul 25 '13

I really like the feel of CoffeeScript, the sugary Pythonesque goodness, but this quirk bit me so many times I've gone back to JS.

Another crazy one is having a loop as the last part of a function - CS builds a results list to return the "result" of the loop:

updateArray = (arr) ->
    for i in [0...arr.length]
        arr[i] += 1

11

u/cashto Jul 25 '13

That's not crazy.

In CoffeeScript, 'for' is an expression that returns a value, not a statement. Also in CoffeeScript, the value of the last expression of a function is its return value (no need to explicitly say 'return').

Put two and two together ...

13

u/Plorkyeran Jul 25 '13

Having a piece of code compile into dramatically different things depending on whether or not it's the last expression of the function is pretty crazy.

Things which individually are perfectly sensible combing into a pretty undesirable end result is a classic indicator of a language that's just a collection of features with no overarching design.

9

u/jashkenas Jul 25 '13

In fact, it's an integral part of the design. A lot of effort is present in the compiler to make is possible to turn things which would normally be statements in JavaScript, into expressions -- but in order to do so optimally, only when you actually use them as expressions.

If you haven't played around with it already, try this:

switch letter
  when "A" then parseA()
  when "B" then parseB()

Versus this:

result = switch letter
  when "A" then parseA()
  when "B" then parseB()

... there are many more examples. if/else, try/catch, first-time variable assignment ...

1

u/didroe Jul 26 '13 edited Jul 26 '13

What you're saying is orthogonal to whether return is implicit. I think the point is that with your example, the intent is clear and is specified solely in that line of code. Where as with return, there's no syntactic difference in the line of code, it's done entirely by position. So it's not obvious which version of a language construct you're asking for. This is even worse when you consider that commenting out a line of code can change the meaning of a seemingly unrelated line of code (commenting out the last line of a function potentially changes the line before's meaning).

Variable scoping seems like a similar issue. With both things, you need to consider a larger scope of code to understand the meaning of something. Whereas other languages could encode that meaning in a more local way. What I find odd (and quite interesting) is that this complexity of understanding comes from the pursuit of simplicity in the syntax. I think it shows that simple syntax does not mean simple semantics, and the two may even be opposed in some situations.

5

u/cashto Jul 25 '13

Having a piece of code compile into dramatically different things depending on whether or not it's the last expression of the function is pretty crazy.

Having "-> 3" compile to "function() { return 3; }" rather than "function() { 3; }" is not "dramatically different".

If you disagree, then suppose you will find most functional languages to be "pretty crazy" according to that standard.

6

u/Plorkyeran Jul 25 '13
foo = ->
    for i in [0..3]
        doStuffWith i

bar = ->
    for i in [0..3]
        doStuffWith i
    otherFunction()

compiles to

var bar, foo;

foo = function() {
  var i, _i, _results;
  _results = [];
  for (i = _i = 0; _i <= 3; i = ++_i) {
    _results.push(doStuffWith(i));
  }
  return _results;
};

bar = function() {
  var i, _i;
  for (i = _i = 0; _i <= 3; i = ++_i) {
    doStuffWith(i);
  }
  return otherFunction();
};

The for loop here is compiling to things that differ in more ways than the presence of return based on whether or not it is the last expression of the function.

10

u/cashto Jul 25 '13

Yes, it's optimizing away the unused _results variable in the second example.

Hopefully you aren't denouncing every language that allows a compiler to perform dead-store optimization as being merely a jumble of features without an overarching design.

3

u/Plorkyeran Jul 25 '13

CoffeeScript is not marketed as an optimizing compiler. Quite the opposite, in fact: the homepage emphasizes that it's a very simple and straightforward translation to JS. I've had multiple people ask me questions along the lines of "why does this code get so much slower when I comment out the console.log call at the end of the function"[0] due to this, because the language supports treating for loops as if they were statements just well enough to be confusing. I think that implicit returns and expression-for-loops are individually both good things, but they combine poorly and a well-designed language would have found a way to avoid that.

[0] It's obvious enough when you look at the JS, but people new to CS often need a few nudges to get into the habit of looking at the JS whenever they have an issue.

6

u/hderms Jul 25 '13

it doesn't even make sense to me why a for loop would return a collection. It seems very bizarre that it would have those semantics (it's 'for', not 'map')

5

u/[deleted] Jul 25 '13

Ruby works the same, every statement is also an expression. I never saw the use with for and each() either though, it just causes problems and confusion.

5

u/loz220 Jul 25 '13

I'm guessing because the cofeescript author(s) decided everything was going to be an expression even in situations where it's totally redundant or can bite you. Kind of like the example Plokryeran provided.

Now if you use coffeescript you always be aware of this behaviour. If you don't want your code to be doing needless work you have to end it with an empty return instead.

→ More replies
→ More replies (1)
→ More replies (1)
→ More replies (1)

3

u/[deleted] Jul 25 '13

It is a gotcha if you care about generating garbage. But that's a pretty small subset of javascript...

1

u/masklinn Jul 26 '13

In CoffeeScript, 'for' is an expression that returns a value, not a statement.

It's also an expression which returns a value in Ruby, that doesn't mean for and map have to be conflated.

1

u/nachsicht Jul 27 '13

It doesn't have to be either/or. In scala, for is an expression too, but we can control its return type:

val x = Array(1,2,3)
for(i <- 0 to 2) {
   x(i) = i * 2
} //returns (), which is Unit. This behaviour is analogous to C++ or java's for loops.

for(i <- 0 to 2) yield {
  x(i) = i * 2
} //returns Vector((),(),())

2

u/mitsuhiko Jul 27 '13

I like rust's solution. The presence or absence of the semicolon after the last expression indicates weather the return value is the expression's result or nil. If the semicolon is left off the value is returned, otherwise the expression result is discarded.

0

u/Arelius Jul 26 '13

but this quirk bit me so many times I've gone back to JS.

Agreed, this one problem is worse than all the problems that Javascript has. I felt weary from the beginning about lack of variable declarations, it biting me just showed one reason why.

4

u/illamint Jul 25 '13

Yeah, over 20,000 lines of CoffeeScript in production for us and it's never been a problem. 10,000 or so at my last gig and, again, never a problem.

18

u/Eirenarch Jul 25 '13

Don't worry you'll find your problem at some point. It is there somewhere in those 20K lines :)

16

u/vincentk Jul 26 '13

Most likely, somebody else will find the problem. After first having to study 20K lines.

1

u/mitsuhiko Jul 26 '13

The big problem is that this scoping bug is usually related to the order in which you call functions. You might already have made that mistake, you just have not seen it yet.

2

u/[deleted] Jul 25 '13

Same here. After tens of thousands of lines of CoffeeScript I've never eaten a shadowing bug.

I haven't used it, but LiveScript uses the := syntax.

1

u/Arelius Jul 26 '13

I was working in a 20k project, when I had it bite me twice in the same day. I started searching around the project and found at least two more broken edge cases that just hadn't been caught yet.

1

u/ponytoaster Jul 29 '13

May I ask what it is your writing? 20K of Javascript seems excessive for a client. Keep business logic on the serverside :)

1

u/notorious1212 Jul 25 '13

I have sympathy for this only because of the callback hell inherent with javascript libraries, but I still haven't run into an issue with it myself. If you are relying on this for large swaths of your code then I don't want to be on your team. If this is a serious issue then you have serious problems that aren't coffeescript.

24

u/iopq Jul 25 '13

This is really WTF. Not because of its practical significance, but because this is what I would expect of a language like bash or something. Have the creators of CoffeeScript heard of lexical scoping?

18

u/rwbarton Jul 26 '13

It's pretty sad, considering that ALGOL 60 is half a century old, that people are still inventing languages that play guessing games with scope. Just require every variable to be declared and these problems disappear. Is it really so hard to type "var"?

5

u/masklinn Jul 26 '13

Technically (as many other languages do) CoffeeScript uses lexical scoping with implicit scope declaration. The issue is the latter, and the way it infers scope.

I don't think scope inference is a good idea to start with, but the way CoffeeScript does it is almost the worst possible way (the worst one being javascript's hey-you-did-not-declare-that-here-is-a-new-global-for-you).

-5

u/tiglionabbit Jul 26 '13

This is lexical scoping. It is just avoiding shadowing.

15

u/iopq Jul 26 '13

Lexical scope IS shadowing. Inner scopes win over outer scopes, think of it as a scope stack. It's not shadowing, it's rather being at the top of the stack for that binding.

You know what this guarantees? When I refactor the code and I take out a var inside my JS function, I am guaranteed that I didn't break any other code. If I refactor an unknown CoffeeScript function, I have no idea. I could break your global value that you declare in another file somewhere.

6

u/TNorthover Jul 26 '13

If there's only one scope, you can probably call it lexical, dynamic or whatever you want. I favour "batshit insane", personally.

1

u/tiglionabbit Jul 26 '13

I could break your global value that you declare in another file somewhere.

No. CoffeeScript compiles each file in isolation.

Lexical scope IS shadowing

I prefer not to conflate those two ideas. Besides, CoffeeScript allows shadowing if you do it in function arguments (or using do). They just discourage the practice, because a program is simpler to understand if you're not shadowing names all the time.

0

u/a_marklar Jul 26 '13

This problem is limited to a single file since each file compiles into an immediately invoked function. From what I've seen its not really a problem in practice.

5

u/iopq Jul 26 '13

Yeah, it's not a problem in practice because people bind their "vars" to arguments in do so they have a closure in a closure

which is a silly thing to do instead of just allowing you to declare vars. Is typing var so hard?

→ More replies (1)
→ More replies (13)

5

u/doomhz Jul 26 '13

That's nasty. I'm using CoffeScript for almost 3 years already and I didn't notice this issue as I never rely on global vars and my code is mostly stuffed in classes. Instead of global vars I'm using @myVar as a class attribute. I hope it makes sense...

25

u/AreaOfEffect Jul 25 '13

IMO, CoffeeScript has a limited life span. ECMAScript 6 has a vastly improved syntax that rivals python.

4

u/redditthinks Jul 26 '13

There is certainly some ambiguity and quirks in CoffeeScript that they need to take care of in the long run. Implicit parentheses is one such thing.

-10

u/runvnc Jul 26 '13

I am amazed that anyone can compare the syntax of CoffeeScript, ECMAScript 6, and Python, and not see that CoffeeScript is clearly superior.

1

u/Arelius Jul 26 '13

And I'm amazed that anyone can compare the syntax of CoffeeScript, ECMAScript, and Python. and not see that CoffeeScript is clearly the worst, most error-prone, unreadable of the three.

While I understand it's due to prioritizing write-ability over everything else. It seems that everything unique in CoffeeScript is built specifically to be as error-prone, and arcane as possible.

→ More replies (2)

12

u/dons Jul 25 '13

Fucking variable bindings, how do they work?

1

u/ueberbobo Jul 26 '13

You can't explain that.

2

u/[deleted] Jul 26 '13

This is why in Python, assignment is a declaration of a local variable by default, you have to mark it as "global" (or "nonlocal" in Python 3), if you want to refer to a global.

5

u/smog_alado Jul 26 '13

I actually kind of hate the python scoping. Since the lexical scoping is so limited ("nonlocal" is a verbose hack) everyone ends up using big classes and explicit self dictionaries instead. Automatic variable declaration also means that typos get turned into new variables instead of errors.

3

u/mcguire Jul 26 '13

There is a reason why most languages do it the other way, with a keyword to introduce a new variable.

1

u/[deleted] Jul 28 '13

Yeah, it's a bit of a wart in the language, but a smaller one than JavaScript's assignment of a new variable that defaults to global scope.

5

u/PaintItPurple Jul 25 '13

In most languages with closures (including JavaScript) the inner y and the outer y could and would be separate.

This is not really true. The whole point of a closure is that it closes over the surrounding scope. For example, in Ruby:

y = 0
test = lambda do |x| 
  y = 10 
  x + y
end
puts test y
puts test y
puts test y

This will print "10", "20" and "20". Exactly like the equivalent CoffeeScript. If you couldn't access the surrounding state, it wouldn't be a closure. In fact, this property of closures is used all the time in JavaScript (for example, to allow for "private" object members). It is not specific to CoffeeScript at all.

The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.

3

u/MatmaRex Jul 26 '13

Ruby's lambda does capture scopes, but that's intended (all blocks do).

Ruby's def (which is what you use to define a function) doesn't, non-global variables from outer scope are inaccessible within it.

0

u/PaintItPurple Jul 26 '13

Ruby's def does not create a closure, which is what we're talking about here. See the quote I was discussing.

2

u/MatmaRex Jul 26 '13

Yes, I realize, I was just clarifying – after reading your post I though that it makes it seem like Ruby has the same issue as CoffeeScript here and that is luckily not true :)

1

u/PaintItPurple Jul 26 '13 edited Jul 26 '13

Well, Ruby does behave exactly the same way — it just doesn't come up as often because of how closures are used in the two languages. To some degree, this can actually lead to more surprising behavior in Ruby. For example, if you define a method with define_method, it works like CoffeeScript, but not if you use def.

2

u/didroe Jul 26 '13

The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.

I think it's both things really. Python, for instance, is much saner than Coffeescript yet doesn't distinguish between defining and setting a variable. It gets away with it because variables are function scoped (like Javascript). This creates other issues though such as not being able to write to outer-scoped variables without an additional construct. Distinguishing variable declarations from setting existing variables seems like the cleanest way to do it to me, and it supports arbitrary scoping. I also think it conveys the intent of the code better and allows for better error checking.

3

u/Newtonip Jul 26 '13

Madness?

THIS IS COFFEESCRIIIIIIIIIPT!!!!

7

u/jashkenas Jul 25 '13 edited Jul 25 '13

Despite the amount of spittle and feigned outrage that this feature generates among people who have never given the language an honest try, it's nice to hear a bit in this thread from the folks who have written tens of thousands of lines of CoffeeScript using this style of variable declaration, and have never had a problem. In short -- suspend disbelief for a moment, try something new, and you might like it...

But in long -- CoffeeScript's style of scoping is a deliberate change and (I'd argue) simplification from the usual dichotomy between variable declaration, initialization, and use. Let's reason about it a bit from first principles:

When you write a program, and use a variable to name a value at a certain point in the code, that variable should be a clear identifier for the value. In languages with first-class functions, this gets a little more involved, as closures and lexical scope means that any function defined within the scope where the variable is present is able to access it by name. This is what a closure is, and does -- allows access by the same name to variables in every lexical scope "above".

CoffeeScript tries to stop there. You have variables as names, and they're always expressions -- there's no statement required to declare them -- and they're always local and lexical to the file, and to the smallest scope in that file where they're used. If you want to make a global variable, you must explicitly make it global by attaching it to window, in the browser. The complexity of declaration-vs-initialization is gone.

And (the critical bit) the complexity of shadowing-by-default is gone. A chunk of nested code, where an inner variable shadows an outer variable of the same name, lexically -- is always a bit of a wart on a perhaps otherwise well-structured codebase. When you shadow, you're making it impossible to access the closed-over variable from anywhere further down in the lexical scope, and you're creating a region of the code where it's more difficult for the reader to know the identity of the shadowed variable. Is x the thing it was a moment ago? Oh wait, it's a new value now in this inner part, and as soon as this function ends, on the next line x will again stand for that other thing. If you choose a better name for the variable, instead of shadowing -- which is something that's always possible to do in a lexically-scoped language, your code will be easier to read.

So, CoffeeScript's variables are lexical-to-the-file, scoped as locally as you use them, and ideally, feel more transparent as identifiers, because when you see x, you know it's the x in the current lexical scope, and not an x with multiple identities.

That's the rationale.

6

u/kamatsu Jul 25 '13

You know you can have expressions that declare variables too, right? Look at let expressions.

1

u/rwbarton Jul 26 '13

do seems to be essentially a let expression.

5

u/kamatsu Jul 26 '13

nah, that's a hack. Do is a construct that immediately invokes the passed function, forwarding any arguments, and proper scoped bindings can be simulated here by passing some default values for those arguments.

→ More replies (5)

8

u/rwbarton Jul 26 '13

I'm sure you are aware of the bug in your own compiler introduced by this very issue, mentioned here.

Your motivations for eliminating shadowing are very reasonable. The problem is that you cannot even detect whether the programmer intended to shadow a variable, since there are no mandatory variable declarations. And it's easy to "accidentally introduce intentional shadowing" by adding a function or global variable with the same name as some local variable in the same file, as in the linked bug.

CoffeeScript "forbids shadowing", but in a very strange way: it does not (cannot) reject the program, but instead silently changes the meaning to something entirely different from what the programmer intended. Add mandatory variable declarations to CoffeeScript, and suddenly you can detect shadowing (as well as many other common errors). Then if you want to reject programs that shadow, you'll hear no complaints from me.

2

u/jashkenas Jul 26 '13

I'm certainly aware. Like everything else, eliminating shadowing in this particular way is a tradeoff. When you're introducing new variables at the top level of the file -- something that's pretty rare, that the patch did -- you certainly have the possibility for conflicts. But shadowing also has a possibility for conflicts -- by var'ing a variable in an intermediate scope, you can break an inner scope's access to an outer variable. I've had that problem before in JavaScript.

Incoming pull requests are often slightly broken in various ways, and need to be tweaked after merging. If you think of the tradeoff of changing that variable name, versus having to go through and add var or a special assignment operator to every introduction of a variable in the entire codebase -- it's a choice I'd always be happy to repeat.

Keeping variable names descriptive and singular within a lexical scope, and removing the need to even think about "has this variable been declared in the right place", is a huge boon, in my opinion.

2

u/mcguire Jul 26 '13

On a more serious note, if you wanted to eliminate shadowing, wouldn't a better strategy be to have the compiler complain if the programmer tries to shadow a variable?

2

u/rwbarton Jul 26 '13

I'm certainly aware. Like everything else, eliminating shadowing in this particular way is a tradeoff. When you're introducing new variables at the top level of the file -- something that's pretty rare, that the patch did -- you certainly have the possibility for conflicts. But shadowing also has a possibility for conflicts -- by var'ing a variable in an intermediate scope, you can break an inner scope's access to an outer variable.

The difference is that a compiler can detect the latter, so the cost is that you spend 1 second compiling your program, get an error and rename the conflicting variable.

When you reuse a name in CoffeeScript, the cost is a bug that can go undetected for a long time.

You haven't gained this:

Keeping variable names descriptive and singular within a lexical scope, and removing the need to even think about "has this variable been declared in the right place", is a huge boon, in my opinion.

because you can get it in a language with explicit variable declarations too! All you've gained is not typing "var", at the cost of obscure bugs.

Implicit variable declarations are a choice I hope no language designer ever makes again.

1

u/xardox Jul 27 '13

Dude. Bug in the compiler. Don't lecture us on how we should be perfect just like you are, unless you are actually perfect.

8

u/[deleted] Jul 26 '13

This pure utter bullshit.

[...] people who have never given the language an honest try [...]

Guess why people just stay away from it in the first place?

from the folks who have written tens of thousands of lines of CoffeeScript using this style of variable declaration, and have never had a problem

Everyone of those who claims this just hasn't found his bugs yet. The nature of this language bug is that you never can't be sure that you haven't made a mistake by design.

2

u/mcguire Jul 26 '13

it's nice to hear a bit in this thread from the folks who have written tens of thousands of lines of CoffeeScript using this style of variable declaration, and have never had a problem.

Psst! Hey, buddy! Wanna try some COBOL?

1

u/lucian1900 Jul 28 '13

Shadowing is a really useful thing and is a way to reduce the problems. It really should shadow.

2

u/agumonkey Jul 25 '13

Isn't it only more painfull because it's the only big problem of coffeescript ? like a 79/21 ..

2

u/howsyourweird Jul 26 '13

Comment #1 on this page and the preceding thread pointed out this shadowing 2+ years ago.

One paragraph in the CoffeeScript doc completely turned me off. I wonder how you deal with it? The paragraph is “Because you don’t have direct access to the var keyword, it’s impossible to shadow an outer variable on purpose, you may only refer to it. So be careful that you’re not reusing the name of an external variable accidentally, if you’re writing a deeply nested function.” What? Be careful? That is what scoping is for so I don’t have to know every name used outside my function. I saw in some comments on the developer’s site that he thought this is like Python. But he’s clearly wrong about that.

2

u/scandium Jul 25 '13

As soon as you use a variable named x for example in an outer scope, x becomes global across all the inner scopes.

:o

2

u/notorious1212 Jul 25 '13

I guess it's important to keep reiterating this argument. However, I don't really see a value in the 3000th complaint blog about this. I don't really think this article is constructive in any way.

I haven't been programming for forever, but I don't honestly think that shadowing is as bad as everyone has tried to make it out to be. It can be extremely uncomfortable, and may cause you to have to include extra steps, but I certainly do not think this ruins coffeescript. I guess it depends on programming style, and the problem only grows about as big as you let it. I haven't written more than a thousand lines of coffeescript, however I would like to happily note that coffeescript hasn't yet kidnapped my dogs and held them for ransom. Everything's going ok.

13

u/Eirenarch Jul 25 '13

This article helped me move CoffeeScript in my list of "WTF?!" languages. It was quite useful to me as I may have considered using it hadn't I read this.

→ More replies (18)

1

u/gargantuan Jul 28 '13

Fuck it! Dart it is then!

1

u/Denommus Jul 26 '13

Well, lots of people who don't know how scoping works.

This is lexical scope, and it's useful as hell to create closures, which are ESSENTIAL to a functional-style programming.

If what you want is to SHADOW a variable from a previous scope, you can do that with Coffeescript's "do".

Coffeescript is not my cup of cake, I hardly ever program with it, but this behavior is predictable. I would have the same behavior in any language that has lexical scope and mutation, that's why things like "let" or "do" exist.

For instance, in Common Lisp you wouldn't do this:

(let ((y 0))
  (defun test (x)
    (setf y 10)
    (+ x y))
  (prin1 (test 5))
  <more code that uses "y">)

That would be insane. You'd simply:

(let ((y 0))
 (defun test (x)
    (let ((y 10))
      (+ x y)))
  (prin1 (test 5))
  <more code that uses "y">)

For the same reason, in Coffeescript you could:

y = 0
test = (x) ->
  do (y = 10) ->
    x + y;
alert test 5

If you want a local binding, "do" is there.

11

u/rwbarton Jul 26 '13

The difference is that in Lisp I can look at a function and a use of a variable in that function and know whether it is local to the function or defined in a wider scope (perhaps globally). Your first test is obviously using an outer y, since there is no (let ((y ...)) ...). Every variable has to be declared somewhere.

In CoffeeScript if I have a function

test = (x) ->
  y = 10
  x + y

I have no way to know whether y is local or global! It depends on whether there is a global variable y. But either way, test is valid.

It's quite true that you can use do to introduce a local variable. But the default is this crazy context-dependent behavior. do should be the only way to introduce a variable, then you would have the same sane scoping as in Lisp.

0

u/Denommus Jul 26 '13 edited Jul 26 '13

That's a common problem on languages that don't make a difference between assigment and binding. But you can always use a subset of the language that makes sense.

Quite in fact, Javascript has a similar problem, but you solve it in a weirder way (with self-calling functions).

1

u/didroe Jul 26 '13

Javascript has a similar problem, but you solve it in a weirder way (with self-calling functions).

Javascript has function scope variables (var) so the problem is limited to a function, not the entire scope chain. And "let" is coming in ES6 to give us block scope. And guess what "do" does, it takes a function and calls it in that weird way you're talking about.

1

u/xardox Jul 27 '13

Sure you can use a subset of the language, but THE COMPILER DOES NOT CHECK THAT YOU ARE USING A SUBSET OF THE LANGUAGE. Your solution is fine for perfect programmers who write small programs and never make mistakes. But the whole point of using a compiled language is so that the compiler can check for as many possible mistakes as possible. If your solution to a problem with Coffeescript is to introduce a problem that Coffeescript was designed to solve (that the programmer must be perfect and the compiler ignores possible errors), then Coffeescript has failed at its design goal, and you're just making excuses for bad design, which is stupid.

1

u/Denommus Jul 27 '13

Sure you can use a subset of the language, but THE COMPILER DOES NOT CHECK THAT YOU ARE USING A SUBSET OF THE LANGUAGE.

So? Every language has its own idiosyncrasies that are avoidable, compiled or not. Knowing what can go wrong is important for every developer, ever. And that includes CoffeeScript programmers.

The scope problem has a solution. That's it, why are we still fighting?

1

u/xardox Jul 31 '13

From the preface of the Unix-Haters Handbook:

“I liken starting one’s computing career with Unix, say as an undergraduate, to being born in East Africa. It is intolerably hot, your body is covered with lice and flies, you are malnourished and you suffer from numerous curable diseases. But, as far as young East Africans can tell, this is simply the natural condition and they live within it. By the time they find out differently, it is too late. They already think that the writing of shell scripts is a natural act.” — Ken Pier, Xerox PARC

1

u/Denommus Jul 31 '13

There is an expression in Portuguese that I don't think it makes sense in English, which says:

O que tem a ver o cu com as calças?

Translating literally, it means "what does the asshole has to do with the pants?", which is a way of saying that what you're saying is completely out of scope.

If you can tell me a language that you can program without worrying about a subset, I may agree. But every language has problems. Even haskell, common lisp or scheme, which are well regarded.

→ More replies (3)

0

u/LukeGT Jul 26 '13

This is only a problem if you have such a monolithic file that you can't keep track of what variables have been declared, and if you're using global-ish variables, both of which are generally bad ideas. I say it's not a problem with Coffeescript, it's a problem with you. Shadowing is a bad idea and I'm glad Coffeescript prevents it.

In any case, I daresay JavaScript developers make more mistakes from missing var's than Coffeescript developers do from this situation.

1

u/Shanebdavis Jul 26 '13

This problem is mostly nullified if you don't compile --bare Coffeescript. Without bare you cannot accidentally create or clobber globals. This also means that all variables you could possibly be accessing are in the same file. This helps with visibility. If you suspect you have a shared variable, you only have to look as far as the file in which that variable is used.

Though options like adding "local" or ":=" might seem nice, I'm not sure they help. The main problem is accidental shadowing (or not shadowing in this case). You have to know to use local or := in the first place, so that doesn't solve the problem. If you know you need to use local or :=, you could just use a new variable name instead to the same effect.

2

u/didroe Jul 26 '13

You have to know to use local or := in the first place

I think the point is you would always use it when you wanted to declare a new variable, not only when you know it shadows something.

1

u/Shanebdavis Aug 01 '13

I agree that was probably the original intent. However, IMO, variable declaration doesn't fit the spirit of dynamically typed languages where brevity is typically valued over explicit declaration.

It does makes sense in a statically typed language, though . If variable declaration is desired, then languages like Dart (http://www.dartlang.org/) make more sense than CoffeeScript.

1

u/drunken_thor Jul 25 '13

This is not a complaint for me, does no one else have a problem with lack of private methods and how easily those could be included in the language?

5

u/[deleted] Jul 25 '13

How could private methods be easily added? Keep in mind that Coffeescript is meant to just be a thin syntax layer over ordinary Javascript. One of the goals is that Javascript code should be able to seamlessly use classes written in Coffeescript.

1

u/[deleted] Jul 26 '13

How could private methods be easily added?

Similar to python for example. It has no private methods per se, it just mangles names of the methods.

1

u/[deleted] Jul 26 '13

Yeah.. You can put underscores in front of your names in Coffeescript too. (did I just blow someone's mind?)

1

u/[deleted] Jul 26 '13

And? In coffeescript a.__method will not be changed to something like a.__A__method like in python, which is the entire point of putting underscores in the front of method names in python

3

u/ms_moutarde Jul 26 '13 edited Jul 26 '13

How would the coffeescript compiler distinguish between a javascript function named "foo.__bar" and a coffeescript function named "foo.__bar"?

Under your proposed plan the call to the javascript function would be name-mangeled to be "foo.__Foo__bar" which would result in a runtime error.

1

u/drunken_thor Jul 26 '13

so I have the coffeescript code here

class MyClass
  test: ()->
    @._private_method()
  _private_method: () ->
    console.log "this is a private method"    

Right now this compiles to this javascript:

var MyClass;

MyClass = (function() {
  function MyClass() {}

  MyClass.prototype.test = function() {
    return this._private_method();
  };

  MyClass.prototype._private_method = function() {
    return console.log("this is a private method");
  };

  return MyClass;

})();

with very simple changes to the compiler it could be changed to this:

var MyClass;

MyClass = (function() {
  function MyClass() {}

  MyClass.prototype.test = function() {
    return _private_method();
  };

  _private_method = function() {
    return console.log("this is a private method");
  };

  return MyClass;

})();

This makes it so that it scoped within the outter closure so that it is not in the global scope and only the class methods of MyClass can access the private method. Very simple addition to coffeescript yet the author of cs refuses to even consider it.

2

u/[deleted] Jul 26 '13

You can write this in Coffeescript:

class MyClass
  test: ()->
    _private_method()
  _private_method = () ->
    console.log "this is a private method"

(note the subtle difference between "_private_method:" and "_private_method ="). Then the compiler will generate:

// Generated by CoffeeScript 1.5.0
var MyClass;

MyClass = (function() {
  var _private_method;

  function MyClass() {}

  MyClass.prototype.test = function() {
    return _private_method();
  };

  _private_method = function() {
    return console.log("this is a private method");
  };

  return MyClass;

})();

1

u/drunken_thor Jul 26 '13

Ah! So it does, I just tried it out and it compiled just fine. Why have I not seen this specifie before because that would be a perfect definition of a private method then rather than all these other suggestions I am getting.

Thanks for taking the time to teach me something!

3

u/virtyx Jul 25 '13

Private functions are generally not implemented in dynamic languages. Usually if you prefix a function name with an underscore or two, it's enough to signal to other developers not to mess with it.

E.g. __initializer = -> secretStuff()

That same pattern is used in Python. I think since the languages are not compiled there's really no practical way to simply disallow calling a specific method. It might over-complicate the interpreter implementation for little gain or etc. I'm not aware of the specific technical reasons why dynamic languages tend to not have private access levels.

4

u/munificent Jul 26 '13

Private functions are generally not implemented in dynamic languages.

Dart has them. Privacy is a module-level concept, not class-level, and I find it works pretty well. I find myself missing protected sometimes, but that's much harder to do in a dynamic language. I think Dart's solution here is a nice compromise between simplicity and control.

1

u/smog_alado Jul 26 '13

Privacy is a module-level concept, not class-level

This is a big difference though. Module level privacy is already kind of possible in JS (with local varables inside an IIFE) but class based (like he was trying to say) is really though because JS objects only have public keys and the instance variables and methods are all mixed together.

2

u/munificent Jul 26 '13

Module level privacy is already kind of possible in JS (with local varables inside an IIFE)

That's a bit different. That gives you module-private variables but not module-private methods. Dart has both. JS won't have the latter until private names (or is it "symbols" now?) are in, and those are a good bit more cumbersome to work with.

In Dart, you can define a class like:

class Foo {
  _somePrivateMethod() { ... }
}

Code in that library can call _somePrivateMethod() when they have a Foo. Code outside of the library cannot, even if it gets an instance of Foo.

1

u/smog_alado Jul 26 '13 edited Jul 26 '13

In JS you can always stick a function in your private variables. Of course, you need to use a different syntax so its very annoying if something get converted from public to private or vice versa

self.foo(x)

vs

foo(self, x)
foo.call(this, x)

Its also quite hard to have protected methods or variables (dunno if Dart has those but protected shouldn't be a problem as long as you keep your class hierarchies separate from raw JS)

That said, I'm pretty sure you already know this. I was just pointing out that lots of people tend to mix classes/objects and modules into a single concept even though they are actually very orthogonal.

1

u/munificent Jul 26 '13

In JS you can always stick a function in your private variables.

Yup, but you lose dynamic dispatch in the process. For example:

abstract class Tool {
  void _click();
}

class Paintbrush extends Tool {
  void _click() { ... }
}

class Eraser extends Tool {
  void _click() { ... }
}

Here we've got a couple of derived classes that override a private method. In Dart, within the library where these are defined, you can call _click() and it will dynamically dispatch to the right method if you have a Paintbrush or an Eraser.

2

u/smog_alado Jul 26 '13

Thats what I was talking about when I mentioned the problem of protected methods. This is OK for Dart but its though for Coffeescript because in that case you want to be able to have vanilla JS classes inheriting from your CS class and then its tricky to have a property be visible by the child class without also making it fully public.

1

u/redalastor Jul 26 '13

D also has its privacy at the module level too.

2

u/tiglionabbit Jul 26 '13

I think the coffeescript developers don't find private methods to be important enough to add. I know I don't want them.

-2

u/bart2019 Jul 25 '13

CoffeeScript on the other hand offers you no method to scope a variable. Scoping is entirely automatic, and the lowest level use of a variable name is the single instance of it.

In other words: every variable in Coffeescript (apart from function parameters) is a global.

3

u/donatj Jul 25 '13

Well you have two functions in the same scope, they can both have their own x, but if you define x above the functions, thay're magically both using that x instead.

5

u/[deleted] Jul 25 '13 edited Jul 26 '13

[removed] — view removed comment

1

u/donatj Jul 25 '13

Which is very similar to it being global. Its not global until you try to benefit from the fact that its not global.

2

u/[deleted] Jul 25 '13

[removed] — view removed comment

5

u/donatj Jul 25 '13

"if a name has been used as a global or in an outer scope, then references to that name will always refer to the global/outer variable"

so if I use a variable globally I'm already using somewhere else, it magically becomes global. Am I not correct? Which is almost the exact same behavior as it having been global to begin with.

I can use a variable globally I initially defined in a function. It was scoped, but when I use it outside its not. Might as well have not been scoped to begin with, because it gets none of the benefits.

1

u/rwbarton Jul 26 '13

Might as well have not been scoped to begin with, because it gets none of the benefits.

Not exactly, you can have two functions f and g which both have a local variable named x, and if f calls g, g won't clobber f's x. If x was truly global, it would get clobbered.

1

u/seruus Jul 26 '13

All variables are scoped as usual, the only difference is that there is no shadowing, so if you refer to a variable with the same name as a global variable inside a function, you will access the global one, not shadow it.

1

u/tiglionabbit Jul 26 '13

It's function scoped, not block scoped.

-3

u/Y_Less Jul 25 '13

While I agree that the scoping rules are odd, the problem can be vastly reduced with a consistent naming scheme. For example all variables start with a lower case letter, globals are prefixed with "g_", functions start with an upper case letter etc. Not saying you should use that one specifically, just that you should use one so you know the type and scope of the variable just from the name.

20

u/donatj Jul 25 '13

This is essentially what you do in languages with no scoping like BASIC though. Its a workaround at best.

6

u/[deleted] Jul 25 '13

and here I was about to take a second look at CoffeScript...

6

u/cashto Jul 25 '13

That works well for variables, but functions at the top level have the same problem too:

I wrote a helper function somewhere above:

square = (x) -> x * x

You three month ago write your function 1000 lines below:

createWidget = ->
    square = new Square 10
    square.onResize = (e) -> console.log e
    square

5

u/Y_Less Jul 25 '13

I did actually explicitly mention functions. However, I agree that this is just a workaround and FAR from a stable solution.

5

u/cashto Jul 25 '13

Ah, sorry, didn't see the recommendation that functions should be uppercased.

Note that that conflicts with the JS practice (enforced by jslint, even) of only using uppercase-named functions for class constructors.

→ More replies (15)

-4

u/[deleted] Jul 25 '13

It still beats writing JS! If Coco picks up steam, I would switch over for future projects.

4

u/ikilledkojack Jul 25 '13

What's the problem with writing vanilla Javascript? Perhaps I don't see it as an issue is the reason why I see no benefit to learning Coffeescript.

5

u/yogthos Jul 25 '13

I don't know how many of these apply to Coffeescript, but there are certainly things that can be improved on JavaScript.

Here's a few reasons I use ClojureScript:

  • proper namespacing support
  • libraries
  • sane variable hoisting
  • arbitrary keys in maps
  • persistent data structures
  • comparison by value, including complex data structures, directly facilitated by the above
  • much simpler rules and syntax
  • multiple arity functions
  • named parameters
  • uniform iteration mechanics for all native types
  • proper lexical scope for clousres
  • lazy sequences
  • macros

2

u/[deleted] Jul 25 '13
  1. JavaScript does have a bunch of technical issues and CoffeeScript insulates you from most. Here's some of it.
  2. Too many different interpreters. Increasingly a historical argument, thankfully, but cross-browser compatibility has been a pain. Again, CoffeeScript insulates you, at least to a degree.
  3. It just doesn't feel comfortable. I've seen too many cases of semantic trickery and nested function(){}s that render code ineligible. I feel better writing Pythonesque languages. That's not a technical argument, just personal preference. Just like I use HAML and such, I'd easily use a translator from a style I like to JS, even if it did nothing else.

2

u/cashto Jul 25 '13 edited Jul 25 '13

What are y'all doing in here?

We're writing CoffeeScript. And you don't want no part of this shit!

You know what, I don't want to be tied to no browser, I can't write no browser-specific code.

IT'S NOT PLATFORM SPECIFIC!

I'll have to write some complicated syntax?

IT'S NOT VERBOSE!

Okay, well, I don't want to be unable to do anything I can do in JS ...

IT'S NOT A SUBSET OF JAVASCRIPT!

It's not going to make me want to write JavaScript, will it?

IT MAKES JAVASCRIPT EVEN BETTER!

Sounds kind of hard to learn?

IT'S THE EASIEST LANGUAGE THERE IS!

Hmm.

You don't want it! Get out of here Dewey!

-1

u/[deleted] Jul 26 '13

"Look! When I define a variable in an outer scope, for some reason it gets defined in the outer scope! Madness!"

4

u/donatj Jul 26 '13

It redefines your inner scope variables is my point.

-5

u/[deleted] Jul 26 '13 edited Jul 26 '13

I have become quite attracted to CoffeScript lately and I can see how this blog post could discourage some to avoid it, but actually it just made me like it more..!

Seeing as this is a deliberate design choice from the creator, there must be a reason why it's made this way and why so many people use it.. So one little blog post isn't going to scare me that easily..

The more I think about it and read the comments in this thread, the more it makes me understand what makes CS so special.. how it encourages simple and beautiful code by thinking about how you name things.. and not just be some punch card like stream of verbose mechanical instructions..

An easy and natural way of avoiding accidental shadowing is to group and structure all your code and variables into objects and classes; make your program modular.. that way the global scope won't be cluttered with all kinds of confusing names.. you would only have a limited number of modules who can access each other through each respective namespace.. clear and simple..

Every good developer knows that each tool/language serves different use cases and different people.. hopefully this thread will help you decide better whether CS is something for you.. if not, there are plenty of other languages to go around.. :)

trust your technolust.. :D

EDIT: I guess what I'm trying to say is that I understand how this scoping would fit well with CoffeeScript's personality, but I also understand why the blogger consideres this dangerous behavior, because it really is.. But that's CoffeeScript for you.. take it or leave it..

3

u/mcguire Jul 26 '13

Seeing as this is a deliberate design choice from the creator, there must be a reason why it's made this way and why so many people use it.. So one little blog post isn't going to scare me that easily..

Psst! Hey, buddy! Wanna try some Perl?

1

u/[deleted] Jul 26 '13 edited Jul 26 '13

Does it compile to JavaScript..? :p

...okay, that was kinda lame.. :/

9

u/[deleted] Jul 26 '13

there must be a reason why it's made this way

Yes, complete lack of knowledge of some of the fundamental basics of language design, known since before ALGOL-60.

0

u/[deleted] Jul 26 '13 edited Jul 26 '13

You could just push all your global variables to the bottom of your script.. variable names become declared in the order they first appear in the script..

EDIT: not that I would generally recommend that.. just a suggestion to those who like to pollute their global scope with all sorts of junk..

-8

u/[deleted] Jul 25 '13

The worst offense of CoffeeScript is the syntax. I know they were going for concise, but replacing words with hieroglyphics is never the answer. Didn't everyone reject Perl for that reason? Replace function with def or sub and you're concise. Replace it with -> and you've gone right past concise to obtuse.

5

u/wyaeld Jul 25 '13

-> is coming to JS.

No-one who actually uses it considers it difficult.

0

u/[deleted] Jul 25 '13

I wrote Perl as my primary language for a good 4-5 years, so no I don't have a problem with it. I just don't understand why we would make a conscious decision to move toward it in 2013. I have the same problem with Scala. It's practically the Holy Grail of features, but it reads like a mess of punctuation marks.

→ More replies (3)

-8

u/dmitry_sychov Jul 25 '13

But millions of web monkeys who have hard time separating assignment and declaration feel comfortable with CoffeeScript.

4

u/adotout Jul 25 '13

Right, the only real programmers are the ones that code in [insert your language here]