Expressions and statements
ES.3
Don't repeat yourself, avoid redundant code
Duplicated or otherwise redundant code obscures intent, makes it harder to understand the logic, and makes maintenance harder, among other problems. It often arises from cut-and-paste programming.
Use standard algorithms where appropriate, instead of writing some own implementation.
Example
void func(bool flag) // Bad, duplicated code.
{
if (flag) {
x();
y();
}
else {
x();
z();
}
}
void func(bool flag) // Better, no duplicated code.
{
x();
if (flag)
y();
else
z();
}
Enforcement
- Use a static analyzer. It will catch at least some redundant constructs.
- Code review
ES.dcl: Declarations
A declaration is a statement. A declaration introduces a name into a scope and might cause the construction of a named object.