Brief Video: Rewriting JavaScript into CoffeeScript
ryanflorence.com
ryanflorence.com
When trying to port over some applications to coffeescript, I had to revisit a whole lot of my functions to add in an empty return; line after code like:
test: (m) ->
m(i) for i in this.set
Since I didn't really expect them to build up and return arrays of the results of invoking m, which at times could result in a method that would generate gigantic arrays that I would subsequently throw away, killing my performance.I probably should have expected that, sure, and if you don't have to deal with too much data it probably won't matter.
Certainly, you need to keep in mind the return value of a function while you're writing it -- if you don't want to return any value, then just "return". And it's definitely something that you need to keep more in mind while porting JS than when writing code from scratch. But given all that, would you really prefer explicit returns in CoffeeScript?
What I was trying to express was that I don't generally run into this issue in ruby because the similar looping constructs don't behave this way, and don't carry the same kind of unexpected side-effects:
def test(lst) lst.each{|i|do_stuff(i)} end
this method will return "lst" back to me, it won't create any new arrays of the do_stuff(i) results, the same goes for for/while loops, whereas: def test(lst) lst.map{|i|do_stuff(i)} end
will return the block results. The key difference being that the map method always creates an array and returns it, even if I would have a return; after, so it is consistent.The only situation where I get into the problem is with looping, and after some time using CoffeeScript, only rarely, so it may just be one of those things you have to put up with. Still crops up occasionally after refactoring though.
I would not want to get rid of implicit returns, and would far rather continue dealing with the loop problems than that. Would just love it if those weren't the only options.
I'll guess it's because speed is less of a concern when programming in Ruby.
if you don't want to return any value, then just "return".
Yes, but that leads to clutter and goes against one of the selling points of CoffeeScript.
Previous discussions on this:
I listed some of the pitfalls with implicit returns at the following CS issue: https://github.com/jashkenas/coffee-script/issues/899#issuec...
Besides that, I still get feelings of discomfort when thinking about rewriting JS to coffeescript when knowing that the compiler will just write it as JS again.
EDIT: Tried it. In this case it seems to works.
$.ajax(url, {
data: data,
contentType: contentType,
success: success,
error: error
});
... not so nice. So as a first stab, you can clean it up like so: $.ajax url, {data, contentType, success, error}
... but it's especially nice in the context of destructuring assignment, like for your Node.js imports, for example. {spawn, exec} = require 'child_process'This is probably just a familiarity thing and I would get used to it, but compared to all of the other changes in the video which I think are pretty clean that stood out to me as a bit... iffy?
for {handler, context} in @events[topic]
handler.apply context, args if(!foo) {
maybeDoSomething();
return;
}
bar();
This lets me early exit, if needed, while letting the meat of the function not suffer from unnecessary indentation.The unless statement makes the early exit seem like the default action rather than the exceptional action it (hopefully) is.
Deleted comment
"or=" is the same as "||=", and means: Assign the variable on the left to the value on the right if the variable is falsy.
"?=" means: Assign the variable on the left to the value on the right if the variable does not yet exist (ie. is either "null" or "undefined").
Two different things, both useful.
if a? then a else 0
becomes either a ?: 0 (if you like groovy)
or a // 0 (if you like perl) result = a ? 0
Or postfix (does "result" exist?) result?
Or to conditionally assign, as we've seen above: result ?= a