action true
option1: 1
option2: 2
If you really use CoffeeScript regularly, that isn't confusing. I don't usually write it that way though. I would do this: options =
a: 1
b: 2
action true, options
doSomething () -> 'hello' just isn't valid code.Usually I would write
doSomething ->
return 'hello'
or maybe doSomething -> 'hello'
If you write it the way he did, the parenthesis are confusing.For this one:
action = (token, i) ->
@tokens.splice i, 0, @generate 'CALL_END', ')', token[2]
-- first of all, you always want to indent two spaces. But the main problem with that is in CoffeeScript you do need to use parenthesis after the first call because your code will be unreadable otherwise. You just can't write it the way he did. moveTo 10,
20
10
doSomething 1,
2
3
4
doSomething 1,
2
3
4
Those are ridiculous examples. No one does that. You just write moveTo 10, 20, 10 doSomething (->
'hello'), 1
Indentation is significant in CoffeeScript, just like it is in Python. Just having a parenthesis shouldn't change that. Anyway, what I do is this: somethingDo = (ms, func) ->
doSomething func, ms
and then I can just write somethingDo 1, ->
'hello'
action(key: value, option: value, otherValue)
You don't write it like that if the function takes two objects. You would probably just write action {key: value}, {option: value}, otherValue
x = {
key1
key2
key3: value3
}
This is called destructuring assignment, and its part of the new ECMAScript. Its useful to not have to repeat key1, key2 everywhere when the variable with the value has the same name as the object property. The extra curly braces are just necessary to differentiate from the normal syntax.Sometimes yes/no or on/off are more readable than true/false. That's an advantage.
x = 1 if y != 0;
Don't use semicolons in CoffeeScript. I also don't put if statements at the end of a line because I don't believe that is very readable for most people including me. Also, in CoffeeScript its probably better to use isnt instead of !=, although rather than !=, you would want !==. if y isnt 0
x = 1
I'm not that sure about the use of unless, although it probably is a little bit more readable overall. break for [tag], i in @tokens when tag isnt 'TERMINATOR'
@tokens.splice 0, i if i
He says that is supposed to delete TERMINATOR from tokens. I tested it, it doesn't do anything, and the second example of the correct way doesn't work either. I think he meant this: filtered = []
for token, i in tokens
if not (i is 0 and token is 'TERMINATOR')
filtered.push token
i += block.call this, token, i, tokens while token = tokens[i]
Another example of something you CAN do in CoffeeScript but shouldn't. Actually it is a bad idea in general. I think a CoffeeScript programmer would actually write something like this: class Parser
block: (token) =>
@currentNode.push new Token(token)
parse: =>
for token in tokens
@block token
I would never write something like 'mainModule.moduleCache and= {}'Instead of
js = (parser.parse lexer.tokenize code).compile options
I would write tokens = lexer.tokenize code
parsed = parser.parse tokens
js = parsed.compile options