Monday, July 22, 2013

Coding: JavaScript - Single Var vs Multi Var

I came across an article discussing Single-Var vs Multi-Var declaration for JavaScript. I am almost always for multi-var pattern from experience.

Programming from scratch, I do not particularly care for one pattern over the other. Some argue one looks better than the other. That is a personal preference. Single-Var promoters say it saves space, but most people tab it to the same level as the other variable names which defeats the space saving (unless you compress the file, then maybe there is an argument there). Even then, space is so irrelevant today because either the program is so large that the difference is insignificant or so small that systems today are so advanced that no one will even notice the minuscule delay.

Having to deal with troubleshooting, debugging, and reading through thousands of spaghetti-code, multi-var has so many more advantages. You cannot assume process when debugging or troubleshooting because if you are troubleshooting an issue, you are looking for a problem. On the same note, you cannot assume strict is being used. Also, developers may easily assume others can pick up the code easily or be aware of certain standards but not everyone is like that. You also do not know how many people the code has gone through or even how many companies.

There have been plenty of codes that I had to debug due to a variable not declared properly or even just misspelled. There was a code I had troubleshot because someone used an I instead of l or maybe it was a 1. Who is going to notice an upper-case 'i' to an lower-case 'l' to the number 1 when skimming through hundreds of lines of code?

Another use of declaring multiple vars is also to find where the variables are declared. Sure you can have IDE. But if you find yourself having to troubleshoot in unfamiliar environments, this makes a world of a difference to navigating through code with a simple text editor with a word find (or find word within a library of files). Even if comma-first, you then have to search for both comma-first and with var (in-case it is the first variable in the list or a parameter to a method/function or a different variable on a different block level [scope]).

For any advanced debuggers (I assume advanced developers), the cosmetics of tabs, alignments, etc., is barely a nuisance. At some point, I can read both styles easily without complications even if they were multi-line or single-line or case-insensitive.

So if there was an opinion to be made on this topic, it should be the people who are going to maintain the code after deployment. Developers have a project plan. Troubleshooters are expected to fix the problem yesterday. Time is more valuable to them than developers. No matter how experienced developers are or how mature the procedure may be, errors are human-nature.

Ultimately, either is acceptable. To me single-var would be like the developers leaving the power button in the back of the computer. You don't have to see the button and closer to the power supply which means no extra wires in the case. When was the last time you seen that design (the design did exist for those of you too young to have seen them)?


Kent Dodds -
Ben Alman -
Dan Hough -


3/12/2014 - Added two more references which support multi-var patterns. Ben Alman does a good job describing work-arounds to the advantages of single var pattern. Dan Hough does a good job explaining preventing errors and simplifying rafactoring. I like the addition of the refactoring which also expands on code maintenance.

There were a couple posts on the use of 'use strict'. For someone maintaining code, you may not always have access to modify code. Also, there are times working in a corporate environment which you may not even be able to install your choice of IDE or add-ins. Using multi var pattern is much more helpful for programmers supporting old code. Especially with javascript where code can put interjected in out-of place areas or written by untrained programmers like web designers.

No comments:

Post a Comment