Write clean code, adhere to all conventions and best practices. A challenge for every beginner and professional. These tools are fully automatic for you clean.
Writing good and clean code is paramount in programming. If you or anyone else wants to understand your source code a few months later, it should not be the easiest to rebuild the entire project. But even during the current development process, clean code is indispensable. The more complex your project becomes over time, the harder it will be to make changes to bad code or implement new features.
The first steps to better code
Those who research how best to write clean code are likely to quickly come up with style guides, best practices and design patterns. But looking at a few notes on naming variables and functions or how and when to indent code is far from sufficient. Above all, design patterns have to be internalized and require some experience as to when and how best to use them. The time to gain experience can not be relieved by tools. You should definitely take the time to inform you accordingly. Nonetheless, the following tools will help you get clean code without having to write it yourself.
The best tools for your code editor
For various programming languages, as well as code editors like Atom or Visual Studio Code, there are some tools or plugins that analyze and automatically format the code.
Prettier is a code formatter that can customize your entire source code to make it look good in the end, no matter how poorly written. We are talking about indentations, line breaks on too long lines and much more. Prettier is able to clean up existing code with one command. The tool not only helps beginners, but can also take a lot of work from experienced programmers. Because so you can focus on the actual development. If every employee of a project uses the tool with the same settings, defined conventions can be adhered to with ease and fully automatically.
Installs the Eslint and Prettier plugins of your code editor. With Atom or Visual Studio Code this is not a problem. Now you can set Prettier and Eslint to do their analysis each time you save. The Auto Fix function of Eslint can also be executed.
Format your code fully automatically before the git commit
The following two tools allow you to fully automatically parse and format your code every time you want to commit to git. For this you need Lint-Staged and Husky.
Lint-Staged in combination with Husky
If you want to do linting instead of saving before creating a commit, then Lint-Staged could become the tool of choice. This will prevent you from constantly parsing during programming, but will still ensure that your repository only gets clean code. With the Eslint and Prettier based tool, you can also decide exactly which files to analyze. So you avoid the long search of your entire project.
In addition to Lint-Staged, the second tool is very helpful: Husky simplifies the handling of git-hooks. The tool can be used to automatically execute a script or command by simply customizing a configuration file before committing or pushing. You can take advantage of this and start the analysis with Lint-Staged before committing.
The installation takes place in a few steps:
- npm install –save-dev lint-staged husky the following command in a terminal: npm install –save-dev lint-staged husky
- Supplement the following lines in the package.json :
“*.(js|jsx)”: [“eslint –fix”, “git add”]
There are many more useful code optimization tools for other programming languages. They are great tools to follow certain conventions and key guidelines for clean code. If you use the same tool for all participants in a project, you can also make sure that the entire codebase remains clean. Still, the tools are just one step towards a better and cleaner code. Of course you do not pay attention to design patterns or if your source code is performant. Also, especially beginners should not rely too much on such tools, otherwise they do not learn independently, when a tab or line break must be inserted.