views:

229

answers:

9

Ok, so you can read guidelines on identifier naming 'til you're blue in the face... camel case, pascal case, make 'em descriptive... but they won't help you actually pick the best name for a given application domain.

The easiest to choose (IMHO) are one or two word noun groups:

  • EntryForm
  • Employee
  • WidgetCollection

But not every class fits neatly into a noun so I see a lot of people turn verbs into nouns by adding -er to the end of them:

  • AccountManager
  • RecordCounter
  • ProcessRunner

The biggest problem I see with these is that a lot of time they're ambiguous... especially Manager. What exactly is it managing?

So my question is how do you pick a good name for a class? By 'good' I mean informative and unambiguous.

I know, I know. With refactoring support built into pretty much every modern IDE you can change the name without batting an eye so what's the point? Well a poorly chosen name will confuse and mislead anyone who stumbles across it until it's renamed so its still a valid question.

Related

What’s the best approach to naming classes?

A: 

If you're in a .NET environment, you can use FxCop for guidelines. As far as naming a class, I usually will go with noun/purpose. You're exactly right, what does the Manager class do? If it's a controller class, call it ManagerController.

Joshua Belden
What if it manages ManagerControllers?ManagerControllerManager? :P
Andy Mikula
Sounds like you need an abstract factory for that one.
Michael Myers
A: 

Have a standard that you stick to and then there won't be any confusion. If your page is named AccountManager then with my standard, it would mean "the page that you manage accounts on".

Do similar for class names, events, etc.

rball
What if it's a domain object representing an Employee who manages Accounts? What do you call the thing that manages the manager? AccountManagerManager?
Adam Jaskiewicz
AccountManagersManager. I guess if you don't like the word "Manager" then call it Editor. Sure there might be conflicts, but do you REALLY care? Name it and move on! :P
rball
A: 

Picking terse, informative, appropriate names for things is one of the hardest parts of programming.

There is no formula for it. It's a skill you pick up with experience.

Notice the emphasis on terse. The long names in Java is one of the major turnoffs of that language. It basically has gotten to the point that it's impossible to program in Java without an IDE with autocomplete.

Abbreviations can be your friend when naming. For instance, 'Mgr' instead of 'Manager'. Do I really need to type out 'Manager' a thousand times a day when 'Mgr' gets the point across? Just make sure people use them consistently and judiciously.

I think to-abbreviate or to-not-abbreviate is a basically arbitrary decision nowadays, with IDE autocompletion.
Kekoa
Well, maybe not arbitrary, but based on personal preference.
Kekoa
Mgr to me means Meager, or Migraine. Honestly typing it out isn't that hard and I've never had to do it thousands of times a day -> plus there's auto completion and other tools. I'd rather have clarity so I don't have to decipher what *you* think is an obvious terse name which I have no clue what it is. Shoot, instead of Mgr, why not just 'm'?
rball
@kekoav, Well some of us are still working in languages *cough* C++ *cough* for which auto-complete sucks. And in lots dynamic languages there is just no hope for auto-complete. Also, I think auto-complete has ruined a lot of programmers. They use their IDE as a crutch. With Java it's just kind of unavoidable though. The rigidity and narrowness of the type system just leads to monster names. Not to mention the lack of typedefs which would have solved pretty much all these problems. What were they thinking?AbstractVisitorAdapterFactoryImplementationStrategy
@rball, Well, when people are working on a team they usually come up with conventions. 'Mgr' would be a convention that would save tens (maybe hundreds) of thousands of keystrokes over the course of a project. Notice where I said 'consistently and judiciously'?
A: 

A good point is to use design patterns and you'll have all done :)

ruslander
+2  A: 

1) Don't abbreviate. Use acronyms only if they are industry standard ones like HTML.

2) Be self-consistent in your names.

3) Use terms from the domain your application is in. When coming up with a name, pretend that you have a limited number of bullets to spend when adding new names that people won't be familiar with.

You might feel that some subtle feature in your class differentiates it from a 'Foo,' and feel inclined to make up some new concept for it because of that subtle difference. If 80% of people using it won't care about that subtle difference, stick with the known term.

4) In most OO languages, autocomplete matters. When possible, the first word should tend to be the one that people are most likely to type in when exploring a namespace. Other people feel that having it read well in English is more important, so this is kind of debatable.

Drew Hoskins
A: 

I get sick of seeing classes named like the following:

XxxHandler XxxManager XxxController

Handler and Manager are hardly descriptive of what a class is. If we can't find a good name for a class, we should probably make sure what we have the class doing is clear and is not trying to do too many things. Smaller classes usually make for easier to name classes, because they typically have a narrow scope of what they do.

Sometimes, though, we run out of creative things to call things, and just revert to the same old thing.

Kekoa
I think a lot of times its just the fact that you gotta move on. Instead of thinking of the greatest name ever conceived I'd rather write code to get things done.
rball
A: 

Like kekoav, I am wary of naming something FooManager or FooController. Not only is it indicative of an object that suffers from an identity crisis, but it introduces ambiguity. I've worked on a project where there was an "AccountManager", which was a domain object representing a person who managed accounts (a subtype of Employee). Of course, someone made an "AccountManagerManager", and someone else got confused and created an Account domain object (which wasn't something our app was dealing with), then mixed stuff into AccountManager to manage the Account. It was kind of a mess.

Quis custodiet ipsos custodes? Quoque quis administro ipsos administratorum?

Yeah, my Latin is rusty.

Adam Jaskiewicz
people called 'Romanes' they go the house?
Mark
"Quis custodiet ipsos custodes" is "who will guard the guards?" It's a line from a poem by Juvenal from one of his Satires. My likely mangled modification is meant to be something along the lines of "Likewise, who will manage the managers?"
Adam Jaskiewicz
Likewise, http://www.youtube.com/watch?v=IIAdHEwiAy8
Mark
A: 

I've been reading Clean Code by Robert C. Martin and hadn't made it to this section from chapter 17 yet but I think it gets the closest to answering the question.

N1: Choose Descriptive Names

Don’t be too quick to choose a name. Make sure the name is descriptive. Remember that meanings tend to drift as software evolves, so frequently reevaluate the appropriateness of the names you choose. This is not just a “feel-good” recommendation. Names in software are 90 percent of what make software readable. You need to take the time to choose them wisely and keep them relevant. Names are too important to treat carelessly. Consider the code below. What does it do? If I show you the code with well-chosen names, it will make perfect sense to you, but like this it’s just a hodge-podge of symbols and magic numbers.

public int x() {
    int q = 0;
    int z = 0;
    for (int kk = 0; kk < 10; kk++) {
        if (l[z] == 10)
        {
            q += 10 + (l[z + 1] + l[z + 2]);
            z += 1;
        }
        else if (l[z] + l[z + 1] == 10)
        {
            q += 10 + l[z + 2];
            z += 2;
        } else {
            q += l[z] + l[z + 1];
            z += 2;
        }
    }
    return q;
}

Here is the code the way it should be written. This snippet is actually less complete than the one above. Yet you can infer immediately what it is trying to do, and you could very likely write the missing functions based on that inferred meaning. The magic numbers are no longer magic, and the structure of the algorithm is compellingly descriptive.

public int score() {
    int score = 0;
    int frame = 0;
    for (int frameNumber = 0; frameNumber < 10; frameNumber++) {
        if (isStrike(frame)) {
            score += 10 + nextTwoBallsForStrike(frame);
            frame += 1;
        } else if (isSpare(frame)) {
            score += 10 + nextBallForSpare(frame);
            frame += 2;
        } else {
            score += twoBallsInFrame(frame);
            frame += 2;
        }
    }
    return score;
}

The power of carefully chosen names is that they overload the structure of the code with description. That overloading sets the readers’ expectations about what the other functions in the module do. You can infer the implementation of isStrike() by looking at the code above. When you read the isStrike method, it will be “pretty much what you expected.”

private boolean isStrike(int frame) {
    return rolls[frame] == 10;
}

N2: Choose Names at the Appropriate Level of Abstraction

Don’t pick names that communicate implementation; choose names the reflect the level of abstraction of the class or function you are working in. This is hard to do. Again, people are just too good at mixing levels of abstractions. Each time you make a pass over your code, you will likely find some variable that is named at too low a level. You should take the opportunity to change those names when you find them. Making code readable requires a dedication to continuous improvement. Consider the Modem interface below:

public interface Modem {
    boolean dial(String phoneNumber);
    boolean disconnect();
    boolean send(char c);
    char recv();
    String getConnectedPhoneNumber();
}

At first this looks fine. The functions all seem appropriate. Indeed, for many applications they are. But now consider an application in which some modems aren’t connected by dialing. Rather they are connected permanently by hard wiring them together (think of the cable modems that provide Internet access to most homes nowadays). Perhaps some are connected by sending a port number to a switch over a USB connection. Clearly the notion of phone numbers is at the wrong level of abstraction. A better naming strategy for this scenario might be:

public interface Modem {
    boolean connect(String connectionLocator);
    boolean disconnect();
    boolean send(char c);
    char recv();
    String getConnectedLocator();
}

Now the names don’t make any commitments about phone numbers. They can still be used for phone numbers, or they could be used for any other kind of connection strategy.

N3: Use Standard Nomenclature Where Possible

Names are easier to understand if they are based on existing convention or usage. For example, if you are using the DECORATOR pattern, you should use the word Decorator in the names of the decorating classes. For example, AutoHangupModemDecorator might be the name of a class that decorates a Modem with the ability to automatically hang up at the end of a session. Patterns are just one kind of standard. In Java, for example, functions that convert objects to string representations are often named toString. It is better to follow conventions like these than to invent your own. Teams will often invent their own standard system of names for a particular project. Eric Evans refers to this as a ubiquitous language for the project. Your code should use the terms from this language extensively. In short, the more you can use names that are overloaded with special meanings that are relevant to your project, the easier it will be for readers to know what your code is talking about.

N4: Unambiguous Names

Choose names that make the workings of a function or variable unambiguous. Consider this example from FitNesse:

private String doRename() throws Exception
{
    if(refactorReferences)
        renameReferences();
    renamePage();
    pathToRename.removeNameFromEnd();
    pathToRename.addNameToEnd(newName);
    return PathParser.render(pathToRename);
}

The name of this function does not say what the function does except in broad and vague terms. This is emphasized by the fact that there is a function named renamePage inside the function named doRename! What do the names tell you about the difference between the two functions? Nothing. A better name for that function is renamePageAndOptionallyAllReferences. This may seem long, and it is, but it’s only called from one place in the module, so it’s explanatory value outweighs the length.

N5: Use Long Names for Long Scopes

The length of a name should be related to the length of the scope. You can use very short variable names for tiny scopes, but for big scopes you should use longer names. Variable names like i and j are just fine if their scope is five lines long. Consider this snippet from the old standard “Bowling Game”:

private void rollMany(int n, int pins)
{
    for (int i=0; i<n; i++)
        g.roll(pins);
}

This is perfectly clear and would be obfuscated if the variable i were replaced with something annoying like rollCount. On the other hand, variables and functions with short names lose their meaning over long distances. So the longer the scope of the name, the longer and more precise the name should be.

N6: Avoid Encodings

Names should not be encoded with type or scope information. Prefixes such as m_ or f are useless in today’s environments. Also project and/or subsystem encodings such as vis_ (for visual imaging system) are distracting and redundant. Again, today’s environments provide all that information without having to mangle the names. Keep your names free of Hungarian pollution.

N7: Names Should Describe Side-Effects

Names should describe everything that a function, variable, or class is or does. Don’t hide side effects with a name. Don’t use a simple verb to describe a function that does more than just that simple action. For example, consider this code from TestNG:

public ObjectOutputStream getOos() throws IOException {
    if (m_oos == null) {
        m_oos = new ObjectOutputStream(m_socket.getOutputStream());
    }
    return m_oos;
}

This function does a bit more than get an “oos”; it creates the “oos” if it hasn’t been created already. Thus, a better name might be createOrReturnOos.

codeelegance
A: 

When I find it difficult to pick a meaningful name, it alerts me that the class is too big or I don't really understand the domain of whatever I'm coding.

When you do finally come up with a good design addressing the concerns brought up by Uncle Bob in Clean Code regarding concepts like appropriate abstraction, single responsibility principle and whatnot, then the names become easier to pick.

Difficulty picking names is almost always a sign that the logic's organization still needs work. After all, the compiler doesn't care how well or poorly your logic is organized. Organization is there only to help manage complexity and the sanity of your fellpw developers.

Allbite