Topic on Manual talk:Coding conventions/JavaScript

Jump to navigation Jump to search

Discouraging from using a single "var" for multiple variable declarations.

1
Danwe (talkcontribs)

I have been thinking and researching about the topic of multiple vs. single var statements recently and have changed my ways entirely. I switched from using var do declare bunches of variables at the same time to using one var per line. Here are some reasons, going so far that I would completely discourage from using a single var for anything more complex than var a, b, c;

  • Multiple var are better readable in diffs. Each line ends with a ";" and is completely self-contained. E.g.:

var whatever = 0,
    whatever2 = 2;

vs.

var whatever = 0; 
var whatever2 = 2;

Two lines changed One line changed
The multiple var version will therefore keep the git blame for the first line in tact, which is a huge advantage. Changing a line of code as a side effect of syntactical sugar or preferences is bad.
  • Huge difference when debugging. If you have a list of five variables using one var and you want to see the value of the second one before creating the third one, then this is not really possible in any debugger known to me since all five variables are created in one step.
  • Whenever tabs are displayed with 8 spaces, the whole one var thing looks incredibly ugly:
var foo = 123,
        bar = 321;
vs.
var foo = 123;
var bar = 321;
  • When assigning initial values does not fit into one line, one is left with the question how to spread the assignment over multiple lines and I have seen various models, none of which makes me feel comfortable when looking at. Some examples (there are many more variations, you get the idea I hope):
// Quick skimming over the code could give you
// the expression "xxx" is a variable as well. 
var foo = foo(
    xxx + "someverylongstring..." ),
    anothervar = false;
// less misleading but still kind of ugly
var foo = foo(
        xxx + "someverylongstring..." ),
    anothervar = false;
var foo = {
    field: 1
},
    bar = {
        field: 2
    };
vs.
var foo = {
    field: 1
};
var bar = {
    field: 2
};
or, adding tabs for
formatting the first
var as soon as the
second gets added, sucks
a lot when diffing again:

var foo = {
■■■■    field: 1
■■■■};,
    bar = {
        field: 2
    };

vs.

var foo = {
    field: 1
};
var bar = {
    field: 2
};

  • Multiple var offer higher aesthetics for comments on top of variables without even thinking about it:
// some comment about foo
var foo = true,
    // some come comment about bar
    bar = false;

or (better, but haven't seen this one so often)

// some comment about foo
var foo = true,
// some come comment about bar
    bar = false;
vs.
// some comment about foo
var foo = true;
// some come comment about bar
var bar = false;

Compared to the first multi var version this gives more space for comments and does not look odd or like the one comment is more important than the other one.
Also, the first single var version would look quite horrible in a diff when removing the first variable declaration.

Most of this has also been covered in a more extensive article by Ben Alman.

I am not saying we should change all var statements now, but add this to the coding guidelines and have an eye for it during code reviews. Currently the convention does even discourage from using multiple var, I highly disagree with this.

Reply to "Discouraging from using a single "var" for multiple variable declarations."