5 Tips to Maintain a Code Clean

A code is clean when it’s easy to read and easy to modify.

The worst thing to do is writing code and not caring about being clean, because it’s temporary, because I’m on a rush, or it’s a small fix. Because it’s a mindset which is installed on us, that makes our code quality getting worst.

In this article, I list 5 of the main points to keep a code clean, 5 methods which are easy to put in place, 5 points that I check while reading a Pull Request.

Organize Imports

Having a clean project starts by having clean imports. This means:

  • Organize the imports alphabetically
  • Remove unused imports
  • And avoid wildcard imports

With those 3 tips:

  • the imports will be more efficient, as no additional class is imoorted in memory that isn’t required,
  • And the merge requests are easier, as the order of the imports is the same for everyone in the team and this leads to less conflicts

Short Methods

Keeping methods short is a way to keep methods simple. A method should never be longer than 10 lines.

If i can’t fit a method on 10 lines, it can be beause the method does too many different things. In those cases, i try to group and export the logic in separated methods. And figuring out how to group and how to name the methods is a way to improve the readability of the code.

No Utility Methods or Classes

Utility methods or classes use to be where i put code i don’t know where it should go. It’s usually the trash methods.

When creating a utility method, i always use static method. And The inconvenience of usign static methods, it’s how hard they are to mock or unit test.

Instead i always try to use objects. This way, it’s easier to mock, i cab inject it with a dependency injection system, and i can store values in its variables instance.

Naming Convention

Methods With Verbs

Naming methods is always hard.

Methods do actions, so they must be named using verbs: build, fetch, compute, handle, find…

Fields With Nouns

On the other side, variables should be named with nouns. Singular nouns for simple variables and plural for lists or maps.

I must be able to firgure out what a variable is used for just woth its name.

Get and Set For Getters and Setters

There is a partuclar case with the verbs get and set. By convention, those are already used for getters and setters. So, avoid usign them for methods that are not getters or setters. Avoid using them when the method has more logic than just a getter or a setter.

Code Quality Checker Tools

All these code smells can be detected by tools like sonarqube. But knowing them, and applying them without the need of an external tool is being a step ahead in clean code.

Applying those techniques before any external tool is a way of writing good quality code. But those tools also anaylze ma’y other metrics, like test coverage, complexity, vulnerabilities and more.

Sonarqube can easily be setup to anaylze the code from a merge request and break a ci cd pipeline if the the code isn’t clean enough.

Conclusion

Keeping the code clean on a projet is a task that must be done all along the project’s lifetime. That’s not a task I can schedule for the next spring.

Those are points that must be taken into account by each of the members of the development team.


Never Miss Another Tech Innovation

Concrete insights and actionable resources delivered straight to your inbox to boost your developer career.

My New ebook, Best Practices To Create A Backend With Spring Boot 3, is available now.

Best practices to create a backend with Spring Boot 3

Leave a comment

Discover more from The Dev World - Sergio Lema

Subscribe now to keep reading and get access to the full archive.

Continue reading