Clean Code: One Size Doesn’t Fit All

Yuyi Kimura
Dev Whisper
Published in
3 min readJul 26, 2024

Clean code isn’t just a buzzword or some hot topic that arise now and then — it’s a critical element in building maintainable and scalable systems. Pairing it with thorough code reviews, it becomes an incredible asset for any development team, enhancing both the quality of the software and the efficiency of the team.

To make a pile of code blocks represent a tower, you have to adapt some pieces. Generated by AI
To make a pile of blocks represent a tower, you may have to adapt some pieces. Generated by AI

What is Clean Code?

Clean code is not following a style guide or some naming convention, although these are very important aspects. At its core, clean code is about writing code that is easy to understand, maintain and extend. It emphasizes clarity, simplicity, and consistency, making it easier for developers to understand and work with.

Books like Robert C. Martin’s “Clean Code” and Steve McConnell’s “Code Complete” provide extensive guidelines on writing clean code. Both of these books offer valuable insights, but it’s essential to remember that they are guidelines, not commandments.

The real essence of clean code lies in the ability to communicate the intent of the program clearly to other engineers (yes, including your future self). However, the interpretation for it can vary depending on the project context.

The Importance of Context

To understand the importance of context, let’s consider Google, one of the world’s leading tech companies. Their approach to clean code demonstrates the importance of context when writing code.

At Google, developers often need to sacrifice certain aspects of readability for the sake of performance. Why? Because they are building tools and services that are used by millions of users concurrently, requiring every bit of performance they can squeeze.

Within this context, writing “clean code” requires you to take a different road (but you should reach the same goal). This might involve:

  • Using less intuitive but more performant data structures.
  • Implementing complex optimizations that may reduce readability.
  • Favoring efficiency over conventional coding patterns.

This doesn’t mean the code is “unclean”. Rather, it’s clean within its specific context, optimized for the constraints and requirements of large-scale, high-performance systems.

Following every fancy article or book from renown engineers may be tempting but remember that your role is not to blindly apply these suggestions (which by no means are bad), but to:

  1. Understand the implementation context of each project.
  2. Identify the most critical requirements (e.g., readability, performance, security)
  3. Apply appropriate coding guidelines and patterns that align with these requirements.
  4. Balance different aspect of code quality to achieve the best overall result.

This approach requires critical thinking and often involves making trade-offs. Sometimes, you might prioritize readability and simplicity. Other times, like in Google’s case, you might lean towards performance optimizations.

The Value of Code Reviews

Code reviews play a crucial role in maintaining code quality within a specific context. It serves multiple purposes:

  1. Ensuring that every piece of code introduced adheres to the specific coding standards (some of these can certainly be automated through linters and formatters).
  2. Catching bugs and potential issues early.
  3. Sharing knowledge among team members.

Let’s include a fourth purpose, which is confirming whether or not you reached the goal of writing understandable, maintainable and extendable code.

While code reviews focus on specific requirements and constraints of the project, isn’t it the best place to consider if my fellow team members are able to work with this piece of code introduced?

Reviewers should understand the context in which the code operates and evaluate it accordingly. Only then, code reviews evolve into a crucial piece for building software.

Conclusion

Building great software is inherently a collaborative effort, but aligning multiple minds towards the same goal is often more challenging than it appears. Writing “clean code” is, in essence, a team endeavor. It begins with one engineer crafting code that they believe aligns with the project’s constraints and coding best practices. The process continues as another team member reviews this code, offering valuable feedback and insights.

By understanding the unique needs of each project and applying clean code principles judiciously, we can create codebases that are truly “clean” in the ways that matter most for their specific contexts. This nuanced approach to clean code and code reviews leads to more effective, efficient, and maintainable software systems.

--

--

Yuyi Kimura
Dev Whisper

Full-stack software engineer and machine learning enthusiast.