There are coding guidelines at all development shops. Some codify the
source location for declarations such that they are declared at the
beginning of any basic block (e.g. following a '{') instead of interspersing
declarations willy-nilly throughout a function. While C++ allows
willy-nilly declarations, it in no way requires them,
Usually, in good C++ code declarations are neither collected in one place, nor placed willy-nilly, which both are context-independent strategies that disregard the requirements of the code hand.
Rather, in good code the declarations support the code's requirements, and the scope of any variable is reduced to its practical minimum in order to reduce the number of possible interactions and data flows, and also some times in order to get constructor and/or destructor calls properly placed.
The main rationale for using "const" is roughly the same, to reduce complexity by reducing possible interactions that otherwise would have have to be considered, so in code with declarations placed independent of the code requirements I expect to not see much "const" usage either, and v.v.
and many consider
them to be "confusing", particularly in larger function bodies.
A solution is to not have larger function bodies, for whatever measure of "larger" that is problematic.
A 1200-line function can be OK -- I've maintained one such, happily not mine!, it was a systematic list of cases.
An 8-line function can be complete spaghetti.
However, chances are that the larger the function, the less its programmer or maintainers knew about abstraction, so it will much more likely be spaghetti. Which, interacting with the size, creates a maintenance hell. Which in turn causes code to be just copied around, replicating whatever bugs there are and creating more of the original problem, and so on, in a vicious circle.
When declared
at the start of a block, the maintenance programmer will always know exactly
where to look in a function for the declaration,
Presumably you're not talking about introducing a new block every place a new variable is needed.
If you do, however, then I roughly agree, because that /restricts the scope/ of each variable. It can however be overdone. Easily.
But for the case of collecting declarations at the start of a function or at the start of some main block, I think that finding declarations is what the right-click "go to declaration" is for. ;-)
An up-front group of declarations placement thus fails to have an advantagein C++, but it does have a severe disadvantage.
Namely, the grouping of declarations at the start of a main block means that those variables can be changed everywhere in the block, thus wasting eachmaintenance programmer's time by the need for analyzing the data interactions, the way statements in that block can influence each other via the variables.
and many editing tools have
short-cuts to move the cursor to the start of a block ('[[' in vi).
And the good ones even let you go directly to a declaration. ;-)
So there is no law either way; it is purely a matter of preference and YPMV.
Not quite.
In C++ it's some times important where constructors and/or destructors are executed. This can be due to functional requirements, or due to efficiency considerations, or maybe reasons I fail to recall right now (it's late). For these cases it's crucial to restrict the scope of a variable appropriately.
For example, if a constructor can throw, then you might need that declaration inside a try-block if you want to catch the possible exception, or outside the try block if you want to decidedly not catch that possible exception.. Even though this is a rare situation -- with C++ RAII try blocks are generally rare -- it is important for correctness. Similarly for efficiency..
Cheers & hth.,
- Alf