What to Name It?
This is a constant question we are faced with in daily life. We want to come up with perfect names for our babies. We want to name our animals (pets) and they seem to be happy to have a name! And it seems to be a big thing! Otherwise, we wouldn’t have naming ceremonies like these. We try to name (rename) our buildings (otherwise what would you call Empire State Building?), streets, cities and countries. (Myanmar or Mumbai is yesterday’s story. Mexico wants to rename themselves as Mexico! See here.) And naming is not an easy thing to do. A world recognized music composer in India, Illayaraja, called his symphonic music album “How to Name it” for lack of a better name.
What’s That Got To Do With This?
Java developers (or any developer for that matter) are merely imitating the real world and they too have to face such challenges while naming their programs, classes and objects. We don’t want to name (can’t) two entities the same name. That would cause confusion (Name Collision). To avoid this, we use packages, name spaces, domain, etc.
Each “object” tends to have 2 forms in Java world (or .NET, C++, etc.) – the class and its interface! An interface is a class’s “public profile” – much like public profile we have in linked-in, what we want other classes to know about this class.
We could say the interface is not unlike a business card or a cover letter we send with our resume when we apply for a job. Cover letter contains exact amount of information that may be of interest to the other party while the resume may have lot more inner details. Now they both are about you, but not the same.
So, What’s Up with “Impl”?
So, if you are a developer, what would you call the interface and implementation for an object that models a real car?
OO interface names
This is not as simple as it seems. There are at least 2 camps. One camp, the Microsoft based camp calls the (implementation) classes by just the name and prefixes their interfaces with “I”. To them, the Implementation is the real object and interface is its business card. The other camp (seemingly started by IBM) uses the plain name for interfaces and the (implementation) classes are named with a suffix “Impl” for Implementation. Here the interface represents the abstracted real word object and the Implementation class merely has extra (inner) details that doesn’t concern outsiders (other classes). The Java world mostly follows the “Impl” convention.
So there you have it, when you see Java classes named with “Impl” at the end, you know why. End of story, right? No, not really. Object Oriented pundits and purists don’t agree. Here is a nice post where the author disagrees with “Impl” usage. And here is another perspective to it. And it’s an ongoing debate. Don’t believe me? See here, here, here.
So Which Way To Go?
Naming your class can be a personal choice, but most of us write programs that will be shared with others in the community. That’s why conventions and best practices exist. If you are working on a project, set up and follow standards appropriate to your shop.
In some cases, you may not be able to change the “I” or “Impl” in names. Many programs may have some type of interface (protocol) like CORBA, COM, Web Services. In these cases, they generate code (Stubs, Skeletons, etc.) that you have to base your own code on. Document these and stick to those conventions to avoid any surprises.
Personally, I am OK with using Impl suffix. I work with EAServer which runs both Powerbuilder and Java objects. These objects “talk” to each other using CORBA IIOP. Both Powerbuilder and EAServer tools generate CORBA IDL files which in turn are used to generate stubs and skeletons in Java (or C++). Developers then write Implementation classes for the interfaces generated. The class files generated and the impl classes may end up in the same Classpath and to avoid Name Collision, we have to name implementation classes differently and the convention is to use Impl suffix. This is a convention I am sticking with in my current project.
A note about my “cheesy” cartoon there. While searching for something relevant, I found Toondoo where you can make your own cartoon. You can start with a free account. I also use openclipart for some of the pictures in my other posts.
Filed under: CodeProject
Tagged: Coding Standards