Mutable variable at file scope
ID |
c.maintainability.avoid_global_variables |
Severity |
low |
Remediation Complexity |
hard |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Encapsulation |
Language |
C / C++ |
Description
This mutable variable lives at file scope, so any function in the translation unit can read or change it and nothing records who does. That makes the code hard to reason about locally, hard to test in isolation, and unsafe once more than one thread is involved. Pass the state as a parameter, or hide it behind an accessor that owns the invariants.
Rationale
This mutable variable lives at file scope, so any function in the translation unit can read or change it and nothing records who does. That makes the code hard to reason about locally, hard to test in isolation, and unsafe once more than one thread is involved. Pass the state as a parameter, or hide it behind an accessor that owns the invariants.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Mutable variable at file scope
int request_counter = 0;