Live data from Hacker News

Writing Eloquent JavaScript Without CoffeeScript

oscargodson.com

41–50 of 55 posts

Re: Writing Eloquent JavaScript Without CoffeeScript

#41

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…

[deleted]

Re: Writing Eloquent JavaScript Without CoffeeScript

#42
The bit on the comma operator may be used in many other places, and I've seen it result in quite elegant code, particularly in small function bodies. Its use in the ternary here may be questionable, but in any case a good JS dev should know when it is appropriate.

My 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

#43
I 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?

Re: Writing Eloquent JavaScript Without CoffeeScript

#44
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.)

That's also a syntax error; I think CoffeeScript wins this round.

Re: Writing Eloquent JavaScript Without CoffeeScript

#45

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…

Same here - I used to line up variables with tabs and such to try to make things look neat and put in lots of dashed lines to separate groups of functionality. I don't know if it's experience or just preference but I don't like doing that anymore.

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

#46

Oscar, 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!') );

Python makes little frowns at the end of conditionals and loops that call functions or have sets of boolean logic ):

Re: Writing Eloquent JavaScript Without CoffeeScript

#47

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

Re: Writing Eloquent JavaScript Without CoffeeScript

#48
post #31

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…

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?"

My problem is that I want a comparison of syntax so I can see exactly how Coffeescript is an improvement over the equivalent Javascript.

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

#49

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

... not so much. A big part of the point of having a minimal function literal, `->` instead of `function(){}`, is that using anonymous functions is a bit more pleasant in any context -- including chaining.

    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

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

When 'false' is a valid value for a you'll also be bitten in the same way.
Post reply on HN