views:

260

answers:

7

In the last year and a bit of working on my team's code base I have noticed a steady progression of naming conventions.

For example, there are a lot of classes that are named to express that they are a class that helps you do something.

Here's the ones I've spotted:

MyClassUtil
MyClassFactory
MyClassHelper
MyClassManager
MyClassService

It just seems to me that over time people come up with naming conventions for relatively the same thing and so instead of having everything named in a consistent manner you wind up with a code base that has a bit of every convention. All the new stuff is named based on the latest fad naming convention and so you can pretty much tell the age of a bit of code by what convention was in fashion at the time.

What is the best way to deal with this tendency? Is it really a problem? As these naming fads come into vogue, should one use the latest fad? Should one rename all existing items with the new naming convention? Or should one just accept the variety as something that is inescapable?

+4  A: 

All of the names of classes you've given above indicate to me a striking departure from object-oriented principles. There's no way of telling what "MyClassUtil" or "MyClassService" does. It could be anything. Class naming should be specific, and should relay clearly the actual function of the class. None of these do. The best way to deal with this tendency is to brush up on object oriented programming skills and name the classes accordingly.

Now, it could be that these examples point out the function, within the application architecture, that these classes represent, and your use of "MyClass" is simply a placeholder for something more definitive at runtime, in which case, I wouldn't view these as naming fads, but rather as descriptive indicators of the function of the class itself, with a loose hint of the application's underlying architecture.

David Morton
So in what circumstances would the different naming conventions be applicable. When should one use a Helper vs a Manager vs a Service?
mezoid
Again, this all depends on the architecture the application is programmed with. When you need to differentiate architectural support for a domain business object from the business object itself, suffixes would be acceptable.
David Morton
A: 

I don't really see how Factory or Service fit in to a particular fad...

Factory is a design pattern and if the class really is a factory then it's a perfectly appropriate name.

If a class is a Windows service what's wrong with calling it service?

There isn't a problem unless you find that performing all the rename refactors is too costly even though you really want to do them.

Jason Punyon
but in my code base something listed as a service isn't even a "Windows Service"... often there will be something like a MyClassRetrievalService, MyClassGroupingService, MyClassSomethingElseService etc...
mezoid
+5  A: 

They don't seem like fads... all these names hint at the purpose of the class, and those purposes are different. With programming, it's all in the name, and they should be chosen very carefully. The variety doesn't need to be escaped. The names vary because the purposes of the classes vary.

MyClassUtil -Some utilities for working with MyClass that it didn't come with. Maybe MyClass belongs to a library you're using, but you often use some higher level functions with it and you need somewhere to put them.

MyClassFactory -Creates instances of MyClass in an abstracted way. This allows you to write code that needs MyClass instances. It can get those new instances from a MyClassFactory. This would allow the Factory to modified in future to serve up different specific implementations of MyClass. Maybe under unit testing, the Factory just serves up dummy/mock MyClasses. This means a class that uses the factory can be tested without needing to change it, just change the factory, and voilà you can isolate the class being tested.

MyClassHelper -Ok, I may agree, perhaps this can be more specific. It does something to help with MyClass, but what. Maybe this is a bit similar to MyClassUtil. But, probably MyClassUtil is general functions that work with MyClass, whereas the helper is initialized with a specific instance of MyClass and then can do operations on that one instance. You need a new helper for each MyClass you want to help.

MyClassManager -Maybe this deals with a pool of MyClass instances and stores or orchestrates them. Eg. in a CommunicationsManager, the class would handle wiring together classes that handle talking to a port or connection like ethernet or serial, and a class that deals with the comms protocol being sent over it so it can transport packets, and a class that deals with the messages in those packets.

MyClassService -A service can do things for you, like given a postcode convert it into a grid-reference. Usually a service can resolve to many specific things. With the postcode example, this class might be have implementations that can talk to different web sites to do the conversion.

Scott Langham
Excellent answer. I must admit I'm not fully convinced they are fads..but I had a nagging doubt in the back of my mind...hence the question. As long as I know that each name has a different and specific meaning then I can work with that and name the things I create better.
mezoid
Whether or not the naming is spot on, at least someone is throwing common code in a common class.
Greg Ogle
+4  A: 

If this is pervasive, the team needs to spend some time studying OO design: reading the source code to well-respected OO frameworks, books on design patterns or books such as Evans "Domain Driven Design".

"Util" and "Manager" are often symptoms of poor design - "code smells". So is "Helper" outside of special contexts (Rails apps) where it's well entrenched.

"Factory" and "Service" have precise technical meanings, you can check the code to see if it conforms to those design patterns.

The general remedy is to sit down with the team, and have an explicit discussion about what benefits you're expecting from these naming schemes, what makes sense and what doesn't, and then over the next few months apply refactoring techniques to phase out the names you've all decided are code smells.

Naming is important. It shouldn't be taken lightly, nor is it a subjective matter. True, there is often more than one correct answer to a given naming issue. However, there are seldom many answers consistent with previous choices, which is key.

Morendil
Awesome answer...that really helped me understand things better. So far this answer has helped me learn the most thus far. Thanks!
mezoid
Out of interest, can you recommend any well-respected OO frameworks? In my mind I'm thinking open source tools that I use on a regular basis...like NHibernate or Mono. A lot of people use these so is that a sign of them being respected or are there ones that a models of good OO design?
mezoid
OO is not about frameworks, but about design. When a tool is used by many, it means that many have found it useful for a purpose, but it doesn't say anything about whether the tool promotes good design. (Well, dependency injection frameworks promote good design. For example Guice.)
Esko Luontola
When I wrote "well respected frameworks" I had in mind the Smalltalk implementations of MVC - I studied Dolphin most closely but other variants exist - and the Java core libraries. Stable over time and across several reimplementations.
Morendil
+2  A: 

Renaming the names to better ones and refactoring the code so that each class has a clear responsibility, is recommended. To know what kind of names to use, read Tim Ottinger's article about Meaningful Names.

When a class does only one thing, then giving it a descriptive name is usually easy. Words such as "manager" are vague and may indicate that the class is responsible for doing so many unrelated things, that no simple name is able to describe what the class does. If you can know what the class does just by looking at the name of the class, then the class has a good name.

Esko Luontola
I have a very short version for the hurried programmer, small enough for an index card, at http://agileinaflash.blogspot.com/2009/02/meaningful-names.html
tottinge
A: 

Why not use a static analysis tool to help enforce a set of style and consistency rule?

If you're in the .NET world Microsoft provides a tool called StyleCop

Shawn Miller
A: 

In the classname examples you give does "MyClass" stand for an actual class name, so that you are really seeing names like "PersonnelRecordUtil" or "GraphNodeFactory"? MyClassFactory is a really bad actual name for a class.

DJClayworth