Number of Implemented Interfaces (NOII)
Number of implemented interfaces is a quantitative metric that measures the number of interfaces a class/enum/mixin implements (including direct and indirect interfaces).
High number of interfaces usually indicates classes with too many responsibilities that are hard to understand and maintain.
Why Be Cautious
High number of implemented interfaces can lead to:
- Reduced Readability: Classes with too many implemented interfaces are harder to read and understand.
- Maintainability Challenges: High interface implementation can reduce cohesion and increase coupling, making the codebase more fragile and harder to modify.
- Increased Complexity: A high number of implemented interfaces increases class complexity, making it harder to manage and maintain.
How Can You Improve High Number of Implemented Interfaces?
First, understand the responsibility of the class and each implemented interface. Depending on that, consider splitting the class into smaller classes or reevaluating applicability of each interface.
Config Example
dcm:
metrics:
number-of-implemented-interfaces:
threshold: 5
To set multiple threshold or other common config options, refer to the Configuring Metrics page.
Example
To view what contributes to the metric value, generate an HTML report.
❌ Bad: High Number of Implemented Interfaces
abstract class Printable {
void print();
}
abstract class Savable {
void save();
}
abstract class Shareable {
void share();
}
abstract class Editable {
void edit();
}
// NOII: 4.
class Document implements Printable, Savable, Shareable, Editable {
void print() {}
void save() {}
void share() {}
void edit() {}
}
✅ Good: Low Number of Implemented Interfaces
abstract class Printable {
void print();
}
abstract class Savable {
void save();
}
abstract class Shareable {
void share();
}
// NOII: 3
class Document implements Printable, Savable, Shareable {
void print() {}
void save() {}
void share() {}
}
// Another class can handle editing responsibilities separately.
class EditableDocument extends Document {
void edit() {}
}