views:

1389

answers:

22
+20  Q: 

Smelly class names?

In your experience, what are some "smelly" keywords in class or function names that might be warning signs of bad object-oriented design?

I've found that classes containing the word Manager or Base are often suspect. For example, a FooManger often indicates poor encapsulation or a singleton design that is difficult to reuse.

A FooBase class is typically an abstract base class that is never expected to be directly referenced in caller code. It's only used to share some code implementation without a true IS-A relationship. And the Base class name exposes internal implementation details and does not reflect a "real world" object in a Domain Model.

Functions that include the word And or Or (e.g. DoThisAndThat() or DoThisOrThat()) are smelly. The function is probably lack cohesion and is trying to do too much. And the name exposes internal implementation details.

+7  A: 

I don't agree with you on the Base point. For example, in ASP.NET (both WebForms and MVC) it is not at all uncommon with base classes for a lot of different things - the most common use I've seen is for methods that you want to run every time a page is loaded, or every time a specific group of pages are loaded. You then create a PageBase class that inherits from System.Web.UI.Page, containing the method you want to copy, and make all the pages you want to run this method inherit your PageBase instead of the default System.Web.UI.Page. This isn't necessarily poor design - it is one of many tools to try to follow the DRY principle ("Don't Repeat Yourself).

Tomas Lycken
I'd do <projectname>Page as the class name in that case; you're a specialization of Page, for the given project. "PageBase" would be the base class UNDER "Page."
mjfgates
+13  A: 

To quote Roedy Green's How To Write Unmaintainable Code:

Be Abstract

In naming functions and variables, make heavy use of abstract words like it, everything, data, handle, stuff, do, routine, perform and the digits e.g. routineX48, PerformDataFunction, DoIt, HandleStuff and doargs_method.

JMD
+4  A: 

Util classes can be quite useful, but if the name if plain "Util" or "Utils" it's rather undescriptive.

tehvan
Utils or Tools classes are development Weed ;)
Sven Hecht
+12  A: 

The worst function name I've come across in real life:

DoTheLogic();
anon
Better than DoSomeMagic...
superwiren
Ever seen obfuscated c?
Martin K.
even worse: DoIt();
Sven Hecht
Run() –––––––––
iik
I've once seen a function called Main(). Luckily the Main code wasn't there.
Camilo Martin
+7  A: 

how about a "cls" prefix or a "Class" suffix? Those smell

hunter
A: 

The word "Stuff" in any sort of class/function name is going to be a very bad sign (provided it's not the verb form). DoStuff(), ValidateStuff(), FetchStuffFromDatabase(), pick your poison.

Chad Birch
+14  A: 

The most common 'smell' I've found is confusion of concepts of number. You don't want a class called Contacts if one instance refers to one "contact". You call it Contact instead.

staticsan
+1  A: 

DoThisAndThat() or DoThisOrThat() inherently violate the Single Responsibility Principal. A method should do one thing and one thing only (even if that one thing is calling a bunch of other methods)

Matt Briggs
But there may be exceptions. A Halloween simulator would be doing fine to have a "TrickOrTreat" class.
DarenW
@DarenW: There are always exceptions to pretty much everything listed here.
Matt Briggs
+13  A: 

I've found this to be helpful:

"Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser. Avoid words like Manager, Processor, Data, or Info in the name of a class. A class name should not be a verb."

(From Clean Code by Robert Martin, p25)

Jeff Moser
I (IMHO) disagree with the discouragement of the Manager suffix. It's a noun, which satisfies the first part of the quote. Take @HardCode's example of ConfigurationManager. I can't think of a suitable replacement that doesn't sound contrived and awkward. (I agree re: Data, Info, et al.)
JMD
Names matter. The name of your class should not only tell you what the class does, but also what it doesn't do. Using the word Manager puts no limits on what the class CAN do. It makes for hard to understand designs and usually leads to procedural coding styles that infect the rest of the system.
Dunk
Sometimes it has taken a while to come up with a good name, but I can't recall anytime that I didn't come up with a better name and design once I got rid of the word Manager from the class name.
Dunk
@JMD drop manager and it looses nothing. The configuration class. Better yet name it what it configures. GraphicsConfiguration networkConfiguration etc.
stonemetal
@JMD so there is supposedly a Configuration class and the ConfigurationManager does what exactly? In what way does it "manage" the configuration(s)? Does it serialize/deserialize them? There you go then, it can at least become ConfigurationStorage.
TomA
A: 

I think anything that starts with class foo is likely to be unreadable.

Yes, I mean that literally. I tend to over-use "foo"...

Brian Postow
Ick. I never use placeholder names for anything except interactive testing. Ok, so they show up in some unit tests (especially python doctests), but not many.
TokenMacGuy
In college I had a friend who, in his compilers class added a warning to his compiler "Warning: Over-use of the meta-syntactic variable 'foo'" B-)
Brian Postow
+1  A: 

Class names are largely arbitrary, so pick what works for you and the consumers of your code. But some kinds of class names can be indicative of a larger problem.

To illustrate a point:

ClassName
  LongClassName
    VeryLongClassName
      VeryLongClassNameThatIsIndicativeOfExcessiveUseOfInheritance
      VeryLongClassNameThatIsNotIndicativeOfExcessiveUseOfInheritance
    SomewhatLongClassName
  ShortClassName
    ShortNameOfVeryLargeClass
    ShortClassNameThatAlsoHasTheWordShortInTheName

Don't worry so much about the names. Worry about the design. Where I work, we routinely bandy about classes with names like F, P, V, and $, and it's not a problem for anyone.

Apocalisp
A: 

I don't agree with "Manager", and apparently neither does Microsoft (not that they are always right!) Consider the ConfigurationManager class. It's a legitimate use of "Manager" because this class manages the app.config/web.config files. "ConfigurationReaderWriter" would be bizzare, and having two classes (ConfigurationReader and ConfigurationWriter) wouldn't make much sense.

HardCode
I 100% disagree with using the word Manager in a class name, unless you are modeling a real Manager (as in a person). What does a manager class do? Anything you want. ConfigurationManager is not an example of a valid use, it shows what goes wrong...
Dunk
The ConfigurationManager class does much, much more than your snippet implies. Developers couldn't come up with a better place to put functionality so they just threw stuff in the manager class because it was remotely related. Whose to say that a manager can't do all these things.
Dunk
django uses a 'Manager' class also. It's scope is very narrow, it assembles fragments of SQL into queries for a particular back-end. I'm not too fond of the name, because it's not obvious from the name that it does that.
TokenMacGuy
System.Configuration.ConfigurationManager does not manage configurations. It is not even a real class that would be instantiated; it contains only static factory methods to open configuration files. Those methods could have been static methods of the Configuration class itself. No "Manager" needed.
cpeterso
@Dunk: So, a Manager class representing a person, and not PersonManager ... which implies that Manager manages something. Why not for a non-person entity? This whole "Manager" semantic debate is silly.
HardCode
The debate is not silly. The name of a class should tell you what the class does. As soon as you throw in the word manager (and like words) then the class no longer has sufficient constraints to tell you what it does. In my example, Manager was a title and in certain contexts it has well known...
Dunk
responsibilities. Simply managing persons is not sufficient reason to create a Manager class. Using words like Manager lead to hard to understand designs in nearly all cases. Thus, I reiterate, the debate is not silly, unless you don't care about whether you create hard to understand designs.
Dunk
ReaderWriter = Storage. ConfigurationStorage.
TomA
@Dunk: It seems you're just stating an opinion. You have failed at showing how it leads to bad design. Stating that it does, doesn't make it so.
mangoDrunk
@mangoDrunk: GOOD CLASS DESIGN = The class name lets a reader know what the class does, including inferring constraints on the class. BAD CLASS DESIGN = The class name only vaguely tells what it does, and leaves open to interpretation what does and doesn't belong. Yes, those are my opinions, and because I believe strongly in those opinions, based upon seeing the nightmares wrongfully naming something causes, it makes it so in my world.
Dunk
@Dunk: GOOD CLASS DESIGN = What the class IS, not DOES. Anyway, there are a gazillion of examples of class names that do not make clear what a class is OR does. Just look at .NET - Panel, Button, ConfigurationManager, ... just by reading the class names, it is not apparent (to someone who doesn't already know) what the class is OR does.
HardCode
+1  A: 

Singleton.

MSN
If its a Baseclass or a decorator? If there's only one where's the problem?
Sven Hecht
Singleton is an implementation detail cast as a design pattern. As an implementation detail it's a major impediment to scalability.
MSN
+1  A: 

The words "Data", "Info", "Struct", "Record", etc. in a class name mean that somebody has Not Been Thinking. Ohhhhh, that's the class with the DATA in it! So all these other classes, they don't have any?...

(Yeah, I know, interfaces. But even then, naming the implementation of an interface "InterfaceData" would be pretty dim.)

mjfgates
A: 

Class names that aren't indicative of what they do. There's a lot of things just called 'Manager' or 'Interface' hidden inside libraries that I've worked on in the past. You're never supposed to call them directly as they're internal structures to manage its functions.

It's not clear what 'Interface' interfaces to; is it a programming interface, does it interface on the network or to hardware? To find out you have to go and read the code/docs for the Interface class.

Of course when I say 'interface' I mean any reasonably vague single word name.

Adam Hawes
+3  A: 

" *Helper* ". If a class can't do its job, who can?

Matt
Done it before, and will probably do it again, but man I've seen it used for evil
johnc
The class might be final/sealed. Think StringHelper...
EricSchaefer
+1  A: 

I've seen interfaces called InterfaceNameImpl. What can be worse?

parxier
A: 

http://stackoverflow.com/questions/560648/object-oriented-login-functionality/563392#563392

A Manager is in charge of many directly related entities. The manager is responsible for how those entities interact with a client. Some manager classes do not have well defined names such as array, stack, and queue.

SmokingRope
+1  A: 

If a class can't have a more specific name than FooManager, then it's possibly trying to do too many different things with Foo.

Joachim Sauer
+6  A: 

Manager, Handler, Thing, Dingus, Doodad, Entity, Gizmo, Object, Helper, Widget, Gadget

-> http://journal.stuffwithstuff.com/2009/06/05/naming-things-in-code/

Sven Hecht
"Object", huh? Don't tell that to the Java crowd. :) "NSObject" is fine, I guess, so the Mac/iPhone/Cocoa people are off the hook...
Seva Alekseyev
Well these aren't supposed to go without any decorations (suff- and postfixes)
Sven Hecht
A: 

Naming all depends on the context in the application it is used in. There are no hard and fast rules.

If your comming into a project that's already up and running and they are using verbs for class names everywhere, why would you confuse programmers down the line and take a different naming approach?

Think about the big picture.

Sir Psycho
+1  A: 

My biggest pet peeve is one that I've run into a lot on a large real-life project: Helper. Sometimes it's a bunch of static utility methods, sometimes it's actually a strategy object, usually it's just a blob of code with no well-considered place in the design.

Also, Strategy is a bad sign, much like Singleton as MSN noted. Congratulations! You're using a design pattern!

I never liked Util for utility classes, but that's more a matter of taste. (I prefer the pluralized noun, like Arrays or Collections from the JDK, for a bunch of static methods related to one kind of object.)

Luke Maurer
Yeah, I'm dealing with a lot of "Helper" classes in my current project. These do anything but help!
DarenW