Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Should the equals sign be alined on protocol buffers? I am having a huge discussion with a colleague and we can't get to decide on what's the best practice.

message foobar {
    optional bool var_one_short             = 1;
    optional bool var_two_looooooooong_name = 2;
    optional bool another_var               = 3;
}

vs

message foobar {
    optional bool var_one_short = 1;
    optional bool var_two_looooooooong_name = 2;
    optional bool another_var = 3;
}
share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

I think the second version is much easier to maintain. If you have a new variable with a longer name (like var_two_reaalllllly_looooooooong_name) you don't have to modify three other lines too to keep it nice. The first version also could cause unnecessary patch/merge conflicts.

From Code Complete, 2nd Edition by Steve McConnell, p758:

Do not align right sides of assignment statements

[...]

With the benefit of 10 years’ hindsight, I have found that, while this indentation style might look attractive, it becomes a headache to maintain the alignment of the equals signs as variable names change and code is run through tools that substitute tabs for spaces and spaces for tabs. It is also hard to maintain as lines are moved among different parts of the program that have different levels of indentation.

share|improve this answer
    
I think it's important to note that a lot of modern editors support this (example: Sublime Text 2), so maintenance becomes trivial. –  seand Feb 28 '13 at 16:19
1  
I use sublime and yes it makes it easier. But his opinion convinced me. –  fmsf Feb 28 '13 at 16:21
add comment

This is a simple "Readability vs Maintainability" issue. In a case like this, choose the second one. So, if you change your code or add a new variable, you don't have to realign the code at the end. You can achieve readability with other ways like giving your variables good names.

share|improve this answer
add comment

I like more the first solution because it visually groups together the variables on the left side of the = and the value on the right side. Unfortunately I can't back this preference with a strong reason other than personal taste.

In your situation I'd struggle for uniformity across your codebase and define a single convention that every programmer should follow. It seems that you and your colleague are already trying to agree, but I wanted to point it out explicitly because I think that it is very important.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.