views:

127

answers:

6

Just having an conversation with collegue at work how to declare a variables. For me I already decided which style I prefer, but maybe I wrong.

"C" style - all variable at the begining of function. If you want to know data type of variable, just look at the begining of function.

bool Foo()
{
    PARAM* pParam = NULL;
    bool rc;

    while (true)
    {
       rc = GetParam(pParam);
       ... do something with pParam
    }
}

"C++" style - declare variables as local as possible.

bool Foo()
{       
    while (true)
    {
        PARAM* pParam = NULL;

        bool rc = GetParam(pParam);
       ... do something with pParam
    }
}

What do you prefer?

Update The question is regarding POD variables.

+1  A: 

This is probably a bit subjective.

I prefer as locally as possible because it makes it completely clear what scope is intended for the variable, and the compiler generates an error if you access it outside the intended useful scope.

Mark B
+4  A: 

If due to the language you are using you are required to declare variables at the top of the function then clearly you must do this.

If you have a choice then it makes more sense to declare variables where they are used. The rule of thumb I use is: Declare variables with the smallest scope that is required.

Reducing the scope of a variable prevents some types errors, for example where you accidentally use a variable outside of a loop that was intended only to be used inside the loop. Reducing the scope of the variable will allow the compiler to spot the error instead of having code that compiles but fails at runtime.

Mark Byers
+2  A: 

I prefer the "C++ style". Mainly because it allows RAII, which you do in both your examples for the bool variable.

Furthermore, having a tight scope for the variable provides the compile better oppertunities for optimizations.

James Curran
+1 for reference to RAII
dkackman
+10  A: 

The second one. (C++ style) There are at least two good reasons for this:

  1. This allow you to apply the YAGNI principle in the code, as you only declare variable when you need them, as close as possible to their use. That make the code easier to understand quickly as you don't have to get back and forth in the function to understand it all. The type of each variable is the main information about the variable and is not always obvious in the varaible name. In short : the code is easier to read.
  2. This allow better compiler optimizations (when possible). Read : http://www.tantalon.com/pete/cppopt/asyougo.htm#PostponeVariableDeclaration
Klaim
A: 

I prefer C style because the C++ style has one major flaw to me: in a dense function it is very hard on eyes to find the declaration/initialization of the variable. (No syntax highlighting was able yet to cope reliably and predictably with my C++ coding hazards habits.)

Though I do adhere to no style strictly: only key variables are put there and most smallish minor variables live within the block where they are needed (like bool rc in your example).

But all important key variables in my code inevitably end up being declared on the top. And if in a nested block I have too much local variables, that is the sign that I have to start thinking about splitting the code into smaller functions.

Dummy00001
The main variables of a function are the variables you have to initialize right at the very begining.Am I correct?
the_drow
@Dummy00001: It's easy to find variables. They're right next to where they're needed/are used. You don't need to look for them.
DeadMG
When I need to find the declaration of the variable, I just Ctrl+click its name. It works with all coding habits, including using some insane template metaprogramming and Boost. You just need to use the right IDE ;)
Pavel Minaev
@DeadMG: have you never dealt with a function spanning several screens? there is no guarantee that the declaration is on screen. and advantage of keeping declarations somehow grouped together is that they are much easier to spot without any mouse interventions.
Dummy00001
@the_drow: I would classify as main variables whatever has direct impact on the output of the function. The variables which I have to keep in mind all the time I work with the function. How/when they are initialized is secondary to me.
Dummy00001
@Pavel Minaev: `#`/`*` (+ `:set hls!`) in the VIM :D
Dummy00001
A: 

This isn't a style issue. In C++, non-POD types will have their constructors called at the point of declaration and destructors called at the end of the scope. You have to be wise about selecting where to declare variables or you will cause unnecessary performance issues. For example, declaring a class variable inside a loop may not be the wisest idea since constructor/destructor will be called every iteration of the loop. But sometimes, declaring class variables at the top of the function may not be the best if there is a chance that variable doesn't get used at all (like a variable is only used inside some 'if' statement).

5ound
The question is mostly for POD variables. I'll update the question
dimba