Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>CoffeeScript gives you ruby-like syntax

The implied "only" in that statement seems preposterous to me. For one thing, it's both an over- and understatement of CoffeeScript's featureset, and for another, it's sort of like saying "The printing press just let people print books faster ∗yawn∗."

(Well, OK, so CoffeeScript isn't quite the leap that the printing press was, but you get the point.)



I haven't looked at CoffeeScript yet (I avoid Javascript work as it is already). What else does CoffeeScript give you?


From a comment on the other HN thread (http://news.ycombinator.com/item?id=4534863)

    string interpolation 
    multi-line strings
    comprehensions
    guarded loops (hasOwn...)
    easy constructor/prototype definition (class)
    avoiding global leaks/shadowing
    function binding
    splats, defaults
    safe usage of reserved names
    existential operator
    chained comparisons
    READABLE regular expressions


Dumb question: why would I use CoffeeScript over something like PyJS (pyjs.org) or even (shudder) GWT?

I've worked on enough projects that I've come to view syntax as a cost and not a feature. It can sometimes be worth paying that cost if it gets you lots of features - but all the features you list already exist in Python, and presumably can be translated just fine by PyJS. Why an entirely new language syntax instead of something that you're likely using on the server as well? Or is CoffeeScript basically Javascript with Ruby syntax?


>but all the features you list already exist in Python

Not all:

- string interpolation is way more flexible in CS

- splats are sort of partially covered by several python features, but not nearly so elegantly

- the existential operator is not found in Python at all; you have to test against None instead. Also, Python does not have anything like the "soak" feature that the existential operator does.

- No really, readable regex. Python still doesn't even have a literal notation for regexes, but CoffeeScript allows multiline literals with comments, ignored whitespace, and interpolation.

Also, I don't think that lowboy mentioned multiline lambdas, but yeah, multiline lambdas.

I once thought as you do that syntax is secondary and that what really matters is functionality. That was before I started using CoffeeScript. The thing is, syntax isn't important for the sake of making it a pleasure to write. It's important for making it a pleasure to read. And odd scoping scenarios notwithstanding, CoffeeScript is exceptional in that regard.

Here's an example. String interpolation in CoffeeScript is incredibly simple:

  console.log "Hello #{name}, how are you doing?"
translates to

  console.log "Hello"+name+", how are you doing?"
That's all. Typing it out, CoffeeScript saves you one keystroke. But there is a subtle boon to readability in the interpolation syntax. The first line says "We're placing the result of an expression here". The second line says "We're ending the string, concatenating the result of an expression, and then we're going to concatenate another string to that." The canonization of the idiom into syntax lets your brain relax and stop trying to translate.


CS is more than just syntactic sugar, but speaking in terms of syntax it has a more minimal mindset like Python or Ruby.

The difference between source and compiled code is much much closer. CS has been designed as a pure abstraction of JS, whereas those other projects take full-fledged languages with all of their idoms and features and make JS out of it. I agree that new syntax is a cost, and in general that we should fear abstractions. PyJS seems like it's more of a web framework (as is GWT) rather than a pure language compiler.

Python is a beautiful language, but just code in JS if you're working on the front end and are wary of CS. I'm a capable JS developer and it took me almost no time to get started with CS. Sure, you hit snags here and there with oddball situations and sometimes you have to check the output, but the benefits definitely outweigh the learning costs.


The sad thing is that it actually does a lot more than that, but the docs are gloss over or omit a lot of the subtle headache relief CoffeeScript provides. For example, in addition to making regular expressions readable by allowing a block syntax, it also allows regex interpolation, so this code:

  blah = "crayfish"
  pat = ///#{blah}///
is equivalent to:

  pat = /crayfish/
This is in the docs, but if you blinked, you'd miss it.


lowboy's summary is good. If you avoid JavaScript but are familiar with it, you should give CoffeeScript a try. I've always thought JavaScript got a bad rap from being misused, but I have never been fond of its syntax.

I used to use Python when I needed a more powerful calculator, or when when I needed a script to automate something tedious. Now I go to CoffeeScript for both of those use cases.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: