Why CoffeeScript Isn't the Answer
== compiles to the strict equality operator
===, making it impossible to use the dangerous loose equality operator.
Now, if the event function needs to be expanded, one would logically think it could be changed like this.
However, that doesn’t compile. You have to drop the comma or move it onto the next line, before
How about something like
func1 1, func2 2, 3? Does 3 get passed to the first or second function? I still frequently forget one place where parenthesis are not optional, leading to unexpected behaviour. The statement
x = f -> f looks like it should assign x to the identity function, but it doesn’t. Instead, x is assigned the result of calling f with a function of no arguments returning f. Or how about this,
a + b adds a and b while
a +b calls a with the argument
+b. There are lots of other examples of ambiguous and confusing syntax in CoffeeScript. For more examples, check out My Take on CoffeeScript by Ruoyu Sun and this Gist from Tom Dale.
Readable, Think Again
Ambiguous code is only one part of what makes CoffeeScript difficult to read. Everything is an expression (returns a value) and it has lots of control flow and operator aliases. All of which encourages very English sentence like code. However, that often makes the code less readable, not more. The human mind is good at understanding logic in symbols; English is not good at expression logic. As an example, consider the line
eat food for food in foods when food isnt 'chocolate' from the CoffeeScript tutorial. The declaration of what food is occurs in the middle of the line and doesn’t even look like a variable declaration. Furthermore, until you finish reading the line it isn’t clear which foods will be eaten. That code could easily be worse if
unless eat is undefined was added to the end, making the whole line conditional. One wouldn’t realize it was conditional until reading the end. Imagine if the expression before the
for had been a complex multi-line method call with logic in it. Ryan Florence digs deeper into these issues in his post A Case Against Using CoffeeScript. Suffice it to say, many of the features added to CoffeeScript with the intent of making it “readable” actually have the opposite effect.
To see how this would happen, imagine you are in a hurry to implement the next feature. Unbeknownst to you, the following code is near the bottom of the file you are about to modify.
Now, near the top of a code file you add these lines. So that the file is now.
Did you catch the error? Originally,
lastValue was local to the
innocent function, but now it is global to the file and the
innocent function actually modifies it. We now have a bug waiting to happen when someone calls
innocent then checks the value of
lastValue. Keep in mind there could be multiple screens of code between these two code segments.
Not Far Enough
typeof to something that was more useful, for example the
typeOf() function recommend by Douglas Crockford.
Classes Are an Illusion
new keyword or to make everything a factory function.
CoffeeScript has a
this keyword. When creating a class you can’t help but feel that
this should refer to the class everywhere within the body of the class. That is what it would mean for something to be a class. But the actual semantics of
this are unchanged. For example, if you declare a class with methods using
Car is a class, one expects