˟

[Programming] High-quality Code

Apr. 12, 2021

In this post, we are going to talk about recommendations for writing quality code.

Why Code Quality is important

The very first question you should have is that why this is an important topic that we need to look at. What do we mean by code quality? Do we look at how well the program is built or do we pay attention the the concrete code itself?

In my own opinion, having a design and architecture that are suitable for the requirements and sufficiently simple will lead to the possibility of writing quality code and vice versa, implementing code that are readable, clean, and performant will enhance the design of the application.

We are going to focus on the code itself, since it is up to the developer to write the code. The comprehensibilty of the implementation and the readability of the code are vitals, but often overlooked as long as the general operations (or the entire program) function correctly. Each teams/organizations has their own standards and philosophies on the matter. As such, we are going to discuss what is generally accepted by most.

Characteristic of Quality Code

First and foremost, quality code is easy to read and understand. How many times have you look at someone codes, and become frustrated, not understanding what’s happening. How many times have you look at your old codes and wonder why did you do that in the first place, with a little hint of shame, thinking what if anyone else could have read this. Quality code is also maintained easily, straightforward, and well tested. And lastly, it must have strong cohesion and loose coupling.

Well, realistically, this cannot be avoided. Writing clean, quality codes takes time and efforts. And those usually what you don’t have when you need to catch up to your deadline or when you work at your own leisure. What you can do is trying to improve your own code whenever possible and follow rules (when you have to consider and remember about tons of other things).

Next, I am going to talk about some practices and discuss my opinion on whehter they are effective and helpful.

Coding Conventions

A coding convention is a set of rules for writing code, used within the boundaries of a particular organization or a project.

This practice usually emerges in big and serious projects. Hence with my own experience, and most other developers, this is not strictly enforced. More often than not, developers are already trying to follow conventions that are not too far off from the company conventions, so following the convention is not a challenge. Is this particular practice helpful? Well, to some extent. What you will encounter is that a large chunk of the code follows that convention with exception here and there that does not affect the quality of the program as a whole.

Coding convention may seems popular with all the avaible conventions and stressing on using them but in reality, they are not.

Managing Complexity

This practice may be one of the most important but also the hardest one to do. The main objective is to reduce the complexity that each piece of code has to deal with, and in turn, reduce the complexity that each of the team’s member has to deal with at a certain moment.

Complexity management should be applied at all levels - classes, methods, operators, error handling. Developers usually have to deriving algorithms that takes complexity into consideration and focusing on small pieces of code while abstracting away from the big picture, which leads us to the next part.

High-Quality Methods

A method should solve a small problem. With methods, the overall complexity of a task is reduced. But how simple should a method be? There’s no fixed answers or rules for writing high-quality methods.

One way you can use to gauge is how long a method is. Througout the years, research has been done regarding the optimal length of methods, but a universal formula has not been show. In general, shorter methods not longer than a single screen should be prefered, since it can be read without scrolling and simplified their readings and understanding.

The longer the method, the more complex it becomes. If it’s longer than a single screen, we should think whether we can split it up into a few simpler methods. However, if you are implementing a complex algorithm and consequently come up with a longer, meaningful method, which does one thing and does it well, the length is not a problem.

To be continued …