Good Code: Part Five of Five – Maintainable Code

Welcome back to our five week series, where we explore five aspects of code that should really be fundamental to code being described as “good”. This week we bring you the lucky last in the series, part five. So far we have covered Correctness, Understandability, Efficiency, and Testability.

Much of the series is built upon Sara Falamaki’s talk, entitled “Happy Hackers == Happy Code”, which Eric attended at Linux.conf.au 2010 in Wellington.

Feature Five:  Good code should be Maintainable.

Good code must also be maintainable. Generally this means that it must be easy to understand, but there’s more to it than that.  Code needs to be built in such a way that any future changes aren’t a major mission to build in; this is known as flexibility, extensibility, or modularity, depending on exactly how you’re intending to change things.

For example, one of our recent developments was to take two years’ worth of data from a website, and process it into a single Excel file.  This worked fine for 6 months, until the client asked us to extend the amount of data from 2 years to 5 years. Fortunately, we’d created maintainable code, and this was a matter of making a very simple change to only one line of code.

Maintainable code also means that it needs to be structured in an understandable manner.  Code maintenance often comes up after a very long hiatus.  By the time any changes are required to the codebase for a completed project, months have probably passed.  The code needs to be easy to understand (as above), but also very easy to mentally comprehend – you need to be able to jump straight back into it with a minimum amount of fuss. This often comes down to creating good clear structure, good separation of functions, and good comments, AS WELL AS self-documenting code.

Read more about self documenting code in this excellent answer on StackOverflow: http://stackoverflow.com/a/209089

So there you have it, a brief basis of what ‘good’ code is. There are a number of articles on the web that discuss the topic in greater detail, many of which you can find with a quick google search.