top of page
Writer's pictureCristian Duque

What Is Clean Code?

Updated: Oct 2, 2023



Clean code is a concept introduced by Robert C. Martin in his book "Clean Code: A Handbook of Agile Software Craftsmanship" which defines a set of principles for writing better code.


As Robert C. Martin says: "Clean code is elegant and efficient (no wasted cycles, no bad code to tempt into a mess). It's focused, simple and direct."


It aims to build well-crafted systems that align with the business requirements, and its simplicity makes it easier to understand and read.


As Michael Feathers, author of Working Effectively with Legacy Code, summarised:

"Clean code is code that has been taken care of. Someone has taken the time to keep it simple and orderly. They have paid appropriate attention to details. They have cared."


Clean code is a concept that is widely debated and has profound implications. For that reason, we´ll take a look at some fundamental ideas:


BIG IDEAS

  • Clean code follows a clear order of execution. It´s like a top-down narrative that logically interconnects concepts.

  • Functions are small and predictable. They promise to do one thing well with unintended side effects. Modules are reasonably divided into sections.

  • Well-written tests are the direct gateway to keep our code flexible, testable and maintainable.

  • Meaningful names for variables, functions and classes. The words should be direct, pronounceable, and searchable, describing the object's intent and why it exists.

  • Formatting and indentation are essential for code readability. Coding standards can be defined within teams to improve readability and extensibility.

  • Regular code refactorings to improve code quality without changing the system behaviours.

  • Clean code is also robust. It handles unexpected situations by using error-handling approaches such as exceptions.

  • Comments should be avoided as much as possible. The code should be clear enough to express the intent's system. Comments may be used to amplify the importance of something.


THREE CLEAN PRINCIPLES

  • KISS (Keep It Simple, Stupid): First noted by the US Navy in 1960, it aims to create systems as simple as possible. It favours simple solutions over complexity.

  • DRY (Don't Repeat Yourself): A principle to prevent code duplication and redundancy by writing code once within the overall project.

  • YAGNI (You Aren't Gonna Need It): States that features should only be added when required.


WHY CLEAN CODE?

  • Developers are authors. We spend more time reading code than writing code. The ratio of time spent reading vs. writing is around 8 to 1.

  • The flexible design facilitates business requirement changes and the introduction of new features.

  • Code that never obscures the system's intent, a well-defined path to solving business requirements.

  • High-quality code is the Fastlane to continuous delivery, referred to in the DevOps term CI / CD (Continuous Integration / Continuous Delivery). Poor and dirty code leads to the Slowlane, where the CD is slower the next time.


CONSIDERATIONS

  • 'Dirty' tests are worse than not having tests. Tests solely for code coverage lack purpose and obscure system failures.

  • "The structure of software provides the basis for an evolution based on new requirements. Without structure, the costs for new features increase exponentially. First the analysis and design, then the implementation." What is Clean Code? - Smartpedia - t2informatik

  • The DevOps world faces a huge challenge to integrate developer demands. The estimated number of developers in 2024 is 28.7 million. Imagine a developer writing ten lines of code daily: mindblowing stuff.

  • Senseless code transforms into an uncontrollable mess that leads to zero productivity.


Clean code is not a list of rules; it's an ongoing process that requires discipline and practice to write better code.



BENEFITS

  • Code flexible, scalable and maintainable that saves time and money.

  • Clear, concise and easily understandable code that can be expressed in terms of business requirements.

  • It leads to faster development in the mid-long term by reducing time spent on debugging, maintenance, and covering technical debt.


DRAWBACKS

  • Hard to deal with legacy codebases that may be time-consuming to refactor and adapt.

  • Large teams may struggle to implement clean coding practices because it requires training and coding standard agreements, leading to heated debates from different perspectives.

  • Learning Curve: It takes time to acquire a clean-coding mindset because it involves constant improvement and practice.


Check out my video:


6 views

Comentários


bottom of page