Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript function parentheses

Tags:

coffeescript

I'm just learning CoffeeScript and I'm trying to do something I would normally do in plain 'ol JavaScript.

Here's what I tried to do:

initializeWebGL = (canvas) ->
    gl = canvas.getContext "webgl" or canvas.getContext "experimental-webgl"

Which compiles to what I kind of expect:

var initializeWebGL;

initializeWebGL = function(canvas) {
  var gl;
  return gl = canvas.getContext("webgl" || canvas.getContext("experimental-webgl"));
};

In order to get what I really want, I have to wrap the getContext arguments with parentheses:

initializeWebGL = (canvas) ->
    gl = canvas.getContext("webgl") or canvas.getContext("experimental-webgl")

Which produces what I want:

var initializeWebGL;

initializeWebGL = function(canvas) {
  var gl;
  return gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
};

Is there a better way to do what I'm trying to achieve than to just add parentheses around the function calls like in the second example?

like image 735
OozeMeister Avatar asked Feb 14 '23 19:02

OozeMeister


1 Answers

Is there a better way to do what I'm trying to achieve than to just add parentheses around the function calls like in the second example?

No, I don't think so. My rule of thumb is it's OK to omit parentheses when the function call and its arguments are the last thing on a line, otherwise include them.

OK

someFunction 1, 2, 3

Not OK

someFunction 1, someOtherFunction 2, 3

In general I try to avoid overly-concise, terse statements. They are harder to work with both mentally as well as stepping through in a debugger is trickier.

like image 147
Peter Lyons Avatar answered Feb 28 '23 05:02

Peter Lyons