November 05, 2002
Interfaces are your friend

There is nothing more frustrating than attempting to do a teeny little code change in the back-end and discovering your teeny little code change actually requires changes in 50 different classes all accross the board because someone failed to utilize an interface when they wrote the original code.

Interfaces are your friend. It's good to use them, really.

One thing they do quite well is hide the implementation from the calling classes. Why is that good? Well, when you want to change the implementation you don't have to change the calling classes.. Saves time, saves work, saves QA resources, code is more readable, more versatile and easier to maintain.

Perfect example of when you really should use an interface...

ArrayList (java.util package).. it's a nifty class.. handy, useful, used a lot and it implements the List interface (same package). Imagine you wrote a class at one point that retrieves some data from the database and returns it in the form of an ArrayList. Like so..

public ArrayList getStuff(int id) {
   ArrayList rval = new ArrayList();
   // magical code that gets the stuff
   return rval;
}

Simple, easy, dependable.. but there is a better way! Have you guessed yet? That's right.. an interface!

public List getStuff(int id) {
   List rval = new ArrayList()
   // magical code that gets the stuff
   return rval;
}

What was accomplished here? If at some point you decide you want to use, say, a LinkedList instead of an ArrayList because it happens to be more efficient for the particular purpose you use the results for, the only place you need to change the code is in this little method. Neat, no?

How do you know when a class implements an interface you could use? Javadocs!

Interfaces have many other benefits, but this is the particular one that I somehow wanted to write about today.. no reason, really..

Posted November 05, 2002 08:38 PM in Java
TrackBack URL for this entry: http://www.unix-girl.com/mt/mt-tb.cgi/309
Comments
On November 5, 2002 09:29 PM Derek added:

Does $fuckwit who wrote the bad code still work there? If so, break his code, and let him fix it.

I'm a firm believer in the power of negative reinforcement. When his code breaks all over the place, you can point out that "he was making all sorts of under-the-hood calls he shouldn't have been and it serves his dumb ass right!"

#
On November 6, 2002 02:19 PM l.m.orchard added:

Well, the problem with that, at least where I've worked, is that $fuckwit isn't working on it right now - I am. So, that means "I broke it", leading to "I gotta fix it". Talking about things under the hood causes glazed managerial eyes and a question: "So when will you have it working again?"

But, of course, I know that where I work, the process is less than smooth or smart.

#
Trackbacks
Reflective Surface:A programming horror show
Kasia talks about code maintenance, and proper coding techniques to avoid problems in the future . I can't agree more.
(read more)
November 6, 2002 02:48 PM