Thursday, May 17, 2012

"TIONTITITQ" ("To interface, or not to interface, that is the question")

greg-prentice - Developer Alchemist's best friends: "To interface, or not to interface, that is the question"

"Alchemy requires Shakespeare!

A common question that I've had to answer for myself and have been asked throughout my development career: “To interface, or not to interface, that is the question”. As you hopefully noted, I've taken one of the most famous literary opening monologues from the play Hamlet and will attempt to apply it to the world of a developer. In Hamlet’s opening lines he asks an important question that I feel applies to us in the development world: “Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them?”

I believe what Hamlet was trying to tell us is that as a developer, do we make a conscience decision to “suffer” or “take arms”. To further explain I’ll set the context behind his meaning around the statement: “The slings and arrows of outrageous fortune”. Here Hamlet is trying to tell us that code we have either inherited or written may have to undergo refactoring. Refactoring is clearly called out when Hamlet states: ”Or to take arms against a sea of troubles”. Further that as we mature in our development prowess, our mind will begin to tug at our very consciousness. Begging us to refactor our code which brings us to Hamlets last phase: “And by opposing end them”. I have to say that when I had the realization that Hamlet was talking to us developers, I could not ignore this. I had to share it with all Alchemists! Further I thought I would also provide a concrete example around the meaning of: “To interface, or not to interface, that is the question”!

So some ground rules to begin with:

  • Don’t use an interface just to use and interface.

  • If you have a high number of interfaces you most likely have too many.

  • Use an interface when you want to clearly define a contract between two independent systems

  • Use an interface when you want to extend the behavior of our ‘cool idea’ to others, we only care that they implement the contract; and we hope they do it correctly J.

  • Think classes first; since you have to implement your own interfaces the concrete class will serve you well in defining your contracts.


In closing I want to circle around back to my original topic of “How I know Hamlet was talking to us developers”. He clearly states in his monologue that being complacent is easy, being proactive and addressing adversity is hard. As developers we should be making these hard decisions every day. We should be asking ourselves during our coding, designing, and code reviewing the hard questions around when to use an interface. More specifically we should take the time to refactor our code: ”Or to take arms against a sea of troubles” for the benefit of quality.



If you've been coding for a while you're to have seen the conversations around, for and against, interfaces, but I doubt you've seen it covered in this way (Using Shakespeare? How different is that! ;) No matter where you stand on interface usage this is an good and interesting read.

No comments: