His book Effective C++ Scott Meyersreveals one interesting guide,
Paragraph 26: Postpone definitions of variables as long as possible. This increases the clarity of the program and increases the effectiveness of the program.
The argument that he offers to support the above is that whenever a variable is created or destroyed, we take on some of the costs of creating and destroying the variable. We can have several control flows in which we could return from the function without using the variable defined earlier (at the beginning of the function), and, therefore, may unjustifiably incur the costs of creating an unused variable.
Everything seems logical and really good practice. Based on the background c, I have a tendency to declare my entire variable at the beginning of the function block. Perhaps this is just the background c, but I also feel that all the ads in one place in the function provide easier and better readability.
So, the question is how many of you really adhere to this practice, in everyday programming, or just too redundant to try to follow such a practice.
source
share