Sunday 31 January 2016

design patterns - Naming Classes - How to avoid calling everything a "Manager"?




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:





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.



Answer



I asked a similar question, but where possible I try to copy the names already in the .NET framework, and I look for ideas in the Java and Android frameworks.



It seems Helper, Manager, and Util are the unavoidable nouns you attach for coordinating classes that contain no state and are generally procedural and static. An alternative is Coordinator.



You could get particularly purple prosey with the names and go for things like Minder, Overseer, Supervisor, Administrator, and Master, but as I said I prefer keeping it like the framework names you're used to.




Some other common suffixes (if that is the correct term) you also find in the .NET framework are:




  • Builder

  • Writer

  • Reader

  • Handler

  • Container


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...