While I love CoffeeScript, the always-implicit-return thing is my biggest pet peeve. The language really needs a non-implicit-returning arrow like -/> , if nothing else so that you can make one liners that don't return a value.
Unfortunately, this isn't the direction they want to take the project in. The reasoning is that in CS, all functions should ideally have a return value. I think that's silly, since you have to contend with the fact that it's supposed to interoperate with JS, and plenty of builtins and JS libraries don't behave as if every function has a return value, but what can you do?
Definitely the crux of the issue, as languages/ecosystems where "callables always return a value" tend not to have that issue even when dynamically typed (Ruby, Erlang). And it's not just JS interop, but gateless interop: calling JS from CS or CS from JS is supposed to be "invisible" as opposed to e.g. ClojureScript which also has excellent JS interop but where the interop is done through special forms (e.g. the `js/` namespace calls into JS-global objects), managing developer expectations about the seamlessness of the boundary.
Exactly, and the article's example is spot on for how these expectations get violated by CS's behavior. In CS, the assumption is that aside from minor (and easy to detect) performance penalties, returning a nonsensical value from a function is harmless.
But libraries like jQuery actually give meaning to "no return" in some callbacks. They cleverly let that be the most common behavior, and then let return values specify less common behavior. An example of this in jQuery that creates even more aggravating bugs is event handling. Here's a simple example: http://jsfiddle.net/63fUY/ .
The expectation is that each time you click either button, the total count will increase. But because of the implicit return on line 13, preventDefault gets called half the time. That's an incredibly easy mistake to make, a huge pain to hunt down, and awkward to write a test for. And it would be so easy to avoid if the language had a syntax for "default to no return" which we could just use on all event handlers.
> And it would be so easy to avoid if the language had a syntax for "default to no return" which we could just use on all event handlers.
Alternatively, rollback the "implicit return" but make the explicit return short and sweet yet still visible. E.g. use Smalltalk's unary `^` (which as far as I know currently exists neither in JS nor in CS) so the callbacks would be:
You can always look at something like LiveScript, which has the syntax !-> which prevents default return from functions, I started using it over CoffeeScript lately.
That's not even valid JavaScript, so I would not expect that. It is also somewhat standard that lambda-like operators eat as many tokens as possible.
But I agree with many points. The anonymous function sugar is extremely handy in callback-oriented programming. I wonder if a small syntax extension to JS, consisting of Python like indetation and Ruby like blocks would ease the biggest pains:
function f(some_param, callback)
bla()
return 1
f(10) |u,t| ->
callback_code_here
The call to f would receive the anonymous callback as its last argument. Callbacks in other positions, or multiple callbacks could be named:
I agree, and I don't want to simplify CS, but rather JS. The thing that bothers me about CoffeeScript is the call syntax without parenthesis but with commas - which seems to me the main source of "surprises".
The main benefit of it however is allowing you to write anonymous function arguments without chasing closing delimiters.
I would like something that lies in between JS and CS, with that particular benefit but without the weird grammar.
f.asyncMap (item) ->
foo item
, (error, result) ->
# do something with result
The readability of that is terrible. I (and most people) can easily figure out how to make myself understood to the parser, that's not an issue at all.
The itch that CS scratches is severe, but I believe there is an opportunity to have something much better in terms of readability and predictability.
Sorry, that's my bad, it should be { silent: true }. I translate myself and mix CoffeeScript into this. I guess that's a proof of the fact that I cannot live without CoffeeScript. I have corrected in the post.
You don't need CoffeeScript to do something as redundant as that, since comparison operators return a boolean:
var isLargerThanSix = x > 6
Of course, CoffeeScript doesn't even offer an improvement for the level of explicitness in the example, since it removed the ternary operator. This javascript is invalid CoffeeScript:
Cryptic? Are you serious? I'm a CoffeeScript user because I find JavaScript too verbose and cluttered and I find Coffee's verbosity quite unnecessary too.
Without syntax highlighting skimming over Coffee code is awful because you can't distinguish code from variables easy enough.
This is easier to read:
a && b && !c
Than this:
a and b and not c
The form with symbols is easier to process because you can clearly distinguish its parts.
What's cryptic about ?: ? You learn it just like you learn if ... then ... else and that's it, not cryptic anymore.
Not to mention the yes/true/on thing. What a waste of reserved words.
Text-like does not mean comprehensible. If that was true, then we'd write Coffee like this:
function sum with arguments left and right means
the result is left plus right
Well, if I look at C/C++, I feel that since & means a bitwise operation and && means a boolean operation, and & in front is a reference operator, that & is "Having a meaning that is mysterious or obscure" since the symbol changes in context.
I also feel your semantic example is overly obtuse.
> I also feel your semantic example is overly obtuse.
Exactly what I think of if ... then ... else
IMHO, your plus sign is "pointlessly terse and cryptic". See what I did there? Now seriously, why func? I prefer Coffee's (arg1, arg2, ...) -> syntax for functions. What strikes me as odd is the fact that such a slim language gets verbose in such aspects.
Of course my example is overly obtuse: it's just an hyperbole!
But anyways, I suspect the if ... then ... else syntax is just a side-effect of ? being used as an existential operator in CoffeeScript.
> IMHO, your plus sign is "pointlessly terse and cryptic". See what I did there?
No, I don't see what you did there because + has one and only one meaning, and that is add. Every child can identify what + means, the same cannot be said for (a,b) ->, even to developers who have developed their entire lives (unless they have experience with coffeescript). Hence, cryptic.
> This is easier to read [...] Than this
> What's cryptic about [...] ? You learn it [...] and that's it, not cryptic anymore.
It's hilarious how you state some fact explicitly with your own words and yet completely fail to realize this fact.
Readability is not to be confused with familiarity - and you do exactly this. You say that if you learn something it becomes readable, which is not the point, and not true at all.
Think of readability as a measure of how many things you need to learn to understand something. If you introduce a symbol which meaning is completely arbitrary and needs to be learned, you make your code less readable. While Wikipedia page[1] concentrates on the readability of texts written in natural language, it suggests that the more words the text uses, the harder it is to read.
The second part of readability is text length, and so sometimes introducing a symbol to represent common concept, while making it harder to read by itself, makes the program more readable as a whole because it makes the program shorter. It's a trade-off that needs to be considered when designing a language.
The 'class' keyword in CS is a good example: it codifies a useful idiom, that in JS takes 5 lines of code, into a single word. While you need to learn it's meaning, which on the one hand makes the program less readable, on the other hand it reduces the length of certain class of programs significantly and so it makes them much more readable than JS counterparts.
The 'if ... then ... else' is a bad example. The ternary operator does not reduce the length of any program significantly, especially in the presence of the 'switch' statement. Every time you use a ternary operator you still need a test, true expression and false expression. Ternary operator introduces the infix syntax, and not one, but two symbols that you need to learn. It's bad, really bad for readability.
The code written like this:
if (alive and kicking or dead) then (something) else (something els)
because the latter is not significantly shorter and it introduces as many as 4 (!) symbols with arbitrary meaning. You may not recognize this, because you already know the meaning of all of these, but objectively the latter form is less readable.
Your statement that boolean operators are more readable as symbols than as words is simply not true, at least if we use the definition of readability from wikipedia. You are much more comfortable with these operators as symbols, you're more familiar with them and used to them, but that is something entirely different than readability. You think that it's readable, while it's not.
I wish more people actually knew what readability is. Relevant info is just a click away, yet everyone seems to have his own definition of readability, which makes any discussion highly subjective and useless in the long term.
[EDIT] Forgot to mention, this is exactly why I find discussions on Lisp or Smalltalk syntax so infuriating. People are not interested how readable the syntax actually is, that is - how many symbols it introduces and how many of them are reused in different places. They say that the new syntax is "unreadable", while it is much more readable than alternatives, just unfamiliar.
Nobody here is "introducing" the ternary operator or && for "and," they are standard punctuation in the world of programming. I agree that, in general, it misses the point of readability to say "just learn the terms first," but in this case I think one could certainly argue that the punctuation is more readable than the words, by making better use of word-vs-symbol cues to make the structure of the expression clear.
Finally, drop the idea that your view is "objective" and there aren't different opinions on readability.
I'm sorry, I shouldn't have said "objectively". But it's not my idea. I'm merely working with definitions provided by wikipedia. If you are aware of better source on this matter, please refer me to it.
EDIT: what I mean is that discussing readability is pointless if all the participants have different definitions. I agree that the wikipedia page I linked to does not provide the best possible definition because it talks about texts in natural languages and we're talking about source code. Still, using not-the-best definition is better than using many different definitions.
In spite of arguing about this 'objective' readability, it doesn't address the issue that CoffeeScript is no more readable than Javascript is, because it introduces its own plethora of quirks and crypticness. Given the choice between the two I would opt consistently for Javascript.
I can't edit my comment anymore, but if I could I would replace "objectively" with "measurably" - there are "formulas" on the wiki page that I'm sure are applicable to programming languages and program sources.
And I don't disagree with you. Coffee is less readable than JavaScript: "->" is less readable than "function", for example, and "@" is less readable than "this". It's a matter of trade-offs, these shortcuts make the language harder to read, but easier to write; the assumption here is that people will be able to deal with this level of brevity when reading.
I went and googled for a bit for articles talking about code readability, I found a few interesting discussions:
Hardly, considering its appearance in many programming languages. Although I would argue similarly that, if the ternary operator is terse and cryptic and needs to be changed to 'if ... then ... else ...'; then '->' and '=>' are equally terse and cryptic and should be kept as 'function'.
I wonder if there's a good IDE or linter to pick up on these issues while you code. It would be difficult to farm out to the compiler on every character change, but I suspect something like that is needed
I don't really understand this "bad predictability" thing. If you leave out parens, CoffeeScript always wraps to the end of the line, simple as that. Hard to visually parse at first sight in certain scenarios? Use parens, that's what they're there for. Problem solved. I'd absolutely hate it if CoffeeScript was inconsistent in how it handled these things, but it really is not.
Though that's still predictable for me, I don't have problems with parenthesizing in Coffee (I did the translation off the top of my head and I'm pretty sure it's correct).
If you have the time and inclination, you should write something up explaining this in more detail, with examples. This point is consistently brought against CoffeeScript in nearly every article about, even those for it, and it's one of the main things keeping me (and probably others) from giving it a serious look. Of course, it still wouldn't make up for the lack of a good spec and grammar...
CoffeeScript and co. ensure new developers who adopt those languages never actually be proficient in JavaScript. Its not a good thing. Perhaps its me, but I just don't get why I'd want to write in one language in order to get code in another. Why not just learn the target language?
And please don't bring up the "You write C which gets compiled to assembly or Python which ends up as C" etc. Its not the same thing. JavaScript is not Assembly and its not Machine Language. Its a very simple language and its quirks can be learned quite easily. Its not that more verbose than CoffeeScript. Don't get me wrong, I think CoffeeScript's syntax is nice, but its nice in an academic kind of way, not in as a replacement for a language that if you work and develop for the browser, you should know very well and be friends with. Its a dangerous short cut.
You may be right that the people who write CoffeeScript already know JavaScript. But as the popularity of CoffeeScript increases so will increase the number of developers who only know CoffeeScript and little to no JavaScript.
This may be a temporary problem, though, as it may also become increasingly unnecessary to know JavaScript. But it WILL be a problem.
This isn't and won't be limited to CoffeeScript though. There are still a ton of "developers" out there who know jQuery or Dojo or, God forbid, Ext.js but actually know little JavaScript. A friend of mine was recently interviewing a guy who claimed to be a JavaScript developer but didn't know that "foo['bar']" was the same as "foo.bar". No doubt the guy could glue things together in some library blindfolded but the fact remains; he didn't know JavaScript and it cost him a job.
Exactly. I would never advise someone to use CoffeeScript without first knowing JavaScript, but I think very few people would do this anyways.
And even if they tried, aside from debugging issues, I think the article shows that it's pretty hard to work with CoffeeScript if you don't understand the underlying output code.
First of all why would you want to end up debugging on one language and writing on another?
This is why I specifically mentioned 'new developers'. But I also mean developers who know basic JavaScript and jump on the CoffeeScript hype wagon because its there and its hip.
As I predicted and mentioned in my original comment - That argument does not hold. Its not the same thing. Assembly is hard. There's no way to look at it and say its a straight forward easy language (without sacrificing 100% of your social life that is). You need to understand the hardware you're writing to. You need to be intimate with it. Be on first name basis with all the registers.
JavaScript is a simple language with modern syntax. Its not assembly. Anyone who looks at JavaScript and things "Oh, Assembly" needs a change of careers or a new pair of spectacles. Yes it got quirks, like anything else, but you can learn them and write your own JavaScript.
It can always be better. But you are investing in something that will write code for you for a browser platform that you should understand fully. Python and Ruby can also be better - perhaps we should write a language that compiles to Python or Ruby so we're not stuck with it? The 'better' solution was to allow more languages to interact with the dom and possibly that will happen in the future (Dart?). But to have an intermediary language that produces code for you which you cannot fully control, to run on a platform (the browser) which you at the moment have not much choice in the matter just to avoid punching a few keys or because you don't like the language, to me, doesn't sit well. To each his own I guess.
If you know JavaScript as well as you know CoffeeScript, you're fine. Use CS and enjoy the syntactic sugar. If not, then there's a potential problem.
The problem, in other words, is not about CoffeeScript in particular. It's about people learning CoffeeScript and not learning JavaScript. We are far, far from the point where you can be a great web developer without knowing JavaScript.
I suppose I agree with this. I already knew JavaScript very well. If I find some unexpected behavior in CoffeeScript I'll look at the compiled JavaScript and quickly be able figure out what's going on.
CoffeeScript is more readable if you follow Visual Basic paren rules. Visual Basic, at least the one I used way back when, differentiates between functions and subroutines. Functions whose return values are used in an expression must use parentheses. Subroutines and functions used as subroutines omit parentheses.
Example, I often see this. In this trivial example the intent is simple. In real code this style is unnecessary cleverness.
a b c, d e
In VB, since the return value of `b` and `d` are used, parentheses must be added
While in most cases you can do that, they are not exactly equivalent. For example, you might be passing the callback to a function that attaches members to it.
But usually you see wrapping done in order to bind methods, like
f = function(data) { return obj.doSomething(data);}
>A native method for binding methods has existed for a long time and is called just that:
Well, bind is a relatively recent addition. It wasn't added to IE until IE9, and it wasn't added to Safari until March 2012. In any case, I think that
f = (data) -> obj.doSomething data
is far more readable than
f = obj.doSomething.bind obj
Edit: it's also worth mentioning that in Chrome and FireFox, at least, the latter version of f is an order of magnitude slower than the former (http://jsperf.com/native-vs-non-native-bind)
But yes, wrapping functions is usually (but not always) silly.
Right. But I have seen actual code do this plenty of times and there was no explanation in the article so I naturally assumed it was just like those cases where basics of functions are not understood.
I'm glad to see you agree. I haven't had time to evaluate CoffeeScript, and now I don't need to make it because the advantages CoffeScript offers are not issues that bother me and it introduces issues that would bother me.
Yea, but the write-up failed to mention a few of the other advantages, most notably, comprehensions. Every time I do a for, map, filter, etc. in js, I wish I was writing coffee. Having said that, if the verbosity of array ops aren't a sticking point, then I say don't bother. I'd probably be just fine with js if I'd never typed
dog.praise() for dog in dogs when dog.type is 'pet'
I have gotten into the habit of singing these, so its probably just an oddity I have. Carry on and ignore.
I wouldn't dismiss it so quickly. I dabbled with CoffeeScript for awhile, but didn't enjoy it until I used it full time for a week or two, and now I can't go back to JavaScript. The issues brought up in the article are annoying, but no more so than JavaScript's own annoyances (and in fact CoffeeScript fixes some of JavaScript's annoyances). After a couple weeks of using it you'll know what to watch out for an it will be very pleasant to use.
I really don't find it hard to read. I'm much happier reading the CS that I've written over the last year than I ever was with reading the JS I've written. Sure, you can write unreadable CS, but it's easy not to.
How can you say that a language that is hard to read helps developers to be more productive? My experience in development shows that I spend more time to (re-)read my code than to write it.
Unfortunately, this isn't the direction they want to take the project in. The reasoning is that in CS, all functions should ideally have a return value. I think that's silly, since you have to contend with the fact that it's supposed to interoperate with JS, and plenty of builtins and JS libraries don't behave as if every function has a return value, but what can you do?