There are some real problems with these "eloquent" styles. They work only in a monospaced font. I code in a proportional font, and none of the column alignment tricks will look right when I view the code. Even if you like to code in a monospaced font, there's no reason to write code that becomes hard to read in a proportional font. It's very easy to write code that is perfectly readable in both monospaced and proport…
Writing Eloquent JavaScript Without CoffeeScript
41–50 of 55 posts
Re: Writing Eloquent JavaScript Without CoffeeScript
#42My only gripe is with placement of commas in object literals and variable declarations which has become so common over the past three or four years.
var x = 1
, y = 2;
or var x = 1
,y = 2
or {
k: 1
,k2: 2
}
Are terribly unsightly, and do not in any way benefit the maintainer. It's just now you have to worry about the first line rather than the last one.Re: Writing Eloquent JavaScript Without CoffeeScript
#43Re: Writing Eloquent JavaScript Without CoffeeScript
#44It should be pointed out that "var a = a || someDefault;" can bite you when 0, "", or null are valid values for a. The empty string in particular can be surprising here at times.
In these cases, I would check for the type, so instead of your example, I would say "var a = typeof a !== 'undefined' ? a || someDefault". (Not that that is particularly pretty, it's actually quite ugly.)
Re: Writing Eloquent JavaScript Without CoffeeScript
#45I don't want to sound like an asshole and I'm saying nothing about the author, but in my personal experience I found lining up variables or =s was something I worried about when I first started programming. Now my mind's eye just totally skips variable declaration when visually code parsing, variable declaration is meaningless drivel that exists only to stop typos (a good purpose). I had an opinion on the layout of t…
I find it much more important to put in javadoc style comments to make the code understandable for other users rather than lining things up nicely.
If you're using an IDE you can view all of the variables and methods sorted however you like anyway with a class or outline view.
Re: Writing Eloquent JavaScript Without CoffeeScript
#46Oscar, when your code itself is making little frowny faces at you after you write it, something has gone wrong. x == 1 ? alert(x) :( alert('Error'), x = 1, alert('All Fixed!') );
Re: Writing Eloquent JavaScript Without CoffeeScript
#47I have to admit that I'm resisting CoffeeScript. It looks interesting but for me it seems like just an extra step for something that isn't bothering me. I'd rather write code that's a little more verbose than to have to "remote control" it via another syntax. Am I missing the boat?
Other than that CoffeeScript is pretty cool.
Re: Writing Eloquent JavaScript Without CoffeeScript
#48Just to clear one thing up -- Oscar writes: > The examples on the CoffeeScript site are purposely written > to be ugly to make CoffeeScript look even prettier. ... not at all. The side-by-side examples on CoffeeScript.org are showing you the compiled output . The point is to demonstrate -- although it's certainly far from perfect -- how CoffeeScript features can be compiled into clean JavaScript. Really, our goal is…
This kind of clarification is exactly why I posted this link. It's great to clear the air in one respect and at the same time ask the question, "is there something on the CS site needing change to avoid this confusion?"
When I visit the site and I see a table with CoffeeScript on one side and Javascript on the other, my brain sees just enough to verify that the expected elements are all there before it says, "Yep - there is the comparison you wanted".
So while it does clearly state that the JavaScript is the output of the tool, my brain has already decided that it is exactly what I was expecting, and it takes me a second to re-adjust my thoughts.
Ironically, if the output looked less "normal" I probably wouldn't have the same problem, but it is formatted nicely and is good enough that subconsciously my brain tells me "equivalent syntax" and not "compiler output".
Re: Writing Eloquent JavaScript Without CoffeeScript
#49I have to admit that I'm resisting CoffeeScript. It looks interesting but for me it seems like just an extra step for something that isn't bothering me. I'd rather write code that's a little more verbose than to have to "remote control" it via another syntax. Am I missing the boat?
Not really. It's not usually mentioned but CoffeeScript loses a lot of its supposed elegance when you try to do things like chaining. Especially chaining a function that takes a function as an argument. To achieve better looking chaining you'll talk yourself into not using anonymous functions. tldr: whitespace significant languages do not do chaining very elegantly. Other than that CoffeeScript is pretty cool.
list.map((num) ->
num * 2
).map((doubled) ->
doubled + 5
)
... instead of: list.map(function(num) {
return num * 2;
}).map(function(doubled) {
return doubled + 5;
});
It's not a huge difference. But, by the same token, it's certainly not a reason to avoid the use of anonymous functions.Re: Writing Eloquent JavaScript Without CoffeeScript
#50It should be pointed out that "var a = a || someDefault;" can bite you when 0, "", or null are valid values for a. The empty string in particular can be surprising here at times.