News for hackers. Stuff that matters.
Re:CoffeeScript: JavaScript without the Fail (slides) by shazow on Friday September 09, 2011 @06:33PM (#2980117)
I don't understand why this is downvoted. Does somebody disagree? Why?

It's true, every time I see CoffeeScript vs JavaScript examples, they're often unfair. Some addressed in this older thread[1].

Another thing I don't understand is why make the parenthesis for function calls optional? Such as

    sum_and_difference 5, 2
Especially since one of the most common complaints about JavaScript is ambiguous inconsistency (var, semicolons).

Ok, so parenthesis are optional,

    foo = myfunction()
calls myfunction, but

    foo = myfunction
does not? I suppose this is better than the way Ruby does it where you have to jump through hoops to get a reference to your function, but is this really better than making parenthesis for calls mandatory?

[1] http://news.ycombinator.com/item?id=2542492

Re:CoffeeScript: JavaScript without the Fail (slides) by grayrest on Friday September 09, 2011 @06:56PM (#2980164)
The optional parens is something I personally go back and forth on. I went through a phase where I always left them off but have been moving back towards using them most of the time. The holdout is for calling things that take functional arguments. Here's how I'd write the nodejs.org home page example:

    {createServer} = require 'http'

    host = '127.0.0.1'
    port = 1337

    server = createServer (req, res) ->
        res.writeHead(200, 'Content-Type': 'text/plain')
        res.end('Hello World\n')
    server.listen(host, port)

    console.log "Server running at http://#{host}:#{port}/"
Notice `require` looks sort of like a language statement. The `console.log` thing is my own quirk. However, the call to `createServer` would require a hanging paren if parens weren't optional. It wouldn't look bad here (and, in fact the official example uses it to chain the listen call) but is less attractive if you're using longer functions. Calls like `memoize` and `operation` (think unit of work pattern) tend to work well with this.
Re:CoffeeScript: JavaScript without the Fail (slides) by shazow on Saturday September 10, 2011 @01:57AM (#2980961)
Perhaps I'm crazy but I honestly feel mandatory parenthesis here improve readability

    {createServer} = require 'http'
    {createServer} = require('http')

    console.log "Server running at http://#{host}:#{port}/"
    console.log("Server running at http://#{host}:#{port}/")
At the very least, it improves consistency which to me improves readability. Making something "look like language" (or perhaps "prose" is more appropriate) doesn't improve the parsing process for my brain. In fact, the inconsistency with the rest of the non-language "code" throws it off the tracks.

Even if it didn't, I'd still say the ambiguity is not worth it. Perhaps CoffeeScript needs a "use strict"; for the final stroke of irony.

One more question: Why "#{foo}"?

Is there any template language that uses this syntax? (I am not familiar with one.) If it were up to me, I would have chosen "${foo}" to be like Mako, or plain "{foo}" to be like Python's string formatter, or "{{foo}}" to be like the more recently popular Mustache.

Edit: As cschep points out, the "#{foo}" syntax is Ruby string formatting syntax. Thanks.

Re:CoffeeScript: JavaScript without the Fail (slides) by cschep on Saturday September 10, 2011 @02:19AM (#2980995)
Ruby does string formatting that way.
Re:CoffeeScript: JavaScript without the Fail (slides) by andypants on Saturday September 10, 2011 @06:54AM (#2981336)
Jade is a templating lib for javascript, and it uses #{var} and !{var}.

(Although I don't know what that would be doing in a console log.)

Re:CoffeeScript: JavaScript without the Fail (slides) by arckiearc on Friday September 09, 2011 @10:44PM (#2980631)
I went through the same process as you. I think it's okay not to have hard and fast rules for every acceptable situation to leave off parentheses, it's more a judgement call based on the readability of the statement.

`Require` and `console.log` are perfect examples where leaving off the parentheses helps with clarity, same with the call to `createServer`. In your example I'd even argue that you could do the same with `res.end` too.

Re:CoffeeScript: JavaScript without the Fail (slides) by sunchild on Friday September 09, 2011 @11:29PM (#2980708)
I don't understand your swipe at Ruby. What's so terrible about:

foo = object.method(:method_name)

foo.call

Re:CoffeeScript: JavaScript without the Fail (slides) by philjackson on Saturday September 10, 2011 @09:38AM (#2981548)

    foo = myfunction
Considering the higher-order nature of Javascript programming having quick access to refs is important.

The Coco dialect of Coffee suggests that a bang could be used to denote a call:

    foo = myfunction!
Re:CoffeeScript: JavaScript without the Fail (slides) by guywithabike on Friday September 09, 2011 @06:53PM (#2980152)
How often do you need a reference to a function and how often do you call functions in Ruby?
Re:CoffeeScript: JavaScript without the Fail (slides) by antipax on Friday September 09, 2011 @08:20PM (#2980331)
call a function taking no arguments without using ():

foo = do myfunction