Why attempting to align variables is a bad idea.
The following is a quote from the Learn C++ Lesson 1.8 — Whitespace and basic formatting.
Use whitespace to make your code easier to read by aligning values or comments or adding spacing between blocks of code.
In my opinion, this is very bad advice. The problem is that code changes over time. This advice compels programmers to modify code that is completely unrelated to the functionality of the change that they are currently working on or to accept that over time the code will no longer be aligned in this way which in some ways is worse.
Consider the following scenario.
Revision 1
In Revision 1 of your code you have the following code block.
int cost {57};
int pricePerItem {24};
int value {5};
int numberOfItems {17};
This code is well aligned. All variables are properly initialized. You have no problem getting this past code review.
Revision 2
Your assigned task requires that you add another integer variable named “variableWithAnExtremelyLongName”. How do you do it?
You might be tempted to simply change the above code block to the following.
int cost {57};
int pricePerItem {24};
int value {5};
int numberOfItems {17};
int variableWithAnExtremelyLongName {42};
In some scenarios this change will not pass code review.
The problem
Some organizations have very strict rules requiring that every single line of code that you touch in a given change list be directly related to the current assigned task. Sometimes these organizations may also require that every single line of code you change actually results in a well documented and approved change in functionality.
In other words, you are not allowed to change the other lines in the code block so that the variables continue to be aligned. Thus you would be required to do the following instead.
int cost {57};
int pricePerItem {24};
int value {5};
int numberOfItems {17};
int variableWithAnExtremelyLongName {42};
This is worse than simply not bothering with aligning the variables in the first place!
Some organizations are extremely strict about this. They do not allow a single line of code to change for purely formatting based reasons at all no matter why the changes are being made.
Some organizations are more reasonable. The more reasonable organizations do allow formatting only changes in which you change the formatting of the code or alignment of variables.
For extremely strict organizations attempting to align variables in this case will inevitably lead to the code no longer being aligned the first time that you have to change the code.
For more reasonable organizations, you need to remember to make the formatting only change, go through the review process, and then get started on the assigned task.
This is why in my opinion the following is the only correct way of defining multiple variables.
int cost{57};
int pricePerItem{24};
int value{5};
int numberOfItems{17};
In other words, make no attempt to align the variables. It does not actually make the code that much easier to read and it causes issues with maintaining the code.