0

I'm developing a JavaScript Style Guide and I'm aware that the only rule that can be applied on code conventions is to be consistent but I'm curious about this question since none of the major frameworks use this convention.

What are the advantages and disadvantages of vertical align the colons/equals when declaring objects/variables?

var a   = 1,
    ab  = 2,
    abc = 3;

var obj = {
    a   : 1,
    ab  : 2,
    abc : 3
};
1
  • Easier to read, that is it. Which is the argument set forth for most things in a "Style Guide". Commented Mar 5, 2013 at 18:23

3 Answers 3

2

Better code readability. That's about it.

Disadvantage is when using find to find the value of a variable as there are more spaces than required. So a search for variablename = may result in nothing due to it actually being defined as variablename[space][space][space]=.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that's about the only answer possible, but doesn't that apply to just about everything that one would find in a style guide?
0

Searching in the code would be one obvious disadvantage:

Imagine that I inherited your system, or must work on it. In the progress, I need to figure out what the a variable is. The system has a huge codebase with lots of lines, so simply scrolling through and looking at every line is not an option.

If the code standard was variable = 123, then I could simply do a "search-in-files" for variable[SPACE]=[SPACE].

This is where the standard you posted might be a bad idea. I don't know how many spaces are going to between the variable name and your equal sign. Of course I could search for a[SPACE]=, a[SPACE][SPACE]=, a[SPACE][SPACE][SPACE]= etc., but it'd be a lot harder.

The only way to search for the declarations (in a raw code editor with no "find declaration" helper), would be to do a Regex search for variable\s+= (one or more spaces) or variable\s*= (zero or more spaces), which will take longer and requires an editor with Regex search capabilities.

2 Comments

Shouldn't that be variable\s*=? Who is to say that there's any space at all?
@TedHopp I was simply going for the OP's standard which seems to follow a space before the equal sign/colon, but you're right. I've updated my answer to specify your point.
0

Putting each variable on a separate line will benefit you when you go to read it again (or someone else reads it) because it will be clear that these are different variables, so increased readability is the main factor.

It mainly is a benefit in the long term when it comes down to passing the code to another programmer to maintain or modify something or if you come back to it after a long period of time and can't remember the code.

It is the same in most languages.

Also aligning the variables with eachother (and the values) can make the code look very neat and tidy but it is a lot of hassle to do and most people don't do this. You would align the start of the line but not anything else afterwards.

Imagine what would happen if something changed and you had to realign everything.

NOT GOOD. Waste of time which could be spent programming.

1 Comment

Of course the comma should go on the end of the line but that's a minor difference. I agree with your point that most use a separate var on each line instead of comibining them. I personally do that but there are people who like this way of coding and there is nothing we can do about it. It's like concatenating a string, some people do "Some text" + variable + "some more text" and some people use string format to do the job for them. Matter of preference... But in general if we consider that style. It is easier to read the above style instead of var a = 1, b = 2, c = 3; But I agree with you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.