This recommendation is part of the open catalog of best practice rules for performance that is automatically detected and reported by Codee.
Issue
A scalar variable should be declared in the smallest scope possible. In computer programming, the term scope of a variable usually refers to the part of the code where the variable can be used (e.g. a function, a loop). During the execution of a program, a variable cannot be accessed from outside of its scope. This effectively limits the visibility of the variable, which prevents its value from being read or written in other parts of the code.
Relevance
Explicitly declaring scalar variables in the smallest scope possible makes it easier for the compiler to track its usage. By minimizing the number of statements where the value can be modified, it is easier to diagnose why a variable is taking an erroneous value. Additionally, it reduces the likelihood of reusing variables for multiple, incompatible purposes, making code testing significantly easier.
Additionally, in the context of parallel programming, one of the biggest challenges is to do data scoping correctly and decide on how to manage all the variables of the code properly. For example, it is very important to determine if a variable needs to be declared as private to the threads or if it needs to be shared among all the threads. Not doing so may introduce race conditions during the parallel execution, which makes the result of the code wrong in unpredictable ways. Explicitly declaring variables in the smallest scope possible will also simplify data flow analysis, which may be difficult, time-consuming and error-prone especially in big code bases that contain many function calls spread along multiple files.
Actions
Move the declaration to the smallest possible scope.
Code example
In the following code, the function foo
declares a variable t
used in each iteration of the loop to hold a value that is then assigned to the array result
. The variable t
is not used outside of the loop.
void foo() {
int t;
int result[10];
for (int i = 0; i < 10; i++) {
t = i + 1;
result[i] = t;
}
}
In this code, the smallest possible scope for the variable t
is within the loop body. The resulting code would be as follows:
void foo() {
int result[10];
for (int i = 0; i < 10; i++) {
int t = i + 1;
result[i] = t;
}
}
From the perspective of parallel programming, moving the declaration of variable t
to the smallest possible scope helps to prevent potential race conditions. For example, in the OpenMP parallel implementation shown below there is no need to use the clause private(t)
, as the declaration scope of t
inherently dictates that it is private to each thread. This avoids potential race conditions because each thread modifies its own copy of the variable t
.
void foo() {
int result[10];
#pragma omp parallel for default(none) shared(result)
for (int i = 0; i < 10; i++) {
int t = i + 1;
result[i] = t;
}
}
Related resources
- PWR002 examples at GitHub
- G.J. Holzmann (2006-06-19). “The Power of 10: Rules for Developing Safety-Critical Code”. IEEE Computer. 39 (6): 95–99. doi:10.1109/MC.2006.212. See Rule 6: “Declare all data objects at the smallest possible level of scope”. [last checked May 2019]
References

Building performance into the code from day one with Codee