r/programming • • Jul 25 '13

CoffeeScript's Scoping is Madness

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

315 comments sorted by

View all comments

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.

→ More replies (6)
→ More replies (5)

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.

→ More replies (2)

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

→ More replies (5)

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.

→ More replies (4)

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 ...

→ More replies (1)

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.

→ More replies (10)

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...

→ More replies (2)

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.

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