Best practice when declaring and initializing variables in C ++

Since I'm just starting out with C ++, I try to keep my code clean, readable, and correct.

My question is what is the best practice when declaring and initializing variables.

In the Principles and Practice of Programming in C ++, Bjarne Straustup, he advised always to initialize his variables, if possible. I understand it.

The correct declaration of variables in a single string expression was not mentioned as follows:

int value1 = 0, value2 = 0, value3 = 0; 

unlike three different operators:

  int value1 = 0; int value2 = 0; int value3 = 0; 

Of course, the second way is more readable and probably manageable, but the first is a quick way, which, I believe, is not recommended? What is best practice and why? Does this affect compilation in any way?

Thanks.

+5
source share
3 answers

In terms of compilation speed, there is no difference between the two methods; the difference is more in code style. Having said that, declaring multiple variables of the same data type is more error prone if the variables are pointers. I will consider two methods below:


Method 1 is more error prone in some situations

When you declare variables of the same data type on the same line, for example, the example you cited: int value1 = 0, value2 = 0, value3 = 0; This is more error prone. For example, if you want to declare 3 pointer variables of type int, then the following:

 int* value1 = 0, value2 = 0, value3 = 0; 

it would not be the right way to declare them, since this would mean that the first variable is a pointer, and the second and third are not int type pointers; the above line can be rewritten as:

 int* value1 = 0; int value2 = 0; int value3 = 0; 

This is not what we want; we want to do this:

 int* value1 = 0; int* value2 = 0; int* value3 = 0; 

Therefore, in this case, the variables in separate lines declaring them in one line will not have the expected effect.

NOTE. If you really want to declare them on the same line, you can do the following:

 int* value1 = 0; int* value2 = 0; int* value3 = 0; 

This will solve the problem of declaring a pointer and will differ only from individual lines by type of code.

Method 2 is more enjoyable to read (based on opinions)

Another thing is that the second method of declaring variables in separate lines improves the readability of the code. Please note that this is opinion based; some people who are more used to reading them on one line, in which case method 1 is better for them.


So, if you are sure that declaring variables in one line will not affect the meaning of your code (sorry for the poor choice of words here), as it was in the example in the first heading, then it boils up to the style of the code that you prefer. As for runtime, it really doesn't matter.

+4
source

When I started to study the code, it helped me highlight the variable when declaring. This helped me save the variables by organizing them in separate lines. There is no difference in the performance of the encoding method of these two lines. However, as you get to the programming level, your instructors will probably recommend this format.

  int value1 = 0, value2 = 0, value3 = 0; 

Once you start working on large projects, this will help simplify the section of your code as much as possible. This ad, which has 1 vs 3 line, saves more accurate code. If you have made this expression hundreds of times, 1 and 3 lines of code can really add up.

+5
source

Both are beautiful, and there is no real effect on compilation speed or emitted code. The big question is why first of all you need three integer variables. You want your functions to be as short as possible (but not shorter), which means that you often won't have three or more variables.

+3
source

Source: https://habr.com/ru/post/1264354/


All Articles