Declaring variables best practice?

closed account (91vUpfjN)
Hi,

Just trying to find out what the best practice is regarding variables.

I've previously been told that all variables should be defined the moment the scope starts then all the methods underneath them. Though I can't find any evidence of that...in fact I can't find any evidence one way or the other. In other languages I was told to declare variables and then immediately use them. I'd like to learn what the different practices are and why and which in fact I should be following.

Anyone want to give me a place to start looking?

PS. At the moment there are no limitations to what I can choose, I just want my code looking nice and proper :D.
I really depends on what you prefer but I would say that it's a general rule to declare your variables at the top of the function and then define / change them when you need to.

As for methods (or functions) go, these will either be defined above or below the main function (assuming you're just using a single .cpp file). If they are defined below main, you will be required to create function definitions above main.

Typical layout (in my opinion):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int add (int, int);

int main(){
int n1;
int n2;
int sum;

cout<<"Enter number 1: "<<endl;
cin>>n1;
cout<<"Enter number 2: "<<endl;
cin>>n2;

sum=add(n1,n2);
cout<<sum<<endl;

system("pause");

}
int add(int num1,int num2){
return num1+num2;
}
Last edited on
In C++ you should declare variables as late as you can (that is, as deep in scope as you can).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// old C style
void function()
{
    Type1 a, b, c;
    Type2 d;

    if (/* ... */)
    {
        // use a
        // use d
    }
    else
    {
        // use b
        // use c
        // use d
    }
}
// C++ style (and modern C style)
void function()
{
    Type2 d;

    if (/* ... */)
    {
        Type1 a;

        // use a
        // use d
    }
    else
    {
        Type1 b, c;

        // use b
        // use c
        // use d
    }
}


The reason for using the C++ style is that there's a possible extra cost for declaring variables: when Type1 is a class/struct that has a constructor, whenever you declare a Type1 variable (called object) a function is called for it (the Type1 constructor). Function calls are expensive, so you'll waste time creating objects that you may not use.

In the example above, the only variable that is used regardless of the if() condition will be d, while the others can (and should) be put into deeper scope.

If you are programming in C, simply choose whichever style makes your code easier to read. I personally prefer the C++ style (mostly out of habit). You may prefer the old C style, because your code won't be cluttered with declarations, and if you want to see what the type of a local variable is, you'll always find it at the beginning of the function.
Last edited on
closed account (91vUpfjN)
Thanks for that.
Topic archived. No new replies allowed.