A long time ago I have read an article (I believe a blog entry) which put me on the "right" track on naming objects: Be very very scrupulous about naming things in your program.
For example if my application was (as a typical business app) handling users, companies and addresses I'd have a User
, a Company
and an Address
domain class - and probably somewhere a UserManager
, a CompanyManager
and an AddressManager
would pop up that handles those things.
So can you tell what those UserManager
, CompanyManager
and AddressManager
do? No, because Manager is a very very generic term that fits to anything you can do with your domain objects.
The article I read recommended using very specific names. If it was a C++ application and the UserManager
's job was allocating and freeing users from the heap it would not manage the users but guard their birth and death. Hmm, maybe we could call this a UserShepherd
.
Or maybe the UserManager
's job is to examine each User object's data and sign the data cryptographically. Then we'd have a UserRecordsClerk
.
Now that this idea stuck with me I try to apply it. And find this simple idea amazingly hard.
I can describe what the classes do and (as long as I don't slip into quick & dirty coding) the classes I write do exactly one thing. What I miss to go from that description to the names is a kind of catalogue of names, a vocabulary that maps the concepts to names.
Ultimately I'd like to have something like a pattern catalogue in my mind (frequently design patterns easily provide the object names, e.g. a factory)
- Factory - Creates other objects (naming taken from the design pattern)
- Shepherd - A shepherd handles the lifetime of objects, their creation and shutdown
- Synchronizer - Copies data between two or more objects (or object hierarchies)
Nanny - Helps objects reach "usable" state after creation - for example by wiring to other objects
etc etc.
So, how do you handle that issue? Do you have a fixed vocabulary, do you invent new names on the fly or do you consider naming things not-so-important or wrong?
P.S.: I'm also interested in links to articles and blogs discussing the issue. As a start, here is the original article that got me thinking about it: Naming Java Classes without a 'Manager'
Update: Summary of answers
Here's a little summary of what I learned from this question in the meantime.
- Try not to create new metaphors (Nanny)
- Have a look at what other frameworks do
Further articles/books on this topic:
- What names do you find yourself prepending/appending to classes regularly?
- What’s the best approach to naming classes?
- Book: Design Patterns: Elements of Reusable Object-Oriented Software (Hardcover)
- Book: Patterns of Enterprise Application Architecture (Hardcover)
- Book: Implementation Patterns (Paperback)
And a current list of name prefixes/suffixes I collected (subjectively!) from the answers:
- Coordinator
- Builder
- Writer
- Reader
- Handler
- Container
- Protocol
- Target
- Converter
- Controller
- View
- Factory
- Entity
- Bucket
And a good tip for the road:
Don't get naming paralysis. Yes, names are very important but they're not important enough to waste huge amounts of time on. If you can't think up a good name in 10 minutes, move on.
Processor
here, it's ambiguous and could be anything that takes an input and produces an output. Also in similar sense,SubSomething
should be avoided too. – NottageUserDataProcessor
,DateFormatter
,MonetaryConvertor
,UtilityHelper
,ApplicationLauncher
,ExceptionHandler
,URLEncoder
,InputValidator
,ObjectMapper
,PatternChecker
for class 1, andProviderResponse
,UserIDNameCompositeKey
,ConnectionClient
,LocalizedMessage
for class 2. – OndrejXxxService
orUtils
are also good names for me (classes processing data but with a noun name). Convention is important; but the most important, readability. – OndrejUser<Manag|Help|Controll|Process|Handl|>er
contains a procedure calledcreate
, then extract it into its own command class:CreateUser
. you're less likely to hide unrelated concerns inCreateUser
than you are inUserManager
. your lists of procedures, and their complexity, getting out of hand is a smell that something can be extracted into models that don't need any suffix, you just need to find them. a world without -ers is possible. – Planetstruck