Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the "curly braces always should be on the statement line" rule so important? [closed]

I understand "semicolon implicit insertion", but I'm not sure it's occurs in case of function expressions.

For example, this two expressions always will interpreters equally:

Matrix.scale = function (mat, scaleX, scaleY, dest) {
// useful code
};

Matrix.scale = function (mat, scaleX, scaleY, dest)
{
// useful code
};

I like first one and I notice Google like it too: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Code_formatting#Code_formatting . But my colleague displeased with this style. The question arises whether or not follow strictly this rule even if with the functions declaration or it is a tribute to uniformity and this code will not break after a tricky minification?

like image 869
Microfed Avatar asked Dec 21 '22 17:12

Microfed


2 Answers

In other languages it might be a matter of taste, but in JS it can be a very hard to find source of an error. Consider the following snippet which might not do what one would expect on a first glance:

function foo()
{
  return
  {
    bar: 1
  };    
};

console.log(foo()); // => undefined

further reading: http://en.wikipedia.org/wiki/JavaScript_syntax (search for "semicolon insertion")

like image 158
Yoshi Avatar answered Dec 29 '22 00:12

Yoshi


Code style is just an agreement about how to format code and name variables made in:

  • (Sub)community of those who's coding on this language, developing specific libraries and so on.
  • Your very company.

It is hard to say, which to these two factors is more decisive, but it wouldn't be a bold statement to say that in good companies code style is closely related to the code style "in the wild". Partly this is because in good companies code is often contributed to open source and, thus, it is better not to reinvent it.

There are languages with very firm, thorough and recognized by the overwhelming majority style guides. For example, this_is_very_uncommon_to_java.

As for javascript, this is not the case. There is no such thing like abstract, general "javascript code style". The problem of bracing is still heavily discussed topic in javascript community, as well as semicolons. I guess, this is just because any other issues with js-developing are already resolved ;)

If you want advise (and it looks like you want), just find peace with your colleagues. Make any kind of agreement and follow it. This is the most important thing - good relations in team are overweighting any code styling agreements.

like image 29
shabunc Avatar answered Dec 29 '22 00:12

shabunc