Home > Web Front-end > JS Tutorial > body text

7 Dos and Don'ts to Make JavaScript Better_Javascript Tips

WBOY
Release: 2016-05-16 16:17:27
Original
910 people have browsed it

With the improvement of browser performance and the steady adoption of the new HTML5 programming interface, the volume of JavaScript on web pages is gradually increasing. However, a poorly written piece of coding has the potential to break an entire website, frustrate users and drive away potential customers.
Developers must use all the tools and techniques at their disposal to improve the quality of their code and have confidence that execution is predictable every time. This is a topic close to my heart and I have worked for many years to find a set of steps to follow in the development process to ensure that only the highest quality code is released.
Simply following these seven steps will greatly improve the quality of your JavaScript projects. Using this workflow, errors will be reduced and many processes will be optimized, allowing users to have a pleasant browsing experience.

01. Code

First call ECMAScript5’s strict mode and a “strict mode” declaration in your function, and use the module’s design pattern to minimize the global impact of the sandbox independent code module in the self-executing function closure. Use variables within the module, through any external dependencies, to keep global variables clear and concise. Only use established, proven, tested and proven third-party libraries and frameworks, and keep your functions small and separated from your module operations and other view layer code. Any business logic or data.
Larger projects with multiple developers should follow an established set of coding principles, such as Google's JavaScript style guide, and require stronger code management rules, including the use of async via a library such as RequireJS, using package management Module Definition (AMD) for strict dependency management, use Bower or Jam (client library management tools) packaging management to reference specific versions of your dependency files, and adopt structural design patterns such as the Observer pattern for convenience Loosely coupled communication between your different code modules. It's also a smart idea: use a code repository system like Git or Subversion, have your code backed up in the cloud, provide the ability to restore to a previous version, and, through some service like Source Code or Beanstalk, More advanced projects create a branch of code to perform different functions and merge them together before completion.

02.File

Use structured comment block formats such as YUIDoc or JsDoc to document functionality, so any developer can understand its purpose without having to study its code, which reduces misunderstandings. Use Markdown syntax to create richer, longer comments and explanations. Use the associated command line tool to automatically generate a documentation for the site: based on these structural comments, it is consistent with the latest state of any modifications made in your code.

03. Past performance analysis

Regularly run a static code analysis tool such as JSHint or JSLint on your code. These check for known coding flaws and potential errors, such as forgetting to use strict mode or referencing undeclared variables, and missing parentheses or semicolons. Tool controls to correct any issues to improve the quality of your code. Try setting default options for your project team to enforce coding standards, such as indenting each line by spaces, where to place curly braces, and the number of single or double quotes used throughout the code file.

04. Test

A unit test is a small, independent piece of functionality that executes one of the functions from your main code base with specific inputs to confirm that it outputs an expected value. To increase your confidence that the code will behave as expected, write unit tests using a framework such as Jasmine or QUnit for each function, with expected and unexpected input parameters. But don’t forget those edge cases!
Running these tests on multiple browsers across multiple operating systems, by leveraging a service like BrowserStack or Sauce Labs, allows you to accelerate on-demand testing of virtual machines in the cloud. These two services provide an API that allows your unit tests to be automatically run on multiple browsers simultaneously, and the results will be fed back to you once they are completed. As a bonus, if your code is stored on GitHub, you can take advantage of BrowserSwarm, a tool that automatically runs your unit tests when you commit your code.

05. Measurement

Code coverage tools like Istanbul measure which lines of code were executed when your unit tests ran against your functions, reporting it as a percentage of the total lines of code. Running a code coverage tool against your unit tests and adding additional tests can increase your coverage score to 100%, giving you greater confidence in your code.
The complexity of a function can be measured using the Halsted Complexity Metric: an equation developed by computer scientist Maurice Halsted in the 1970s. The complexity of a function is measured in terms of loops, branches, and iterations. Contained function calls to quantify: Function complexity can be measured using Halstead's complexity measure. When this complexity score is reduced, it becomes easier for us to understand and maintain the function, reducing the possibility of error. The command-line tool PLATO measures and generates visualizations of JavaScript code complexity, helping to identify features that can be improved, while storing previous results, allowing quality progress to be tracked over time.

06. Automation

Use a task runner such as Grunt to automate the process of documentation, analysis, testing, coverage and complexity report generation, saving you time and effort and increasing your chances of resolving any quality issues that arise. Most of the tools and testing frameworks highlighted in this article are associated with Grunt, which can help you improve your workflow and code quality without having to lift a finger.

07. Handling exceptions

Invariably, at some point, your code will throw an error when it runs. Use "try...catch" statements to handle runtime errors appropriately and limit the impact of behavior on your site. Use a network service to log runtime errors. And use this information to add new unit tests to improve your code and eliminate these bugs one by one.

Steps to Success

These seven steps helped me create some of the code I’m most proud of in my career so far. They are also a great foundation for the future. Commit to using these steps to produce high-quality JavaScript code in your own projects, and let's work together to improve the web, one step at a time on your way to success.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!