Live data from Hacker News

Writing Eloquent JavaScript Without CoffeeScript

oscargodson.com

21–30 of 55 posts

Re: Writing Eloquent JavaScript Without CoffeeScript

#22

He's got to be trolling. Multiline ternary operators aren't in my book better than an if block. He also completely misses the point about javascript's global-by-default variables and how easy it is to introduce them with typos, properly building classes, etc.

> He also completely misses the point about javascript's global-by-default variables and how easy it is to introduce them with typos

Using strict mode will avoid these kinds of mistakes.

Re: Writing Eloquent JavaScript Without CoffeeScript

#25
post #11

It 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.

Totally -- I think that 0 (which is a perfectly valid value for a number to be) bites people doing lazy initialization all the time. For this reason, CoffeeScript provides the existential operator, which asks: does this variable exist? (Is it not null or undefined?) You can use it in the same places you would use "||", as well as some others: # In place of `||` result = answer ? default # Does `object.value` exist? i…

I still haven't gone over to CoffeeScript. But I must admit Those are some compelling examples. ?= is especially nice (I miss ||= from Ruby)

Re: Writing Eloquent JavaScript Without CoffeeScript

#26

I 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…

You should remember that you are not the only person that is going to be trying to read that code (unless it is a personal project, of course). Good layout helps other people read, understand and debug your code at a later date (as well as you at a later date, when you've long since moved on to other pojects in terms of active development work).

Also with Javascript's function-scoped variables and the fact it will let you declare them more than once in a given scope (the second and subsequent decleration being taken as either a NOOP or a basic assignemtn (if there is assignment with the decleration), declaring variables at the point of first use can be dangerous as it is easy, especially if you are messing around in global scope (which is to be availaded generally, but can't always be), to create hard to diagnose bugs due to variable values being clobbered because the same name is used elsewhere.

Re: Writing Eloquent JavaScript Without CoffeeScript

#28

Using the ternary operator is more "eloquent" than conditionals? That's true iff "eloquent" means terse in terms of number of characters, but I find if(...){} much more readable.

"I find if(...){} much more readable."

Not only is it more readable, it's more correct in the example given.

"if" is a statement. The ternary operator (?:) is an expression, that returns a value. The semantics are different, and using the ternary operator "merely" to replace an if statement, especially where no assignment of the ternary operator's return value is made, is a code smell.

The ternary operator is great if you're replacing a simple "if something assignment to X else other assignment to X." Any other use is questionable.

Post reply on HN