Skip to main content

Number of Methods (NOM)

v1.0.0
short name: NOM
effort: 10m
default threshold: 10

Number of methods is a quantitative metric that measures the total number of declared methods in a class/extension/mixin/enum/extension type.

High number of methods indicate a class with high complexity, which usually does to many things.

note

This metric does not count inherited methods that are not overridden.

Why Be Cautious

High number of methods can lead to:

  • Difficulty in Testing: More methods can make it difficult to test all possible cases.
  • Increased Complexity: More methods can make it difficult to understand what the class does.
  • Maintainability Challenges: Large classes are harder to update, as changes may affect numerous methods.

How to Address High Number of Methods?

First, understand the responsibility of the class. If the class is doing to many things, consider splitting it into several classes, each with a limited responsibility.

Additionally, check if all methods need to be in that particular class and consider extracting those that do not depend on state as helpers or utilities.

Config Example

analysis_options.yaml
dcm:
metrics:
number-of-methods:
threshold: 10

To set multiple threshold or other common config options, refer to the Configuring Metrics page.

Example

info

To view what contributes to the metric value, generate an HTML report.

❌ Bad: High Number of Methods

// NOM: 12
class UserManager {
void createUser() {}
void updateUser() {}
void deleteUser() {}
void fetchUserData() {}
void validateUser() {}
void assignRole() {}
void revokeRole() {}
void sendNotification() {}
void trackLogin() {}
void trackLogout() {}
void logActivity() {}
void resetPassword() {}
}

✅ Good: Low Number of Methods

// NOM: 3
class UserManager {
void createUser() {}
void updateUser() {}
void deleteUser() {}
}

// NOM: 2
class UserRoleManager {
void assignRole() {}
void revokeRole() {}
}

// NOM: 1
class UserNotificationManager {
void sendNotification() {}
}

// NOM: 3
class UserActivityTracker {
void trackLogin() {}
void trackLogout() {}
void logActivity() {}
}

// NOM: 1
class PasswordManager {
void resetPassword() {}
}