dimba
dimba

Reputation: 27581

Function and declaring a local variable

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.

Upvotes: 6

Views: 3908

Answers (6)

5ound
5ound

Reputation: 1199

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).

Upvotes: 0

Dummy00001
Dummy00001

Reputation: 17420

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.

Upvotes: -1

Mark Byers
Mark Byers

Reputation: 838176

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.

Upvotes: 5

Klaim
Klaim

Reputation: 69682

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

Upvotes: 14

James Curran
James Curran

Reputation: 103505

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.

Upvotes: 4

Mark B
Mark B

Reputation: 96241

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.

Upvotes: 2

Related Questions