How do you name different classes / interfaces you create?
Sometimes I don’t have implementation information to add to the implementation name – like interface FileHandler and class SqlFileHandler.
When this happens I usually name the interface in the “normal” name, like Truck and name the actual class TruckClass.
How do you name interfaces and classes in this regard?
Name your
Interfacewhat it is.Truck. NotITruckbecause it isn’t anITruckit is aTruck.An
Interfacein Java is a Type. Then you haveDumpTruck,TransferTruck,WreckerTruck,CementTruck, etc thatimplements Truck.When you are using the
Interfacein place of a sub-class you just cast it toTruck. As inList<Truck>. PuttingIin front is just Systems Hungarian style notation tautology that adds nothing but more stuff to type to your code.All modern Java IDE’s mark Interfaces and Implementations and what not without this silly notation. Don’t call it
TruckClassthat is tautology just as bad as theIInterfacetautology.If it is an implementation it is a class. The only real exception to this rule, and there are always exceptions, could be something like
AbstractTruck. Since only the sub-classes will ever see this and you should never cast to anAbstractclass it does add some information that the class is abstract and to how it should be used. You could still come up with a better name thanAbstractTruckand useBaseTruckorDefaultTruckinstead since theabstractis in the definition. But sinceAbstractclasses should never be part of any public facing interface I believe it is an acceptable exception to the rule. Making the constructorsprotectedgoes a long way to crossing this divide.And the
Implsuffix is just more noise as well. More tautology. Anything that isn’t an interface is an implementation, even abstract classes which are partial implementations. Are you going to put that sillyImplsuffix on every name of every Class?The
Interfaceis a contract on what the public methods and properties have to support, it is also Type information as well. Everything that implementsTruckis a Type ofTruck.Look to the Java standard library itself. Do you see
IList,ArrayListImpl,LinkedListImpl? No, you seeListandArrayList, andLinkedList. Here is a nice article about this exact question. Any of these silly prefix/suffix naming conventions all violate the DRY principle as well.Also, if you find yourself adding
DTO,JDO,BEANor other silly repetitive suffixes to objects then they probably belong in a package instead of all those suffixes. Properly packaged namespaces are self documenting and reduce all the useless redundant information in these really poorly conceived proprietary naming schemes that most places don’t even internally adhere to in a consistent manner.If all you can come up with to make your
Classname unique is suffixing it withImpl, then you need to rethink having anInterfaceat all. So when you have a situation where you have anInterfaceand a singleImplementationthat is not uniquely specialized from theInterfaceyou probably don’t need theInterfacein most cases.However, in general for maintainability, testability, mocking, it’s best practice to provide interfaces. See this answer for more details.
Also Refer this interesting article by Martin Fowler on this topic of InterfaceImplementationPair