Class and Type Naming
You'll learn how to name classes and types so they clearly communicate what each type represents in your codebase.
Why Classes Need Different Naming Rules
In the previous lesson, you learned that functions should use verbs because they do things. Classes, however, are different—they represent things. A class models an entity, concept, or abstraction in your system. Therefore, classes should be named like the things they are: nouns and noun phrases.
Use Nouns and Noun Phrases
Think of classes as blueprints for objects. Just as you'd call a physical object by a noun ("car," "invoice," "customer"), your classes should follow the same pattern:
Customer— notProcessCustomerorCustomerHandlerShoppingCart— notManageCartorCartProcessorPaymentGateway— notProcessPaymentsEmailValidator— a thing that validates (the validator itself)
The name should tell you what it is, not what it does. The methods inside will handle the doing.
Avoid Generic Suffixes
Resist tacking on vague suffixes like Manager, Processor, Handler, or Data. These words are too generic and hide the class's true responsibility:
- ❌
CustomerManager— manages what exactly? - ✅
CustomerRepository— stores and retrieves customers - ❌
DataProcessor— what kind of data? - ✅
InvoiceCalculator— calculates invoices
If you find yourself using these suffixes, pause and ask: "What does this class actually represent?"
Reflect Responsibilities
A well-named class reveals its role in the system. Invoice holds invoice data. InvoicePrinter formats invoices for printing. EmailNotifier sends email notifications. Each name paints a clear picture of responsibility.
Key Takeaway: Name classes with nouns or noun phrases that reflect their specific responsibilities—avoid verbs and generic suffixes that obscure what the type truly represents.