DEV Community

Lance Bachmeier
Lance Bachmeier

Posted on

"Don't Rewrite Code" is bad advice

I have read many times the advice that you shouldn't do a rewrite of a working program. I think the idea was popularized many years ago in a blog post by Joel Spolsky. That's probably reasonable advice for a million-lines-of-code program you're using in production. It's bad advice when your programs is only a few thousand lines. Here are a couple of reasons rewriting code is a good thing.

Freedom to rewrite makes you more productive. If you know in the back of your mind that you can do a rewrite if it doesn't work out, you won't try to engineer a perfect, general solution on the first go. The critical step is writing a working program, complete with tests, that does exactly what you need it to do. You want to silence the voice in the back of your head that keeps asking, "What if I decide to add X in the future?" Once you give in to that urge, you'll be going down rabbit holes and spending long hours engineering solutions for problems you don't have.

Rewrites are cheap. Your time, on the other hand, is not - don't waste it writing programs that don't provide an immediate return.

You can rewrite and test a lot of code quickly. I can knock out a lot of (fully tested) code in a couple of hours if I'm doing a rewrite. I can reuse much of the earlier code, and once I understand the logic, I can do a much better job. And it can be hard to write good tests while you're trying to define the problem. When you start a rewrite, you know what you're trying to accomplish, and that makes it a lot less challenging (and faster!) to write good tests.

Top comments (0)