2 min to read
Clean Code
Clean code
Howdyyy? Today’s post is dedicated to Clean Code, let’s review some practices and principles. While we probably already know why it is important, let’s take a look at some of its advantages:
- Readability and maintainability: to be able to read, understand and modify more easily and safely code that has not been written by us. It is good to rethink if what we write could be interpreted and modified by someone else.
- Team collaboration: facilitates communication and cooperation between teams. If we establish code standards, and write code that is easy to interpret, understanding the work of different devs is easier and promotes more effective collaboration.
- Debbugging + bug fixes: allows us to locate and interpret better, if we name variables in a meaningful and representative way, if we have a clear structure in the code, we facilitate the identification and detection of errors and their resolution.
Now that we have seen some advantages, let’s get to it!
Comments in the code
Good code does not need comments. Variables, methods and any other code components, such as attributes, should have easily identifiable and descriptive names.
Conditionals
Positive conditionals are easier to read than negative conditionals, so we must consider their interpretation as easy as possible. In case of evaluating more than one condition, we can help that readability, generating a constant with a meaningful name about what we are evaluating and apply it directly in the condition.
Magic numbers
We can avoid “magic numbers” or hardcoding of numbers if we store them in constants that represent what that number refers to or what is the purpose of using it in our code.
Functions and cyclomatic complexity
We can avoid huge functions with a lot of logic if we divide them into smaller functions that only take care of a single task (remember the single responsibility principle).
DRY Principle
Don’t Repeat Yourself. Avoid duplicating and writing the same code more than once. Instead, it is more convenient to reuse, share that code through functions, methods or modules depending on how much we need to abstract. It also helps us to be more consistent and reduce the risk of bugs, since if we need to modify or update something, it is only done once and in one place.
KISS Principle
Keep it simple, avoid unnecessary complexity, promote simplicity. Often less is more. It is important that we keep our classes and methods optimally.
Boy scout principle:
“Always leave code cleaner than you found it.” Whenever we detect code that we can improve, even if it is not part of our changes, either for simplicity or for readability issues, go for it. Today for you, tomorrow for me.
Happy coding!