Writen by Suane Vallim, in 01/10/2025
4 minutes of reading
Clean code: How to write clean, efficient code, and why this is important
Clean code is not just a matter of style, but an essential practice to ensure the quality and sustainability of a project over time.
Have you ever worked on a project that has been going on for a long time, with the contribution of several developers (or even someone with little experience), but without a consistent code standard or documentation? Perhaps you have encountered variables and functions whose names were so vague that it took you hours to understand their real purpose, such as a variable called “x” or even something absurd like “banana”.
Situations like this are more common than we’d like, and they have a direct impact on productivity. Time that could be spent creating new functionality or solving critical problems is spent trying to understand poorly structured pieces of code. In addition, the lack of clarity and organization increases the risk of errors, duplication, and rework, making it difficult to maintain and evolve the project.
The concept of Clean Code, popularized by Robert C. Martin in “Clean Code: A Handbook of Agile Software Craftsmanship,” proposes practices and principles that make code more readable, understandable, and maintainable. Clean code not only improves productivity, but also facilitates teamwork, reduces the accumulation of technical debt, and promotes the delivery of higher quality software.
Given the importance of this topic, this article briefly explores what clean code is, why it is so important, and how it can be applied on a daily basis. In addition, I will provide some practical examples to facilitate understanding and show how to write code that any developer – including you in the future – will be grateful to find.
What is clean code?
Clean code is code that any developer can easily understand, regardless of whether they have worked on the project before.
According to Robert C. Martin, clean code should be:
- Simple: Solve problems as directly as possible.
- Expressive: Use clear names for variables, functions, and classes.
- Focused: Each function and class should do one thing, and do it well.
- Testable: it should be easy to test and have good test coverage.
- Duplication-free: avoid repeating the same code in different parts of the system.
Practical examples of clean code
Meaningful naming
Confusing or abbreviated names, such as “a”, “b”, or “bc”, do not make the purpose of a variable clear and make the code difficult to read. Instead, choose names that clearly describe what the variable contains or represents.
Functions with clear names
The name of a function should be clear and describe exactly what it does. This makes the code easier to read and maintain. Whenever possible, choose specific names for the functionality that the function performs, and avoid generic names that can lead to confusion about its true purpose.
Use two or less arguments in functions
This concept is fundamental to increasing the clarity of your code: reduce the number of arguments in functions. Ideally, a function should have no more than two arguments. If necessary, group the data into an object rather than using multiple primitive arguments. This makes the function easier to understand and maintain, and improves readability by reducing parameter complexity.
Functions should do one thing
Each function should have a single owner. This makes the code simpler, easier to understand, test, and maintain, and improves reusability and readability over time.
Avoid obvious comments
Comments are useful for explaining complex pieces of code or not-so-obvious decisions. However, it’s important to avoid comments that just repeat the obvious, such as “increase the value of x by 1”. This helps keep the code cleaner and easier to understand.
The examples above illustrate how clean, concise code can make your work more practical and efficient. Adopting clean code principles is not just a matter of style, but an essential practice for ensuring the quality and sustainability of a project over time. Clean code facilitates developer collaboration, reduces complexity, and improves system maintenance and evolution. By following best practices such as meaningful naming, single responsibility roles, and reducing code duplication, you create a more efficient development environment where everyone wins: the team, the product, and the developer.
Remember, writing clean code is not an easy task, but it is essential to ensuring the quality and longevity of a project. By writing code that is more readable and easier to understand, you will not only avoid problems in the future, but also make your working life much more peaceful and productive.