未加星标

The best tools to write clean code - HelpDev

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

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教程

代码区博客精选文章
分页:12
转载请注明
本文标题:The best tools to write clean code - HelpDev
本站链接:https://www.codesec.net/view/627862.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(25)