Naming Classes - How to avoid calling everything a `<WhatEver>Manager`?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
As software projects grow in complexity, it's crucial to maintain a clean and understandable codebase. Effective naming conventions for classes are a fundamental part of this, especially to avoid generic and potentially confusing names such as <WhatEver>Manager. Understanding the purpose of the class and the role it plays in the architecture can lead you to more precise, meaningful, and maintainable class names.
The Problem with "Manager" Names
The suffix “Manager” in class names is notoriously overused and can be vague. It often fails to communicate the specific responsibilities of the class, leading to several problems:
- Ambiguity: It's unclear what the class actually manages.
- Overloading: A "Manager" might become a dumping ground for methods that don’t clearly belong elsewhere.
- Maintainability Issues: New developers or even returning developers may find it difficult to understand the class's role in the project.
Strategies for Better Naming
Here’s how you can improve your class naming and avoid the generic 'Manager' trap:
1. Focus on Purpose
Understand what the central purpose of the class is. For instance, a class handling user authentication can be named AuthenticationService instead of UserManager. This gives a clear indication that the class provides a service related to authentication.
2. Use Domain-Specific Language
Using terms that reflect the domain of the application can make names more intuitive and aligned with the business. For example, in a banking application, instead of AccountManager, consider AccountLedger for handling accounting entries.
3. Be Specific About Behavior
If the class is designed to perform a specific behavior, name it so. A class designed to periodically clean up outdated records could be called RecordCleaner or ExpiredRecordSweeper rather than a generic CleanupManager.
4. Reflect Patterns or Frameworks
If your class represents a design pattern, include this in the name. For example, a class that uses the Singleton pattern could end with Singleton. However, ensure that the inclusion of the pattern in the name is justifiable and not just a way to sound sophisticated.
5. Make Use of Verbs for Operations
Classes that perform specific operations might have names that include verbs, suggesting action. Instead of DataManagement, opt for SaveData or DataSaver if the class primarily saves data.
6. Combine Approaches
Often, a combination of the above strategies will result in the most descriptive and useful class names. For example, EmailNotificationSender clearly describes a class that sends email notifications.
Practical Examples
Consider the following refactorings from generic to specific names:
- Before:
NotificationManager - After:
EmailAlertDispatcher - Before:
UserDataManager - After:
UserProfileAccessor - Before:
ProjectManager - After:
ProjectTimelineCoordinator
Summary Table of Naming Strategies
| Strategy | Description | Example Before | Example After |
| Focus on Purpose | Name based on the class's purpose. | UserManager | AuthenticationService |
| Use Domain-Specific Language | Use terms from the application's domain. | AccountManager | AccountLedger |
| Be Specific About Behavior | Name based on specific behavior. | CleanupManager | ExpiredRecordSweeper |
| Reflect Patterns/Frameworks | Include design pattern or framework. | CacheManager | CacheSingleton |
| Use Verbs | Include verbs if the class performs operations. | DataManager | DataSaver |
Conclusion
By moving away from names like “Manager” and choosing names that reflect the specific responsibilities, behavior, or domain context of the class, you contribute significantly to the maintainability and understandability of your code. Such practices not only aid in cleaner coding but also in smoother onboarding and collaboration among developers.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.