Choosing good names for your Java classes

With Object-Oriented programming we often have many classes, therefore it is really important to name them well.

It’s amazing how I can spend so much time searching for good names for classes, and I do think it is worth doing it well, after all naming is all about making the code easy to read and to understand, which is a major concern in professional programming.

However it is not easy to find good names, and by the way what does “good” naming means ? I claim that a name is good when most people understand it the same way. This gives already a valuable advise to check a name: ask your colleagues what they think of it, or what they would suggest instead. In pair programming, this is obviously always the case, and this sure helps.

But how to find good names in the first place ? First the domain analysis gives good names, because they are names everybody agrees on [1].

Then whenever you use design patterns (or analysis, UI or J2EE patterns), these patterns suggest rather standard prefixes or postfixes for your classes and methods names. For example, if you implement the State pattern, one would expect classes with names like “MyDomainThingState”. Of course this is not a rule, but if you have no good reason not to do this way, you’d better follow the most standard way.

Also, any convention or API that is well-known can be used as a naming guideline, such as the Java API, Commons Collections and some major open-source projects. Any Iterator must be called “SomeIterator”, and a class that purpose is to filter objects is a “SomePredicate”. Naming interfaces that define a capability with “SomeCapability-able” is a very common usage, as well as the use of plural to name a class that operates on a type hierarchy such as Collections (note the plural) that operates over several Collection instances.

Of course Hungarian Notation should be avoided (do not prefix interfaces with “I”) as nobody should ever care that a type is an interface or not to use it the same fashion, only the factory has to know about this implementation detail.

Some words do not mean anything precise, like “data”, “object”, “wrapper”… As a result they do not bring any benefit, they are just in the way. Just try to get rid of them or replace them with words from the domain (with Rename refactoring this is now very easy) and you will be surprised of the level of expressiveness you gain.

Often you think of a name, but for many reasons you cannot use it: maybe you already used it to mean something else, or it suggests something wrong to some people, or it does not suggest anything, or you really do not like it… Then I very often use an online dictionary and a thesaurus to find synonymouses and related words.

A random search in a web search engine is also a good way to go in-depth in the semantic context of a name, in order to find similar and related words that may be used for your naming. This is particularly relevant in specific domains, like finance, where dictionaries are not up-to-date or relevant. By seeing words already used in their context, you can use them more accurately. For example, in finance, we can talk a a group of products as a Package, a Bundle, or a Strategy. The latter is not good because it could mislead to the Strategy pattern. A google search only could help to choose Package, or Bundle, according to the very case you are dealing with.

Package names also help in this topic. Do not forget a class does not have to tell everything in its name, as its package name is part of the actual name and has the purpose to give a context to the short class name. As a consequence, it is not a problem to have the same short class name several times in a project, as long as they are not used together too often. Typically if you have an abstract factory of two families of things, each thing in each family can have the same name, in a dedicated package.

[1] See Ubiquitous Language, by Eric Evans –

UPDATE: An interesting reader’s comment have been destroyed when cleaning spams: this comment suggested to use Eclipse ctrl-shift-t/ctrl-shift-r to see lists of existing class names. Apologises for that.

UPDATE: A class name can reflect what the class really is, or what it is used for. In many cases naming a class according to what it is makes its more reusable, in which case the instance name (field of variable name) tells its purpose for the particular use.

Initially published on Jroller on Thursday April 27, 2006


Software development, Domain-Driven Design, patterns and agile principles enthusiast