A Funny Java Flavoured Look at the World

Wednesday, August 02, 2006

Don't go over board with your overloading

I was writing a class today and I was creating a method which returned an xml Element and then I thought perhaps I should write another method which returns a String version in case someone wants that.

At this point I caught myself and thought "why are you doing that"

I was about to spend extra time (not that much in this case) writing another method to return a String instead of an xml Element but why. I am never going to use that method and I doubt anyone else is ever going to use it either. All it would do would slow down any auto complete function in my IDE as I have to choose between two similar looking methods. Plus it would probably be some more code to maintain.

This extra overloading is one of the things I am trying to cut out, it's a complete waste of time for no benefit. I'm trying to get out of this habit because if someone else wants it returned in another way then they can bloody well write it, wrap my method if was an external user.

I just thought I would blog about this because it seems like such an stupid thing to do, writing methods which might be used. This is one of the benefits of writing unit tests firsts, it helps you do just what you need to pass the test and what this really means is you write code to your requirements and not any extra (and not needed) code.

Simplicity and less code is the way forward my friends.

I can't remember where I read/heard it but I think it was A Josh Bloch interview and they asked him if he had any advice to give to new programmers, his advice was "if in doubt, leave it out". I didn't really understand it to much at the time but I think I am beginning to. They also asked how he had been going on a project and he said "pretty good, I took out 40 lines of code today. I don't judge my work on how much code I write but how much I take away these days".

So take the code out before you put it in, a pre-emptive strike.

2 Comments:

  • Aka YAGNI...

    By Blogger DerelictMan, at Wed Aug 02, 03:19:00 pm 2006  

  • That sort of function is one place where I think that most OO language designers get a bit lost. In my mind, there are two very distinct kinds of operations on an object: ones that are fundamental to the object, whose code is implementation-specific, and which cannot readily be simulated if missing; and ones that are (or reasonably could be) derived from the public interface and are independent of the implementation details.

    The GoF "Decorator" pattern attempts to address this, but I look on that as a kluge which is necessary only because the language does not provide a way to write a sharable method definition that is bound to a specific type but is optional, not part of the type definition, and restricted to the type's public interface.

    The Dylan language went to the opposite extreme, making all methods separate from the type itself. Ruby is one of the languages that is slightly less radical, allowing methods to be part of the type but also allowing anyone to temporarily add methods to the type; the problem that I have with Ruby's technique is that it exposes the entire object to the added methods, not just the public interfaces.

    By Blogger Doug, at Wed Aug 02, 05:37:00 pm 2006  

Post a Comment

<< Home