I don't buy this argument at all. Let's start with what might be a fairly typical set of variable declarations in Javascript:
var firstVar = "one";
var secondVar = "two";
var thirdVar = "three";
I think you'd be hard-pressed to find anyone that would say this is significantly more difficult to read than the lined-up alternative:
var firstVar = "one";
var secondVar = "two";
var thirdVar = "three";
But, of course, variable names aren't always going to be short. You might have something like this instead:
var firstVar = "one";
var secondVar = "two";
var thirdVar = "three";
var thisVarHasAReallyLongName = "four";
var thisIsAnotherVarWithALongerName = "five";
My eyes don't have any problem scanning the variable names to see what's what. I also find it very natural to scan the variables by-value to determine which value is assigned to which variable (which I believe is typically the argument for the lined-up-equals alternative). Lined up, this looks like the following:
var firstVar = "one";
var secondVar = "two";
var thirdVar = "three";
var thisVarHasAReallyLongName = "four";
var thisIsAnotherVarWithALongerName = "five";
I don't know about you, but I find it significantly more difficult to visually parse these by-value to determine the variable corresponding to values "one", "two" and "three". The whitespace gap means that visually I may forget the vertical alignment of what I was looking at on the right when I try to find the variable name on the left.
I also think that it's at least a little bit harder to consistently enforce a convention of this kind. If a variable name gets too long, and you're not using some sort of auto-formatting, you have to manually space-out the variables to a new name length every time you introduce a newer, longer-named variable.
I think the real impulse people have for this kind of convention is that it actually just looks a little bit neater, because things are aligned in columns. In reality I think it's much more difficult to parse and maintain.
Agree! I can page through code like a speed-reader, until I get to some pretty-printed spaced-out page where I have to stop and line up stuff like that. I think its just OCD folks distracting themselves, its absolutely not contributing to the code, reader or bottom line.
Your last example could be aligned a little bit differently to make it easier to visually parse:
var firstVar = "one";
var secondVar = "two";
var thirdVar = "three";
var thisVarHasAReallyLongName = "four";
var thisIsAnotherVarWithALongerName = "five";
var firstVar = "one";
var secondVar = "two";
var thirdVar = "three";
var thisVarHasAReallyLongName = "four";
var thisIsAnotherVarWithALongerName = "five";
The different length of the variable names should be derived from the fact that the variables themselves are different. So they should be grouped separately.
I think aligning the variables is better it you want to highlight some relation between the values in the second column. For intstance, in the example you gave it immediately pops out that the values are ordered in 1,2,3,4,5 if you look at the aligned version.
I've got this processor VM implementation that uses an enum for all the instructions and their opcodes - hundreds of them. It's easier to discover missing opcodes with them all aligned.
Comments
Anyone else have a gag reflex when they see code like this?
If I've got an enum where all values require assignment, lining up the = makes it easier for my human eyes to scan names and values later.
I don't buy this argument at all. Let's start with what might be a fairly typical set of variable declarations in Javascript:
I think you'd be hard-pressed to find anyone that would say this is significantly more difficult to read than the lined-up alternative: But, of course, variable names aren't always going to be short. You might have something like this instead: My eyes don't have any problem scanning the variable names to see what's what. I also find it very natural to scan the variables by-value to determine which value is assigned to which variable (which I believe is typically the argument for the lined-up-equals alternative). Lined up, this looks like the following: I don't know about you, but I find it significantly more difficult to visually parse these by-value to determine the variable corresponding to values "one", "two" and "three". The whitespace gap means that visually I may forget the vertical alignment of what I was looking at on the right when I try to find the variable name on the left.I also think that it's at least a little bit harder to consistently enforce a convention of this kind. If a variable name gets too long, and you're not using some sort of auto-formatting, you have to manually space-out the variables to a new name length every time you introduce a newer, longer-named variable.
I think the real impulse people have for this kind of convention is that it actually just looks a little bit neater, because things are aligned in columns. In reality I think it's much more difficult to parse and maintain.
Agree! I can page through code like a speed-reader, until I get to some pretty-printed spaced-out page where I have to stop and line up stuff like that. I think its just OCD folks distracting themselves, its absolutely not contributing to the code, reader or bottom line.
Your last example could be aligned a little bit differently to make it easier to visually parse:
My solution:
The different length of the variable names should be derived from the fact that the variables themselves are different. So they should be grouped separately.I think aligning the variables is better it you want to highlight some relation between the values in the second column. For intstance, in the example you gave it immediately pops out that the values are ordered in 1,2,3,4,5 if you look at the aligned version.
I've got this processor VM implementation that uses an enum for all the instructions and their opcodes - hundreds of them. It's easier to discover missing opcodes with them all aligned.