Of all the words in the dictionary, how to choose a clear classname?
The basic rule is that classes should be named according to what they do (eg. LoadTestingClient.java) or represent (SupplierInvoice.java). The name of the class should be made as long as required to convey what it does.
public class AdministrationAction
{
:
}
What does the action do? Administer what?
public class UserAdministrationAction
{
:
}
Here, the UserAdministrationAction text shows the viewer immediately that the class is used to administer users, whilst still keeping the Administration tag.
Some real classnames that never caught on.
Classname |
Why |
---|---|
WampID3v2.java |
Put the version and other information in the package name or in the final built .jar |
Toodoo.java |
Unprofessional and meaningless. |
AdviserSessionSessionAttributeReplaced.java |
Very long yet no real meaning. Session word contained twice. |
AnyXMLContentType.java |
Ambiguous. |
Id3v2_4TagFactory.java |
Version numbers should be moved to the class and underscores have no place in classnames. |
ZooComponent.java |
Meaningless |
PostBodyPartWith100Chars.java |
Describes detailed behavior which may soon change. |
PostEnc.java |
Enc could mean Enclosure, Encapsulate, Encoding. Next time use the full word. |
Level_World00_Level01.java |
Contains underscores and version numbers. Clearly this class is designed to be found using reflection which breaks the OO model – use a factory instead. |
FixPasswordCantChange.java |
Words like “Can't” and “Can” do not represent a concept or kind of object. “Fix” is the equivalent of a “to do”. |
James.java |
This was either written by James or a class written to complete some task James wanted completed. Either way names should not be seen in classes. |
Some classnames that everybody loved.
JaasSecurityProvider.java
JarResourceLoader.java
PlayerDataMessage.java
GameServerThread.java
PricingUtils.java
DocumentInfo.java
InitialServlet.java
UploadManagerServer.java
AbstractPasswordEncoder.java
Invoice.java
Song.java
SpriteFactory.java
CarOptionsBackingBean.java
SearchInvoiceByMailingHouseRegressionTest.java
UseCase008RelationalPersistenceConnectionTest.java
ScansPerMonthPerStationReport.java
Exception:
Whilst it is OK to make classnames verbose to describe their function, don't use a longer word when a shorter one will do. For example, avoid MenuItemSpecificationConfiguration if you can use MenuItemData instead.