How is better way to declare variables?

Hello everyone ...
I have a doubt kind of dumb ... but still is a doubt ...

What is the right way / better to declare the variables in my program? All at once at the beginning or as needed?

Ex:
1
2
3
4
5
6
7
8
9
10
...
int a,b,c,z;

a=10;
...
b=SOME_VALUE;
...
...
z= a+b*c/d;


or...:

1
2
3
4
5
6
7
8
9
10
11
12
...
int a;


a=10;
...
int b;
b=SOME_VALUE;
...
...
int z;
z= a+b*c/d;


Thanks....
Delay your variable definitions until as late as possible, if they're only needed inside of an if statement for example then declare them inside of the if statement's body, this way, if the if statement evaluates to false then the objects are not created, saving time and memory.
Last edited on
For me, the first one, declaring all variables all at once makes your program neat and easy to understand, if you not properly manage your code you end up a lot of problems later.

http://codewall.blogspot.com
Last edited on
Scott Meyers backs me up, that alone is enough of a reason to take my advice. God I love Scott Meyers.
"if the if statement evaluates to false then the objects are not created, saving time and memory."

If you don't initialize a stack allocated variable and it's a POD (Plain Old Data) type with no constructor then there is literally 0 overhead.

I do agree with you on declaring stuff as near to the points of use as possible, but that point isn't quite correct.
If you delay declaration you cannot use an uninitialized value:
1
2
3
4
5
6
7
8
9
10
bool a;


//...
if (a) //oops. Used "a" before defined! Intended as a random generator?
{
//...
}
//...
a = true ; // you intend to use "a" from here on 


If you define close to first usage:
1
2
3
4
5
6
7
8
9
10
//bool a;


//...
if (a) //oops. Used "a" before defined declared! => Compile time error!
{
//...
}
//...
bool a = true ; // you intend to use "a" from here on 




Other reasons to define a variable close to use:
* if used inside a block, it limits access to this variable (remember: grant access to a variable to the least extend possible; that's why you should use globally defined variables sparingly)
* if used in an inner block, resources get also released earlier
* You don't have uninitialized values.

Declaring all variables in the first lines is a relict from C. Don't do it.
Last edited on
I think that it depends, and you should just learn which one is better on your own. On a case by case basis.

If you write a very complicated long function, which demands sparsely located definitions, then I think you are in need of re-factoring. Functions should be shorter and neater in which case the access to a variable is almost always immediately after the definition.

It is true that definition and initialization should not be separate. This is devious code and calls for errors.

As for performance - it is nowhere near obvious which approach is better. For user types with complicated construction process, placing the definition in one of the program's branches can save time. Sometimes it is even necessary, if there is a side effect from the constructor. For POD types, it is not really worth it. This way, a dumb compiler may end up allocating stack space incrementally instead of at once. If it is optimizing, then it will probably allocate the stack space in one go and perform initialization when the definition is reached, but memory efficiency will be the same as without nested blocks.

I am saying that readability and clarity are your top priority, and there is no silver bullet there. So, if you keep your functions simple and make all initializations at the beginning, I claim you wont go wrong. Try to not ever separate the initialization and the definition by code, though, as the others also advised. And if you think that the function has two parts that are logically separate, like two cases of some algorithm, then sure, use variable definitions for each case.

(This is a bit like the debate on whether you should have more than one exit point from a block of code. Like mid-function return-s, or mid-loop break-s. The advice is that you shouldn't (; according to the principles of structured programming). But make the code intelligible and piece-wise simple, and the rest is generally a secondary issue.)
Last edited on
I stand corrected, cheers Stravant.
Hmmm... I ever declare my variables just when I need him and incialize it as fast possible to avoid problems...

Thanks for all guys =D Happy New Year!



Sorry for bad english... =D
I recommend delaying the declaration as long as possible and initializing them in the same statement when applicable. For example:
1
2
3
4
5
6
//...
int a = 10;
//...
int b = SOME_VALUE;
//...
int z = a+b*c/d;


In the case of only needing the local inside a block, you can also do this:
1
2
3
4
5
int f() { return SOME_NUMBER; }
//...
if( int n = f() ) {
    // n is both initialized and local to this scope
}
Topic archived. No new replies allowed.