As a learning exercise, I converted a piece of Javascript code i’d written into CoffeeScript.
So what’s wrong with this?
1
2
3
4
5
6
7
8
9
|
initialize = ->
$('select[name="language"]').change (e) ->
renderLocalizedText $(@).val()
return
# Initialize the application on DOM ready event.
$(document).on 'ready', ->
console.log "hello, world"
initialize
|
Oh that’s right, initialize doesn’t get called.
It must be “initialize()” when you want to call a function w/no params. But when you do have params, you can leave the parens off. What inconsistent bullshit is this? I guess it’s nice that “initialize” by itself is a statement w/o a side effect. But then when you run it through the compiler (when the hanging return isn’t there), it spits out “return initialize;”, which isn’t what I meant at all and could be a side effect for anyone trying to maintain this down the road. Better to make it explicit that this thing won’t return anything a caller can (ab)use, until and unless I choose to make that the case.
So I’ve been adding hanging returns so the compiled code returns nothing as often as it should.
Sigh, I don’t know that I dig the whole implicit “return whatever was on the last line” thing that so many of these terse scripting languages have. And it strikes me as a weird idea to try and save on parens.
So the CoffeeScript adventure begins, but I’ll be damned if I’m not already building the defensive programming idioms in.