Function and declaration of a local variable

Just talking to a colleague at work how to declare variables. For me, I have already decided which style I prefer, but maybe I'm wrong.

Style C is the whole variable at the beginning of the function. If you want to know the data type of a variable, just look at the beginning of the function.

bool Foo() { PARAM* pParam = NULL; bool rc; while (true) { rc = GetParam(pParam); ... do something with pParam } } 

The "C ++" style is to 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 about POD variables.

+4
source share
6 answers

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

  • This allows you to apply the YAGNI principle in your code , since you only declare a variable when you need it, as close to using them as possible. This makes it easier to understand the code, since you do not need to return to the function to understand all this. The type of each variable is basic information about this variable and is not always obvious in the name varaible. In short: code is easier to read .
  • This allows you to better optimize the compilation (when possible). Read: http://www.tantalon.com/pete/cppopt/asyougo.htm#PostponeVariableDeclaration
+12
source

If, due to the language you use, you need to declare variables at the top of the function, then it is clear that you must do this.

If you have a choice, it makes sense to declare variables where they are used. I use the rule: Declare variables with the minimum scope that is required.

Reducing the volume of a variable prevents some types of errors, for example, when you accidentally use a variable outside the loop that is intended to be used inside the loop only. Reducing the size of the variable will allow the compiler to detect an error instead of code that compiles but does not execute at run time.

+4
source

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

In addition, the limited ability for a variable provides compilation of the best options for optimization.

+3
source

This is probably a bit subjective.

I prefer as locally as possible, because it completely clears which area is intended for the variable, and the compiler generates an error if you access it outside the intended useful area.

+1
source

This is not a style issue. In C ++, non-POD types will have their constructors called at the declaration point and destructors called at the end of the scope. You must be wise in choosing where to declare variables, or you will cause unnecessary performance problems. For example, declaring a class variable inside a loop may not be the wisest idea, as the constructor / destructor will be called every iteration of the loop. But sometimes declaring class variables at the top of a function may not be the best if there is a chance that the variable will not be used at all (for example, a variable is used only in some "if" expression).

0
source

I prefer the C style because the C ++ style has one main drawback: in a dense function, it is very difficult to find a variable declaration / initialization. (No syntax highlighting was able to cope with reliability and predictably with my C ++ habits for coding dangers .)

Although I strictly adhere to no style: only key variables are placed in it, and the smallest minor variables live in the block where they are needed (for example, bool rc in your example).

But all the important key variables in my code inevitably end with a declaration from above. And if I have too many local variables in a nested block, this is a sign that I should start thinking about dividing the code into smaller functions.

-1
source

Source: https://habr.com/ru/post/1314875/


All Articles