Number of Added Methods (NOAM)
Number of added methods is a quantitative metric that measures the total number of methods that a class defines on its own, separate from those it inherits from parent classes.
This metric is calculated only for non-abstract classes that extend another class.
Why Be Cautious
High number of added methods can lead to:
- Increased Complexity: More methods mean more logic, making it harder to understand and work with the class.
- Maintainability Challenges: With more methods, maintaining and updating the class becomes more difficult and error-prone.
- Reduced Reusability: Large classes with many methods are often harder to reuse effectively because they may handle too many responsibilities.
How to Address High Number of Added Methods?
Define common behaviors in interfaces or abstract classes. Implementing these interfaces allows classes to adhere to contracts without inflating NOAM unnecessarily.
For methods that are closely related, consider encapsulating them in a separate class or module. For instance, if a class has multiple methods related to data validation, a Validator class could handle those methods instead.
Before adding new methods, consider whether the functionality could be inherited from a parent class or handled through composition with helper classes. This approach can reduce NOAM and increase modularity.
Config Example
dcm:
metrics:
number-of-added-methods:
threshold: 10
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 Added Methods
class BasicCalculator {
void add(int a, int b) => print(a + b);
void subtract(int a, int b) => print(a - b);
}
// NOAM: 8
class AdvancedCalculator extends BasicCalculator {
void multiply(int a, int b) => print(a * b);
void divide(int a, int b) => print(a / b);
void modulus(int a, int b) => print(a % b);
void power(int a, int b) => print(a ^ b);
void squareRoot(int a) => print(a.sqrt());
void log(int a) => print(math.log(a));
void factorial(int a) => print(calcFactorial(a));
// Overridden method
void add(int a, int b) {
print("Advanced Add: ${a + b}");
}
int calcFactorial(int a) {
// Logic to calculate factorial
return 1; // Example return
}
}
✅ Good: Low Number of Added Methods
class BasicCalculator {
void add(int a, int b) => print(a + b);
void subtract(int a, int b) => print(a - b);
}
// NOAM: 2
class AdvancedCalculator extends BasicCalculator {
void multiply(int a, int b) => print(a * b);
void divide(int a, int b) => print(a / b);
// Overridden method
void add(int a, int b) {
print("Advanced Add: ${a + b}");
}
}
class ScientificCalculator {
void modulus(int a, int b) => print(a % b);
void power(int a, int b) => print(a ^ b);
void squareRoot(int a) => print(a.sqrt());
void log(int a) => print(math.log(a));
}
class FactorialCalculator {
void factorial(int a) => print(_calcFactorial(a));
int _calcFactorial(int a) {
// Logic to calculate factorial
return 1; // Example return
}
}