38 points · by insraq · 13y ago · 69 comments
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: f(10, success, failure)
succss: |data| ->
console.log("Success!", data)
failure: |error| ->
console.log("Error: ", error) function f(some_param, callback)
bla()
return 1
f(10) |u,t| ->
callback_code_here
or f(10, success, failure)
succss: |data| ->
console.log("Success!", data)
failure: |error| ->
console.log("Error: ", error)
are not simpler than valid CoffeeScript, such as f = (someParam, callback) ->
bla()
callback 1 # presumably your intent
f 10, (u, t) -> # callback code here
or success = (data) -> console.log("Success!", data)
failure = (data) -> console.log("Error: ", error)
f 10, success, failureThe 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.
Wrapping ambiguous objects or arrays is another:
f 1, 2, {
foo: "bla"
bar: "bla"
}, (error, result) ->
# do something with result
For a series of function parameters, the leading comma is another: f.asyncMap (item) ->
foo item
, (error, result) ->
# do something with resultUnfortunately, 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.
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.
Hah. Fun one.
> 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:
->
totalClicks++
$('#total').text totalClicks
^false # explicitly return false
and ->
$('#state').text if nextState then "On" else "Off"
nextState = !nextState
# no explicit return -> undefined as in JS isLargerThanSix = if x > 6 then yes else no
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: var isLargerThanSix = x > 6 ? true : falseHe is just demonstrating how `if` is also an expression, the comparison and return could be anything.
There are better examples of "everything is an expression", like "for" loops and multi line "if then" blocks.
I certainly agree that CoffeeScript has a bad predictability - it is full of surprises.
This 100% captures my experience, especially when it comes to interstitial whitespace sensitivity a = b c d,
e: 1
f: 2
Translates to: var a;
a = b(c(d, {
e: 1,
f: 2
}));
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).> 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?
This seems to imply to me you think most coffeescripters don't know javascript?
I've always thought coffeescript was for people who _did_ know javascript, and just wanted a little neater syntax.
Why use it? For the same reason I tweak my text editor: when I spend hours a day doing something, even small efficiency wins add up.
I agree that learning only coffeescript, and not javascript is a bad idea.
edit: toned down language.
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.
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.
I bet someone has uttered nearly these exact words when debating assembly versus C/Fortran/etc.
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 a b(c, d(e)) function(data) { return doSomething(data); }
With doSomething
Named functions are just as first-class as anyonymous ones...But usually you see wrapping done in order to bind methods, like
f = function(data) { return obj.doSomething(data);}
If you replaced that with f = obj.doSomething;
you'd likely have broken code. f = obj.doSomething.bind(obj);
Wrapping functions like in the example is different and is very common among cargo cultistsWell, 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.
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.