Clean Code

Featured image

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:

code 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. batman

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. dont

KISS Principle

kiss 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:

scout “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.

References here and by here

Happy coding!

Beer!