Hacker News new | ask | show | jobs
by brightsize 4694 days ago
I love CS myself. To me, in terms of aesthetics, CS is to JS what Scala is to Java. I have been bitten by the whitespace issue that you mention though. I don't recall the circumstance but an indent that was off just a bit did not cause the compiler to complain, but did cause it to generate code that I did not intend. It took some debugging of the JS output to find that one, and it certainly gives one pause to think how similar bugs might slip into the code undetected. I've never encountered this kind of problem with Python, so I wonder if the CS compiler just needs to be a little fussier/smarter with its whitespace processing?
2 comments

Compilers/interpreters complain about code that's invalid. Since the code is valid it shouldn't interfer, the problem is in the language's syntax design that's prone to shooting yourself in the foot. The design lets you mix indentations, something that Python won't let you do.

A very simple scenario where hell is a keystroke away:

    hello =
      woot: 1
      yay: 2
      funfunfun: (x) -> x + 1
    
    hello2 =
        woot: 1
        yay: 2
       nowthatsfun: (x) -> x + 1

Other:

    y1 = (x) -> 
        x + 1

    z1: (x) -> 
        x + 1
      
      
    y2 = (x) -> 
        x + 1

    z2 = (x) -> 
        x + 1
http://js2coffee.org/#coffee2js

Those are fairly visible here, not so much on a huge codebase (not to mention with some callback hell attached). You can write perfectly legal code that just doesn't work as expected.

//edit:

worth a read, with more trap examples http://ruoyusun.com/2013/03/17/my-take-on-coffeescript.html

While the examples are completely accurate, I've just never run into an issue like that in practice. The majority of my codebase is CoffeeScript and it's just never been a problem.

It's pretty visually obvious when your indentation is out of whack. Even if it isn't, this kind of error would be picked up pretty quickly by your test suite.

Your second example though exists in exact the same way in python and ruby. Some people prefer to hunt for missing comma's and brackets, others for spaces.

I think that's a matter of taste. Coming from python, I've quite enjoyed coffee-script's syntax and I've actually done something in between, using brackets for objects just because I like the readability of it, but avoiding all semicolons and taking advantage of the indentation for code blocks.

Yes, the second is a bit forced, couldn't remember what was the other case I banged my head against the desk. I'm a Python programmer too, maybe that's why I expected the language will prevent me from mismatching blocks of code. Well, even sass and haml (which aren't full-blown programming languages) aren't that easy to mess up. Using Python convention (4 spaces indent) instead of the unofficial but common ruby/js/sass ones (2 spaces) helps a bit to spot out obvious bad indents, but still - in general I don't find the language trustworthy enough to jump into something big with it (for all the reasons I listed before).

btw added a link above with some more syntax gotchas.

Indenting with tabs (that are displayed as wide as multiple spaces) would make the first scenario much more noticeable.
Comparing Coffeescript to Scala is an outrage! ;)

When I heared about Coffeescript I thought about Scala, too. Not because it reminded me about Scala, but who it took to make Scala and then out of the blue some guy comes along with some new lang, which should ease some problems.

CS is to JS what PHP is to Perl.