6 ms·
Just because you CAN do something in CoffeeScript doesn't mean you SHOULD, or should ALL of the time. Just like in JavaScript where, if you want, you can write
by ilaksh 14y ago
Just because you CAN do something in CoffeeScript doesn't mean you SHOULD, or should ALL of the time. Just like in JavaScript where, if you want, you can write all of your code on one line, but you shouldn't.
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
- klapinat0r 14y agoExactly this. Thanks for doing a nice writeup.
- thezilch 14y agoSo, in short... In CS, assign everything to a var, because good luck parsing CS tokenization. In JS, assign a few things with a keyword or explicitly to a namespace, because good luck with global state(s). Unfortunately, only the later is recommended, while the former is NOT and discouraged in lieu of being so verbose like the later. sparkles
- ilaksh 14y agoThe last example I assigned the results to a var because it was doing a huge amount of work, any of the steps could fail, and it was just a lot clearer that way. You don't always have to assign things to vars. If you have two or more function calls on the same line, use parentheses after the first one. Or use parentheses in all of your function calls. Its just an advantage for some people to be able to leave the parens out because it is a little more readable for them. Otherwise, you can just write it the same as you would in JavaScript. Scope is another big advantage of CoffeeScript.
- thezilch 14y agoYou had more than one example of pulling out "anonymous" objects from a call, because that is "how it's done." The point is that it is ridiculous that one must go to these lengths to help parse a language, especially when it flies in the face of the language's doctrine. Same goes for parentheses "here or there but not there, sometimes." This may very well be the anathema for those that don't understand the claims that ";" or "function" or "}" are real burdens.
- shardling 14y agoIt doesn't go against the language's doctrine. The guy who invented it explicitly recommends pulling out anonymous objects and functions if they're not super trivial.