Roy Osherove

View Original

Interface Naming - Anything But Java's Standard, Please

Jeremy dislikes Interface names that being with 'I' ("ISerializable"..). Len Holgate disagrees with him, but likes to name his classes in a "verb-noun" fasion ("IManageUsers").

 

Personally, I'd much rather have "I" for interfaces. It makes the code much clearer.

Having worked both in Java and C#, I find it very confusing in Java when I keep seeing class names, but I alwasy have this thought in the back of my head "maybe it's really an interface?". And so I have to keep hovering my mouse over the variable types to understand what they really are.

 

To me, seeing "I" for a type, lets me know that this variable represents something abstract, which its implementation is changeable (I can cast it to other stuff, and send it as a representation of my own stuff).

It lets me understand code which uses lots of dependency injection and realize just what I'm up against.

 

As for how to name interfaces (the part after the "I"), I like to go with a different rule of thumb than Len's. I name my interfaces as to *what can be done to them*. So a class that implements them suddenly has more abilities to be performed through it. For example, "ISerializable" indicates that the class that implements it supports another operation performed on it.

Len's example states he'd name something "IManageUsers". I'd rather call it "IUserManager" instead, since it is more in the realm of the real implementers of this interface ("UserManager", "DBUserManager" etc.)

 

It's really a matter of taste and the standard your company employs, but whatever it is, I feel that Java makes it the hardest to comprehend what you're up against. For example, that interface in Java would have been called "UserManager" in the first place, leading to much uglier names such as "UserManagerImp". Ugh.