Thoughts from KMC

How to write robust code

Elvis Mulinzi Thelemuka

01 Nov 2018

How to write robust code

In the world that we live in, technology is slowly taking over. People are using technology for almost everything, several apps are out there helping us on day-to-day basis, but the sad thing with it is most if not all of those apps are not bug free. There was a time when scientists thought it would be possible to write bug free code but they soon realized that it was not that easy to achieve.

One of the reasons bugs are found is due to multiple dependencies used in the code and its size. When the project starts, the developer scans and manages the code easily. As time passes, the application gets bigger and bigger. If not careful, the developer can introduce complexity in their code by using multiple dependencies.

The most dangerous bugs are the ones you cannot spot right away, the ones you know exist but cannot find because they occur randomly.

A project with a lot of developers has a high chance of turning into a bug show project. Each developer is different, each developer has his own way of coding, people use different conventions, and when a large number of people are working on the same project, that increases the chance of ending up with an end product that has bugs.

With all that being said, we should mention that it is not impossible to write bug free code. Bug free applications can be achieved if some simple guidelines are followed during development, such as:

Have a proper design

It is or should be common sense that every project should have an analysis period, usually the analysis is used to come up with a proper design for the product. When a developer is given a task without proper requirements or proper design, it makes it difficult to write code efficiently. Without a proper design the developer can find him or herself in a black box and do not know exactly what is expected. The developer will write a piece of code, and will change several and will end up introducing bug while if the developer knows exactly what to do and properly guided, the bugs can be kept to the minimum if not zero.

What does it mean to have a proper design?

A proper design covers all aspects, all cases, all situations, a proper design ensures that the requirements are not redundant and are not contradictory to one another and the developer is able to understand the design.

Ensure your code base is neat and tidy

Yes code can be neat, just like it can be dirty. Having code that is neat is having simple code, it is when the code is written in simplest way possible and is easily readable. By looking at a function or method you can easily know what the method is about. In a very large project, neat code is all the above as well as a neat integration between different components. Different components are integrated in a way that it is easy to understand and you do not need to scroll on a page or go one method to another to understand what is going on.

Follow a proper naming convention

To ensure that your code is robust and remains that way, the naming convention should be on point. Naming variables and methods plays a big role in writing code. It is helpful to whoever is going to support your code so that they will not break it or introduce bugs whenever the initial developer is not present. Correct naming can help the initial developer after sometime, the developer tends to forget what a particular method does and if it well named, it won’t be a problem.

Your code should be well tested

If your code is well written, then each method or class performs a specific function and has a specific objective. A better way to make sure that your code does exactly what it was intended to do is to test it. Writing unit tests will help you achieve that. Writing tests that test the behavior of each method or should I say each unit or piece of your code. Testing your code has a lot of benefits other than just ensuring your code is bug free.

Unit tests confirm that the behavior expected in that specific situation is the same behavior you get when that situation occurs.


Always make sure that your code does not break not matter what is the input. The only way to achieve this is to validate every input that is given to each method before any other action is done. As a developer you should be paranoid with validation so that you do not leave a room for NullPointerException and any other exception. This improves the quality of your code.

Basically one might say that writing perfect code is impossible but there are ways to get closer to perfection. There are many ways in which you can make sure that your code is bug free as mentioned above. It is sometimes difficult but with a lot practices, a lot of dedication and reading the right material. We just might achieve perfection someday.

Elvis Mulinzi Thelemuka