views:

94

answers:

3

Hi,

I did not find any suitable questions answered yet, so I'd like to know what is "better" C++ style in the mean of performance and/or memory.

Both codes are inside a method. The question is: When to declare long prio? And what are the implications?

Code 1

while (!myfile.eof())
{
   getline(myfile, line);
   long prio = strtol(line); // prio is declared here
   // put prio in map...
   // some other things
}

Code 2

long prio; // prio is declared here
while (!myfile.eof())
{
   getline(myfile, line);
   prio = strtol(line); 
   // put prio in map...
   // some other things
}
+3  A: 

There is no difference in performance in this case. If you compare the generated code, it will very likely be the same for both cases.

I think the most common style is to declare the variable as close to its first use as possible, but as with all matters of style, it can be very subjective what is "best".

As others have mentioned, if possible it's better to confine each variable to as tight a scope as possible. Doing so reduces the risk of the variable being used in the wrong context, overwritten by mistake, and so on. It also reduces the amount of code you need to read in order to figure out where a variable is being used, which aids understanding of the code.

One advantage of declaring late is that for cases like these, where the value read is not modified during the rest of the body, it can be made const which helps clarity and readability. Since a const variable cannot be assigned to after being declared, this only works in the former style.

unwind
The rule of thumb going with this is: try to define it in the smallest scope possible. This prevents it from accidentally being used in another place.
Thirler
+1 for suggesting to declare as close to the use as possible. Not only is it safer (because it will go out of scope where and become inaccessible where it isn't needed), but it makes the code infinitely more readable. It is also better, because the variable is declared and initialized in a single statement.
Michael Aaron Safyan
+1  A: 

It is always better to limit the scope of the variable to where it is being accessed. in this case its the while loop.

And if you are worrying whether prio will be declared each time the loop runs - :) - I can assure you that wont be the case (declaration is not a part of runtime code). there is no performance overhead in this.

SysAdmin
+1  A: 

There is no difference in terms of performance, but IMHO, the former (declaring within the scope in which the varaible is used, and using an initializer in the declaration of the variable) is preferred.

The advantages to declaring variables closest to where they will be used are:

  • They go out of scope when no longer needed, which means that:
    • They won't be improperly accessed where they aren't used, and:
    • They will be destructed when no longer used (if it were a user-defined type with a destructor)
  • The declaration is visible in the code near where it is used, making it more readable
  • The use of an initializer in the declaration leaves no doubt that it is initialized before use
Michael Aaron Safyan