Let’s face it, we all know that when we sit down and start coding anything, the requirements are going to change and once we have released the code into the wild our clients are going to want updates. So the real challenge with coding is writing something that can be easily maintained, whilst remembering that it may not be ourselves doing the maintenance.
Maintainability is probably the single most important thing in software development, so why is it that so often code becomes difficult to maintain, and the older the code gets the more difficult it becomes to maintain? This post will give a very brief overview of some common techniques that should be remembered to help improve maintainability.
Meaningful and Consistent Naming Conventions
The first thing to remember is that someone else may be reading your code, and thus it is important that they can understand what a class, method or variable is doing based on the name given to it. This is particular important with larger projects where developers need to be able to find functionality quickly, otherwise maintenance costs will increase.
There are quite a number of standard naming conventions around, so once you have picked one, stick to it. If possible use tools that help you stick to those conventions.
Remove “Code Smells”
One of the major problems with code is that is can be full of “code smells” and over time the code can rot more and more. Some of the common code smells are
- Duplicate code
- Large methods
- Large classes
- Excessive commenting
- Conditional complexity
- Dead code
For a fuller list with explanations read the Code Smells article on Coding Horror.
Separation of Concerns
SoC is about breaking the software into distinct pieces of functionality. On a simple level this may mean separating the UI layer, the business logic layer and the database access layer. If functionality is not separated out then it becomes difficult for developers to pin point the area they need to update during maintenance. It is also likely that this mixed functionality will lead to making changes more complicated, due to code being reliant on other areas of code.
A major benefit of SoC is that it generally leads to much smaller methods and classes, thus improving the readability of code. It also improves the ability to test each area of functionality.
Coupling is the measure by which each object relies on other objects. If objects are loosely coupled a change to one object will have a minimal effect on all other objects. One way of loosely coupling objects is to follow the Dependency Injection pattern.
Testing is an essential part of software maintenance. If code is not covered by tests then it becomes difficult and time consuming for developers to check if updates have caused knock on effects in other areas of the software. Ideally a developer should be looking to practice Continuous Integration, and a build server should run the tests whenever new code is checked in. This way the team members will know quickly if the build has broken and problems can be resolved quickly.
There are many more areas to creating maintainable code that haven’t been mentioned above, the purpose of this post is to act as a starting point.
Please lets us know how you ensure your code is easy to maintain.