Why is this an issue?
When two methods have the same implementation, either it was a mistake - something else was intended - or the duplication was intentional, but may
be confusing to maintainers. In the latter case, one implementation should invoke the other.
This rule raises an exception when two methods implemented inside the class definition share the same implementation.
Noncompliant code example
class Point {
int x;
int y;
// .....
public:
void setX(int v) {
if (v >= 0 && v < MAX_X) {
x = v;
return;
}
error();
}
void setY(int v) { // Noncompliant
if (v >= 0 && v < MAX_X) {
x = v;
return;
}
error();
}
};
Compliant solution
class Point {
int x;
int y;
// .....
public:
void setX(int v) {
if (v >= 0 && v < MAX_X) {
x = v;
return;
}
error();
}
void setY(int v) {
if (v >= 0 && v < MAX_X) {
y = v;
return;
}
error();
}
};
Exceptions
Empty methods, methods with the same name (overload) and methods with only one statement are ignored.