Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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")

According to the book Code Complete, the bracket should be put on the first line (which is referred to as the control statement) because it's misleading to put in on the second line since it doesn't show the logical structure of the code. If viewed from a graphical perspective, the second approach looks like this:

enter image description here

Where lines A-E represent the following, which is simliar to the second approach declared in this question.

A function foo ()
B {
C     print "Hello";
D     print "World";
E }

Which you can see is misleading because you can't tell if line B is subordinate to line A or if line B is its own separate statement

EDIT: In case the image is too abstract, here's a quick explanation of why the curly bracket is often included on the first line along with the control statement

Functions have two main elements:

  1. the control construct
  2. the function's statements

By putting the curly bracket on the second line (line B in the image) it is neither in the control statement nor is it part of the function's statements.

EDIT 2

IMO, each line of code should have a purpose to save vertical space to view more lines at once on my screen and to preserve the logical meaning of the code. Therefore, when the brace is on the same line as the function declaration then it is thereby associated with the statement that it opens. However, when it is on a line on its own (line B in my example) AND it is not indented to show subordinance to the function declaration, then it breaks the logical meaning of the code because it is neither a associated with a control construct nor is it a function statement. So, by putting the brace on the same line as the function declaration, then every line of code serves a purpose, it holds the logical meaning of the code, and it saves my screen space.

function foo() { // brace is on the same line and it's associated with the statement that it opens
    print "Hello"; // this line does something
    print "World"; // this line does something
} // this last brace terminates the function which is part of the control construct

This is 100% a taste matter. Whatever you prefer, I do agree it's preferred to be consistent. So if you're coming new to a project you really should follow the established style guide.

When you don't work for Google you don't need to follow that rule. It doesn't break anything. Personally I like the latter version it's one more line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top