Thursday, December 08, 2005

Reinventing the wheel is BAD!

I know you’ve probably been told that before, and it’s one thing I’ve told myself over and over again, but sometimes I just forget. I’m working on a soon to be explained, but I’ll keep quite for now, little side project in Java which requires creating beans and setting properties (hint). As I got down into the setProperty methods, I realized the rabbit hole is a lot deeper than I thought. The problem is, I will have a property name and a value that I want to find a method for in some given object. So I can look for methods with reflection, but a lot of issues arise. I have to look for a method that accepts not only the type of value I have, but also any classes it extends, the same goes for interfaces. Also, the bean may accept a primitive type, such as int, but for all sorts of reasons, you may have to wrap that value as an Integer object, which means I need to try to un-box those types (that’s what c# calls it, I can’t remember the java term) as well. After more than an hour, and after all sorts of methods, (setProperty is now becoming a chain of 4 or 5 methods and I’m not done!) I realized I better do some googling to see how this is done in say, Struts. I ended up looking into some apache commons code, initially reading it (dumb), when it struck me. What the hell am I doing? This code is written, and open source! Duh! So I scrapped my code and replaced it with one line of code using commons.beanUtils.

Moral? Reinventing the wheel is a waste of time! There are lots of GREAT open source solutions to many common problems, the apache commons project is just one of them, specific to Java programming issues. The great thing is, other than the fact that the code is already written, you or I are just one person, dealing with the issues of entire systems. These tools are written by groups of very skilled programmers, they are singularly focused to provide highly tested, well written solutions, which are probably going to be better designed than what you or I are trying to come up with. I spent 2 hours doing something that really only had to take 5 minutes! This is one of the core mantras of the book Better, Faster, Lighter Java, which I guess I forgot for a second.

3 Comments:

Anonymous Anonymous said...

I found that I've learned a lot from re-inventing the wheel, and gained more of an appreciation for well written components & frameworks because of it.

To me, there is nothing wrong with it, and everything right... as long as your manager knows what you are doing. If you have a deadline, and you're writing your own version of Spring, then that's bad.

12:42 PM  
Blogger Chris Scott said...

Solid point. Certainly trying to hash out some solutions to common issues for the purpose of education can be a great thing, especially when you take the time to then compare your work to some proven code. However, if that's costing your employer development time that in surely unnecessary, well... Long ago (in internet years) Larry Wall, the creator of Perl, had a great quote, "The three virtues of a great programmer are laziness, impatience and hubris". There's a world of knowledge in those words!

5:24 PM  
Anonymous Anonymous said...

I googled "reinventing the wheel is bad", and it is funny that most of the results followed that clause with a "but".

Anyway, just finished a blog on this topic. Though you may interest to read.

9:05 AM  

Post a Comment

<< Home