Live data from Hacker News

Writing Eloquent JavaScript Without CoffeeScript

oscargodson.com

11–20 of 55 posts

Re: Writing Eloquent JavaScript Without CoffeeScript

#12
So it's not just me, then. IMO, writing `someThing ||= {}` is much more eloquent than `var someThing = someThing || {}`.

The first point he raises is a valid argument; CoffeeScript's intro does uglify the JS a little to make its point. But it doesn't take much.

> ...merely inaccurate due to a lack of knowledge on JavaScript...

Kinda think the opposite idea was how CoffeeScript came into being, amirite? It was written to make JavaScript look AND act better. It wasn't developed by people that had no idea how to use JavaScript, and it certainly isn't used exclusively by people who don't know how to use JavaScript, too.

Re: Writing Eloquent JavaScript Without CoffeeScript

#13
Just 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 to make the JS look as nice as possible.

Re: Writing Eloquent JavaScript Without CoffeeScript

#14
JavaScript can be pretty too and the examples on the CoffeeScript site are purposely written to be ugly to make CoffeeScript look even prettier.

I'm not sure how many times this will have to be clarified, but the JS code on the CoffeeScript site are not examples, but the compiled JS. Even so, it's in many cases prettier than Oscar Godson's "eloquent" JS, IMHO.

Re: Writing Eloquent JavaScript Without CoffeeScript

#15
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?
    if object.value?

    # Lazy initialize the speed of the car:
    car.speed ?= 60

    # Give me the value of `length`, only if `object` exists.
    length = jsonResponse.list.object?.length

Re: Writing Eloquent JavaScript Without CoffeeScript

#17
post #12

So it's not just me, then. IMO, writing `someThing ||= {}` is much more eloquent than `var someThing = someThing || {}`. The first point he raises is a valid argument; CoffeeScript's intro does uglify the JS a little to make its point. But it doesn't take much. > ...merely inaccurate due to a lack of knowledge on JavaScript... Kinda think the opposite idea was how CoffeeScript came into being, amirite? It was written…

I must agree whole heartedly. I feel the author looks at the first few examples on the coffeescript page. Can I see an eloquent example of list comprehension or nested functions?

Re: Writing Eloquent JavaScript Without CoffeeScript

#18

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.

For the sake of a productive discussion, two of the things mentioned about converting ifs to ternaries _could_ be used to create better code.

The ternery operator has some readability problems, but it is an expression, not a statement. Turning statements into expressions is powerful, which is why CoffeeScript's if/then compiles into an expression. Too bad this wan't mentioned.

Likewise, the comma operator allows JavaScript programmers to use a block of consecutive statements as an expression, something the author points out when explaining how to use ternaries in place of if statements.

The notion that you can use a sequence of statements as an expression without having to resort to a "let" form (function () { ...; ...; ...; return foo; })() is another underused construct that can make for improved JavaScript.

Put the two ideas together, and yes you can express a multi-line if statement with shorter keywords, but more importantly you have an if expression without having to wrap it in a function and encumber it with return statements.

I'm sorry that idea wasn't given more attention. If you aren't using JavaScript directly, this pattern may be useful in a more functional, expression-oriented style of programming.

Re: Writing Eloquent JavaScript Without CoffeeScript

#19
post #4

This page shows no content on kindle webbrowser.

It took my browser 20 seconds to render the text. I've no idea why he would load static content dynamically.

Looks lite it's been pulled in from his Posterous blog. Bizarre way of doing it… why not just link to the posterous blog post?

Re: Writing Eloquent JavaScript Without CoffeeScript

#20
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.

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.)
Post reply on HN