Skip to main content

Number of Implemented Interfaces

The number of interfaces implemented by a class is an important metric for understanding its responsibilities and complexity. Implementing multiple interfaces can promote code reuse and flexibility, but excessive implementation may indicate potential issues.

A recommended NOII (Number of Implemented Interfaces) is 3 or less.

Config Example

To configure the number-of-implemented-interfaces metric in DCM, you can specify the maximum number of interfaces a class is allowed to implement.

dart_code_metrics:
...
metrics:
...
number-of-implemented-interfaces: 5
...

In this configuration:

  • The number-of-implemented-interfaces is set to 5. You will see the measured indication by DCM which are in different metric levels including [below, near, high, very high] and if the value is 2x (or above) of the config, it will be marked as very high.
  • Adjust this value based on your project's specific requirements and complexity guidelines.

Why Be Cautious About NOII

There are several reasons you might want to be careful about NOII (Number of Implemented Interfaces) including:

  • Complexity: A high number of implemented interfaces increases class complexity, making it harder to manage and maintain.
  • Single Responsibility Principle (SRP): Implementing many interfaces may violate SRP, suggesting the class has too many responsibilities.
  • Cohesion and Coupling: High interface implementation can reduce cohesion and increase coupling, making the codebase more fragile and harder to modify.
  • Maintainability and Readability: Classes with too many implemented interfaces are harder to understand and maintain, impacting readability and ease of understanding.

How Can You Improve?

  • Evaluate Responsibilities: Regularly assess the class to ensure it adheres to the SRP.
  • Refactor When Necessary: Split the class into smaller, more focused classes if it implements too many interfaces.
  • Interface Segregation Principle (ISP): Create smaller, more specific interfaces to keep class implementations clean and manageable.
  • Design Review: Conduct regular design reviews to maintain a clean and maintainable architecture.

Example

❌ Bad: Violating the Recommended Number of Implemented Interfaces (NOII)

abstract class Printable {
void print();
}

abstract class Savable {
void save();
}

abstract class Shareable {
void share();
}

abstract class Editable {
void edit();
}

// This class implements more than the recommended NOII of 3 interfaces.
class Document implements Printable, Savable, Shareable, Editable {

void print() {}


void save() {}


void share() {}


void edit() {}
}

✅ Good: Adhering to the Recommended Number of Implemented Interfaces (NOII)

abstract class Printable {
void print();
}

abstract class Savable {
void save();
}

abstract class Shareable {
void share();
}

// This class implements a manageable number of interfaces, adhering to the NOII recommendation.
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() {}
}