What makes the code good?


Can you guess which code is better?

It’s quite easy. Do you ever wonder how many curses colleagues said looking at your code? What should your code be to not makes your colleagues crazy?

https://gifdb.com/images/high/computer-not-working-0h085udgx0idjy9r.gif

When you ask the “internet” ‘what is good code’ you will get many characteristics, such as readability, simplicity, consistency, maintainability, efficiency, testability, modularity, scalability, well-documented, etc.
All are important, but let’s consider on which we should focus on? In my opinion, the ones below:

  • code is easy to understand

When you look at the code, you should easily read it and understand. Why is it so important? According to the statistics, we read code 10 times more than we write it. I think it’s much more these days. You don’t want to spend hours or days deciphering and struggling to understand what the code does. It’s a huge waste of time and money for you and the company. Code that can be easily understood can be effortlessly improved, refactored, or rewritten.

  • code that reflects the requirements

It’s not easy. In many projects, I’ve seen a lot of code that no one knows why it exists. I bet you’ve seen many times code that, for example, removes an item from a list and no one knows why, or code that adds some constant or function that does some magic.

As a programmer, you write code to reflect business or technical requirements, so … when you read the code, you should know what the requirements are. I know this is difficult because many languages, including Java, are primitive (low-level programming). We write code using technical/primitive instructions, but we want to express business/high-level requirements. It’s really difficult, but we should try. In a separate post, I will describe what we can do to reflect the requirements in the code.

https://geekandpoke.typepad.com/geekandpoke/2012/01/coding-is-an-art.html

The above two: understandability and reflection of requirements, are the most important aspects of good code. I’ve added one more that makes the code even better:

  • simple to test

Code should be easily testable with unit tests. This enforces other features such as simplicity, clarity, etc. If the code is easy to test, the code itself must be simple, clear, and understandable. I’ve been refactoring a lot of code lately, and I can say that this is absolutely true. Simplicity is the key to all aspects of great code.

https://www.monkeyuser.com/2018/applied-tdd/

Final words

In one project, we were responsible for maintaining the application, mainly fixing bugs. The code was unnecessarily too complicated. We spent hours and days debugging and troubleshooting. No one understood what was going on in the application code. It was a nightmare. Fortunately, it only lasted three months, because I was close to quitting the project.

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler.


Leave a Reply

Your email address will not be published. Required fields are marked *