Interface naming in Java
Choose descriptive adjectives for interfaces such as Serializable
, Cloneable
. For operation-specific interfaces, use -er
or -or
endings, like Renderer
or Listener
.
Aim for self-explanatory names that reveal the interface's true functionality.
Unnecessary prefixes: The "I" before the storm
Traditionalists used to prefix interfaces with an "I". It's often seen as an outdated, redundant convention. Stress on the functionality the interface provides for intuitive code. Shifting from abstract class to interface? Absence of "I" means no renaming catastrophe.
The art of descriptive naming
Names like List
or Stream
deliver their purpose without fitting all scenarios into a rigid naming convention. Opt for interface names such as Appendable
, Iterable
, or Observable
. Keep sight on interface-driven design's philosophy - focus on usability over identity.
Direct class usage: The forbidden fruit
Make class implementation names less appealing to promote interface usage. Name your class HTTPUrlConnection
, but promote URLConnection
usage in clients. Encourage coding practices like programming to an interface.
Redundant naming: Déjà vu?
Be wary of the redundancy in naming. UserImpl
and User
don't hint at how UserImpl
uniquely implements User
. Name implementations to reflect their nature, like LDAPUser
or SQLUser
. This adds an intuitive connection without generic suffixes like Impl
.
Tailoring to suit application needs
Interface naming strategies should be adapted according to the specific domain. In an e-commerce application, for instance, interfaces like CartAddable
or InventoryCheckable
are informed choices, immediately revealing their roles.
Taking a page from Spring's book
Spring's preference to omit "I" prefix influences Java community practices. Instead of enforcing a less popular convention, go with a naming strategy that's future-proof and conforming with widely-accepted norms.
One implementation interfaces: An existential crisis?
Metrics to justify a single implementation interface balance upon its complexity and its extensibility. Scrutinize the cost-to-benefit ratio within the specific context of your Java framework.
Writing code: A time-traveler's guide
Strive for code that's future-proof. And as Java evolves, aim to write code favoring functional contracts. Amidst Java's functional programming shift, interfaces such as Function
, Consumer
, or Supplier
becoming commonplace imply the importance of adaptability in your code.
Molding names for diverse applications
Interface names should service a range of possible applications. An interface Notifiable
could be implemented by user classes, services, or systems with notifications, making it versatile and scalable.
Was this article helpful?