Rob Cook


Home | Donate

On Rewriting Code


2020-01-08

For a discipline renowned for an obsession with re-use, the refrain "let's rewrite it" is a commonly heard one. I find myself once again at the leading edge of a product rewrite. This time round I advocated against it, in the past my inexperience and enthusiasm would have me in favour.

Rewriting an application from scratch gives you unparalleled design freedom, but it also abandons all of the accumulated knowledge the code represents. All those pesky edge cases, quirky client specific behaviours, and strange workarounds. Viewed through the lens of a rewrite, these things are impurities to be cleansed by the new design. They will of course ultimately reappear in the new land of clean code, albeit in a different guise. Time pressures, misbehaving third party services, and those darn users and their perverse requirements are ever present. Nothing stays pristine for long.

Despite my reticence and desire to atone for past sins, a rewrite in this case might well be justified. The code lacks any useful level of organisation, descending into spaghetti in places, and there are no automated tests or documentation to speak of. Yet the product works, new development is possible if unpleasant, and paying customers are asking for new features.

A certain level of pragmatism is necessary if a rewrite is to be successful. Dreams of clean lines and cleverly layered architecture should be set aside. Accept that rough edges will be present, and design your processes to mitigate and minimise them. Resist the temptation to design from the outside in. Don't architect a beautiful box only to find the parts don't fit inside.

Top down design should be a heuristic not a rule set. Use the vision of a clean system to guide the bottom up building of working code that actually does something. Avoid getting lost in layers of abstraction that are often no more than the product of mental masturbation. Keep it simple.

end