Practical Clean Coding: A Comprehensive Guide to Maintainable Code

This is the first article in the “Practical Clean Coding” articles series.

1. Introduction: The Art of Writing Code Humans Can Understand

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

Clean coding is a mindset and a set of practices to achieve better code readability, maintainability, and simplicity.

When applied correctly, it should lead to a better code design and an easy-to-understand code structure that allows you to make changes more efficiently and improve performance when needed. It has many other benefits.

It is essential for developers because it makes maintaining a codebase easier and avoids bugs earlier than in production.

It is essential for teams because if each team member generates clean code, their velocity will increase, and operational costs caused by bugs and changes will be reduced.

It is essential for businesses because it will enable more productive and adaptive teams, reducing costs and increasing profit in turn.

I experienced the benefits of clean coding during my programming career. I have also encountered numerous challenges due to the lack of clean coding. I saw unnecessarily cryptic codes that were impossible to touch to make even a tiny change. I saw terrible tests which are supposed to guarantee the correctness of the system, but it is doubtful that they are correct. I saw mind-blowing inheritance hierarchies.

I generated low-quality code in a certain period of my career. However, I deliberately tried to improve myself and learned as much as possible from my mistakes.

2. The Importance of Clean Coding

Clean coding practices have several benefits. It deserves its article, but let’s summarize them under a few categories:

  • Readability: When you follow clean coding practices, you will produce more readable code both for your today self, your future self, and other teammates or someone who will maintain the code.
  • Maintainability: Your code will become easier to maintain. You can’t avoid changes or fixes, but if your system is easy to change, you can apply changes and fixes more quickly.
  • Efficiency: If you can understand the code better, you can eliminate redundant parts. It would also be easier if you need to improve your code performance.

I experienced these benefits when I worked on a project to migrate a legacy application from a monolithic code base to its microservice code base. The monolithic application was a nightmare to maintain. We had user stories and feature requests over five years but couldn’t implement them because the codebase was untouchable.

Why is that? Clean coding was not the priority then because there was a rush to release things in a stressful start-up environment. I understand that, and I can relate. However, I want to emphasize the importance of clean coding and the effect of the lack of clean coding in the real world. I’m sure that the non-clean structure of the code leads to many bugs eating hundreds of developer hours, which effectively makes things slower.

We drastically improved team efficiency after switching to the microservice, which is a cleaner codebase. We are delivering with a higher velocity. Even our new teammates can complete tasks in the new codebase in a short time. No particular expertise is needed anymore! No history of the code knowledge is required anymore! Knowing the domain is enough to start working on it.

3. What to Expect from the Series

I plan to talk about my experience-based opinions on clean coding in this series. Each article will discuss a single specific approach below the clean coding umbrella. There will be actionable, practical advice and counterexamples to draw the picture more clearly. Hence the name: Practical Clean Coding.

I’ll share my stories, primarily painful ones, related to the discussed topic. I’ll try to keep it in a story-telling style to not bother the reader with too much technicality and non-practical discussions.

Even though I plan to use Java/Kotlin for demonstration purposes, the series will mostly be language-agnostic and experience-neutral. So, every developer should gain some benefits from the series, from beginners to seasoned developers, especially if they are using a C-family language.

4. Upcoming Topics

I plan to cover the important topics and observe that are commonly misinterpreted or overlooked. Here’s a preview of some topics I plan to cover:

– Naming

– Comments inside code

– Tests with assertEqual only

– Avoiding inheritance

– Using static code inspection

– Good unit testing

– Good documentation

– static vs non-static

– final vs non-final

– private vs. non-private

– Optimal class field count

– Better interfaces

This is not a complete list, and I’ll add or remove topics in the future. If you want to read about a specific topic from the list or not, please let me know in the comments.

5. Join the Conversation: Your Thoughts Matter

Subscribe not to miss the following articles in the series and others from me.

I need to hear your comments and thoughts on the topic. Please comment and share your ideas. What do you think about this series? Which topics would you like me to cover in the following articles? Please let me know.

You can also follow me on other platforms listed here: https://mdemirbas.com/about

6. Conclusion

Practical, clean coding is a necessary mindset and skill in a developer’s skillset. Deliberate learning and practice can improve it. Let’s explore together!