Reuse Code Or Code It Yourself?

2008-11-05 08:48:00

Posted by kdawson on Tuesday November 04, @08:02PM

from the good-or-great dept.

Programming

eldavojohn writes "I began coding for a project that had simple requirements

for my employer Web services and a test application for them. But

requirements have been creeping, as they always do. Initially I had decided to

use the Spring Framework with Hibernate. And I re-used a lot of libraries that

made things simple and quick for me. The new requests coming in involve

capabilities beyond those of the frameworks. Now, I used to be told that good

programmers write code and great programmers reuse code. It's starting to look

like I would have saved myself a whole lot of time if I had written the

database transaction using JDBC instead of Hibernate now that I'm married to

this object model framework, some of this stuff doesn't look doable. So what is

better for the majority of software projects out there: reuse code, or code

from scratch? What elements or characteristics of a problem point to one option

over the other?"

It's not rewriting code or reusing code that makes you a great programmer. It's

knowing when to rewrite code and when to reuse code that makes you a great

programmer.

- the first case is trying to make the best of what you have and building on

that

To add to this, never ever simply "reuse" code; if you just copy and paste

you'll end up with a horribly designed hack job worth of TDWTF. Instead, take

what you have and massage it into place.

In this particular case, it sounds like your design no longer hashes well with

the requirements. My suggestion is to start rewriting/reworking isolated

portions of the code base and create a temporary compatibility layer (if

necessary) to interface with the old code. Continue doing this until the entire

application is converted over to the new design.

Did he really make a mistake? He already has one version out and is now finding

out what the software really needs to do. That sounds pretty damn successful to

me. Now he's dithering about rewriting and replacing some of the libraries,

which he would have had to do anyway even if they were custom-written.

If he had written everything from scratch:

existing libraries that he used?

needs were actually quite typical, and well-served by existing libraries?

His original choice was correct. As far as replacing Hibernate, he's in just as

good a position now as he would have been if he had written his own persistence

layer. Remember, he chose Hibernate, which turned out to be unsuitable, because

didn't know the requirements when he started. He would have been damn lucky if

he had just happened to develop a custom persistance layer that neatly

anticipated the unknown requirements. So, ripping out or rewriting the

persistence layer was inevitable. If you're going to throw code away, it might

as well be code that you got for free, instead of code that you sweated and

toiled over.

If you're really dead set against replacing or rewriting any libraries, the

only way to improve your odds is to use the biggest, oldest, kitchen-sinkiest,

most bloatiest library you can find.

-----

On the spot!! If the "poster" hadn't reused code from the get-go he was

probably still coding. But instead of now making a choice between "hanging in"

and "starting to rewrite", there is a third and IMHO better option: Throw out

the framework that doesn't fit anymore and replace it with a better fitting

one. Don't jump immediately into the "and now I've to write it all from

scratch" excuse.

Your original "reuse" decision was the right one, but appearently you didn't

pick the right framework. Which is normal, because at the beginning of the

project you didn't know exactly what the requirements would be. Based on your

improved knowledge of the application, you should redo your investigation of

frameworks and find one that has the right fit.

And if you can't seem to find one that fits, you need to do some deep-deep-deep

thinking. Is your situation really so unique that nothing "standard" out there

will fit in, or is it the too often happening "not invented here" syndrome.

Don't fall into the trap of "I can do it better myself". Yes, I'm sure you can

:-), but you won't have the time!! Or better said, your boss won't allow you

the time.

-----

If you'd tried to write it all yourself from scratch from the beginning you'd

still be coding and you wouldn't have gotten the feedback about what needs to

change as quickly. Prototype quickly then optimize later.