Why is this an issue?
If a private
field is declared but not used in the program, it can be considered dead code and should therefore be removed. This will
improve maintainability because developers will not wonder what the variable is used for.
Noncompliant code example
class MyClass {
private:
int foo = 42; // Noncompliant, foo is unused
public:
int compute(int a) {
return a * 42;
}
};
Compliant solution
class MyClass {
public:
int compute(int a) {
return a * 42;
}
};