Thread:Manual talk:Coding conventions/JavaScript/Discouraging from using a single "var" for multiple variable declarations.

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


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


 * style="padding:.7em;" | vs.
 * Two lines changed || || One line changed
 * }
 * The multiple  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.
 * The multiple  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  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:


 * style="padding:.7em;" | vs.
 * }


 * 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):


 * }


 * style="padding:.7em;" | vs.
 * style="padding:.7em;" | vs.
 * style="padding-left:2em;padding-right:.5em" | or, adding tabs for formatting the first var as soon as the second gets added, sucks a lot when diffing again:''
 * style="background-color:white;" |
 * style="padding:.7em;" | vs.
 * style="background-color:white;" |
 * }


 * Multiple  offer higher aesthetics for comments on top of variables without even thinking about it:

or (better, but haven't seen this one so often) Compared to the first multi  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  version would look quite horrible in a diff when removing the first variable declaration.
 * style="padding:2em;" | vs.
 * }

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, I highly disagree with this.