small medium large xlarge

Back to: All Forums  CoffeeScript
03 Jun 2011, 22:44
Michael Edmondson (1 post)

I’m trying to wrap my head around the optional parentheses.

from PDF page 60-63

new Tribble if @isAlive
tribble1 = new Tribble
showArea new Square(2)
throw new Error('Invalid starship captain')

Just curious if there was a convention to follow, as it appears that you wrap constructor params with parens.

I tested (successfully) the following:

showArea new Circle 4
throw new Error 'showArea requires a Shape instance!'

…but is this frowned upon?

You seem to be consistent in wrapping constructor params in parens; whereas, if no params are passed, sometimes you have parens (e.g., @new Raven()@) and sometimes not.

04 Jun 2011, 00:54
Trevor Burnham (54 posts)

Right. So, this is a personal stylistic preference of mine; I just think that

showArea new Circle 4

reads less clearly than

showArea new Circle(4)

or perhaps

showArea(new Circle 4)

Otherwise, the division between @showArea@ (a function) and @new Circle(4)@ (an object) just doesn’t seem as clear to me as it could be.

Some CoffeeScripters are of the school of thought that parens, curly braces, etc. should be omitted whenever possible. I think that while implicit parens and curly braces are generally great, a dash of extra punctuation can make code a lot more readable. (Also, code that relies on too much implicit punctuation is more likely to be broken from one CoffeeScript version to another as the parser is tweaked.)

You must be logged in to comment