One of the biggest problems for programmers is to write clean code. When starting a new, exciting project, we usually forget about best practices, expecially when we have to deal with strict deadlines. In these cases, code cleaning is the last of our worries.
Writing clean code is fundamental though, especially for a future code maintenance, both our or, more important, of anybody. Producing clear code helps understand faster what the code does, and helps find the errors in an easier way. We want to propose you some tools that can be really helpful in writing clearer code.
Maintaining code cleanliness implies various precautions and measures to be adopted. First, the code has to be elegant. This means that it has to be easy and nice to read, from just a visual point of view: this implies a correct indentation, useful to highlight the different parts and logics. Also, the variables, classes, functions ad methods names have to be significant and expressive, so that who reads our code doesn’t need to search in it for their meaning or utility.
Comments should be reduced as possible. If functions, classes and so on were named correctly, it won’t be necessary to comment every declaration. One single comment should be needed to describe the general logic of a class or, for example, a particularly complex function.
This tool lets you format your code in a specific and automated way. By modifying the configuration file, it’s possible to specify formatting rules that will be applied to your code whenever you need it.
Prettier is easy to use, and very convenient for cleaning the already existent code. By using this tool, you’ll never waste anymore time or energy to adjust the aspect of what you’ve already written.
A “linter” is a type of static analysis that search for problematic patterns or code that doesn’t follow specific style guidelines. There is a linter for each programming language; ESLint is for Javascript. Being it a loosely-typed dinamic language, the tool comes in handy, because it is not necessary to run the code to use it.
This tool lets the programmer execute Git hooks. These are scripts that Git executes before or after a specific event, as commit or push. That means you can execute custom actions right before doing some actions on the repository, like executing tests or formatting the code.
As the name suggests, this tool lets perform linting on those files that are in the staging phase, right before a push operation. In this way, you can prevent the uploading of a file with errors or that doesn’t respect the style guidelines. A good strategy could be using Husky and Lint-staged together, by configuring with the first one the execution of a lint-staged script.
In a profession like our, where the handover of the projects is much frequent, it’s essential to code knowing that we’ll not be the ones that will work with what we’ve written. Writing clean and smooth code can be hard and time-consuming, but it’s fundamental. These tools we’ve presented offer a concrete help for this purpose.
Let us know if you use or know other tools, so that we can share the knowledge between us programmers!
All tools are recommended by freeCodeCamp