
Variables and functions should be declared in the minimum scope from which all references to the identifier are still possible.
By using a larger scope than is necessary, code becomes less readable, harder to maintain, and more likely to reference unintended variables . Minimizing scope requires that developers declare variables and functions where they will be used, improving maintainability(see DCL01-C. Do not reuse variable names in subscopes).
Noncompliant Code Example
...
In this noncompliant code example, the function counter()
increments the global variable count
and then returns immediately if this variable exceeds a maximum valueThe variable maximum
is declared as a static variable outside of the function findArrayMax
although it is not necessary. Thus, maximum
may not be properly initialized and findArrayMax
may not work as intended if called more than once. Additionally, if any other part of the program modifies maximum
then it may cause faulty behavior in findArrayMax
.
Code Block | ||
---|---|---|
| ||
static unsigned int maximumcount = 0; /* function finds max value in an array of ints */ public function findArrayMax(int *values)void counter() { ... if if(values[i](count++ > maximumMAX_COUNT){ maximum = values[i] return; } /* ... */ } |
Assuming that the variable count
is only accessed from this function, this example is noncompliant because it does not define count
within the minimum possible scope.
Compliant Solution
The maximum
variable's scope is only in the function that uses it. As a result, it is properly initialized each time the function is called. This also removes the possibility that another part of the program can modify the variable and alter the function's operationsIn this compliant solution, the variable count
is declared within the scope of the counter()
function as a static variable. The static modifier, when applied to a local variable (one inside of a function), modifies the lifetime (duration) of the variable so that it persists for as long as the program does, and does not disappear between invocations of the function.
Code Block | ||
---|---|---|
| ||
/* function finds max value in an array of ints */ public function findArrayMax(int *values) void counter() { static unsigned int maximumcount = INT_MIN0; ... if(values[i]if (count++ > maximumMAX_COUNT){ maximum = values[i] return; } /* ... */ } |
The keyword static
also prevents re-initialization of the variable.
Noncompliant Code Example
...
The counter variable i
is declared outside of the for
loop. This goes against this recommendation because it is not declared in the block in which it is used. If this snippet were reused with another index variable j
but there was a previously declared variable i
the loop could iterate over the wrong variable.
Code Block | ||
---|---|---|
| ||
public void doStuff(...){ intsize_t i = 0; for (i=0; i < 10; i++){ /* Perform Operationsoperations */ } } |
Compliant Solution
Complying with this recommendation requires that you declare variables where they will be are used, thus improving readability and reusability. In this example, this would be done by declaring the loop's index variable i
within the initialization of the for
loop. This was recently relaxed in the C99 standard.
Code Block | ||
---|---|---|
| ||
public void doStuff(...){ for (intsize_t i=0; i < 10; i++) { /* Perform Operationsoperations */ } } |
Risk Assessment
Failure to minimize scope could result in less reliable, readable, and reusable code.
...