代码区项目交易流程

The best tools to write clean code - HelpDev



The best tools to write clean code - HelpDev

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

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.

So far, the tool mainly works with some major programming languages, libraries and web development frameworks. Includingjavascript, HTML, CSS, TypeScript, JSON, YAML and GraphQL. Work is already in progress on additional languages such aphp, Java, Swift or python. Prettier is also available as plugins for Atom, Visual Studio Code, Web Storm and others. On the Prettier website you will find a complete list of all languages and code editors.

Eslint

Eslint is a linting tool for JavaScript and JSX. It is used for static code analysis of the source code. It shows you problems from a certain set of rules in your code, such as an unused variable, a required return expression in a getter function or the suppression of an Else if a return has already occurred in the If part. The ruleset includes rules for avoiding runtime errors, best practices, variable declaration, and more . A few of the rules can even be automatically fixed with a single command. You can freely decide according to which rules the tool should search your code and which not.

Tip:

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 : "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "*.(js|jsx)": ["eslint --fix", "git add"] }

That was all! Now if you git add and then run git commit , your JavaScript and JSX files will be parsed and the code optimized.

Conclusion

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.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

点击收藏

LAST 实现一个Promise(基于Promise/A+规范) wpk: a friendly, intuitive, intelligent, and unofficial CLI for webpack NEXT