Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice when Declaring and Initializing variables in c++ [closed]

As I am just starting with c++, I am trying to keep my code clean, readable and correct.

I have a question as to what is the best practice when declaring and initializing variables.

In Programming principles and practice in c++ by Bjarne Stroustrup, he advised to always initialize your variables if you can. That I understand.

What wasn't mentioned was the correctness of declaring variables in a single line statement like this:

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

as against using three different statements like this:

  int value1 = 0; 

  int value2 = 0;

  int value3 = 0; 

Of course the second way is more readable and probably manageable, but the first is kind of a quick way, which I suppose is not advised? Which is best practice and why? Does it affect compilation in any way?

Thanks.

like image 799
Onome Sotu Avatar asked Feb 17 '17 16:02

Onome Sotu


People also ask

Should you always initialize variables in C?

Variables and data structures must be defined and initialized before they can be used to store data.

Is it good practice to initialize variables?

Summary. Start with a clean slate: Uninitialized variables are a common source of bugs in C and C++ programs. Avoid such bugs by being disciplined about cleaning memory before you use it; initialize variables upon definition.


2 Answers

When I started to learn code it helped me to separate variable upon declaration. It helped me keep the variables organized by having them on separate lines. There is no execution difference in the way these two lines are coded. However, as you rise through the programming ranks your instructors will probably advise this format.

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

Once you start working on large projects it helps to keep sections of your code as compact as possible. In this declaration having 1 line vs 3 keeps more tidy code. If you were doing this declaration hundred of times, 1 versus 3 lines of code can really add up.

like image 53
Luke Shinn Avatar answered Sep 29 '22 16:09

Luke Shinn


In terms of compilation speed, there is no difference between the 2 methods; the difference lies more in the styling of the code. Having said that, declaring multiple variables of the same data type is more prone to errors if the variables are pointers. I'll go over the 2 methods below:


Method 1 is more prone to errors in some situations

When you declare variables of the same data type in a single line, like the example you provided: int value1 = 0, value2 = 0, value3 = 0;, this is more prone to errors. For example, if you wanted to do declare 3 pointer variables of type int, then the following:

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

would not be the right way to declare them, as it would mean that the first variable is a pointer, but the second and third ones are non-pointers of type int; the above line could be rewritten as:

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

Which 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 as declaring them in the same line will not have the intended effect.

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

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

This would solve the problem of pointer declaration and only differ from separate lines by style of code.

Method 2 is more pleasing to read (this is opinion-based)

Another thing is that the second method of declaring your variables in separate lines improves the readability of the code. Note here that this is opinion-based; some people that are more used to reading them on the same line, and in that case, method 1 is better for them.


So to conclude, after you are sure that declaring the variables on the same line will not affect the meaning of your code (sorry for poor choice of words here) like it did in the example in the first header, then it boils down to the style of the code you prefer. As for execution time, it really makes no difference.

like image 35
BusyProgrammer Avatar answered Sep 29 '22 16:09

BusyProgrammer