Build it in a way that you can improve it later

(written by Lawrence Krubner, however indented passages are often quotes)

Some code is junk and needs to be re-written from scratch. Yet no code gets everything right the first time, so some re-writing must be done, without re-writing everything from scratch, since the re-write will also get things wrong. The ideal has to be write code in a style that makes it easy to incrementally fix the mistakes that you are currently making, even though you don’t know you are making them.

I Point the Finger at You, I Have Already Pointed it At Myself

It could be that everyone my age, or everyone who had a similar career path, or everyone that read documentation from Sun when they were too young to know how horrible such documentation actually is have all ended up making these same mistakes. I toiled away on a project for four long years that heavily leveraged the now universally despised EJB 2 specification. I myself have drawn out huge UML charts that ended up being printed as a shockingly large sheaf of pages that were then taped to a whiteboard. I personally have to live with the shame of a very clear and vivid memory wherein I circled objects that covered nearly four sheets of this paper and wrote, in my own hand and with a clear-head: “A good candidate for an abstract factory! :)

Eventually I realized that the emoticon was mocking me. Laughing at my presumptuousness, laughing at my premature optimization. Laughing at my fear of re-factoring.

Premature Optimization and Re-factoring

That’s what I was doing, I was trying to solve a problem before I even had it. Before I had even written one line of code, I was trying to decide how the pieces of the puzzle fit together. It was simply too much detail too early on. No one ever gets it right the first time, what made me think that I was so special?

I’m not saying it’s not worth it to think ahead and to plan, but be mindful that no one has perfect knowledge of their problem space and just as things that seem simple can turn out to be complicated, sometimes problems that seem really hard end up solving themselves. Maybe that’s just how business logic works: you talk to the right person and it turns out that you had it wrong the whole time.

Source