7 Considerations for making JavaScript a better _javascript tips

Source: Internet
Author: User
Tags coding standards

As browser performance improves, the volume of JavaScript on the Web page is gradually increasing, along with the steady adoption of the new HTML5 programming interface. However, a poorly written program code has the potential to break the entire site, frustrating users and driving potential customers.
Developers must use all the tools and techniques they use to improve the quality of their code, and be confident that each execution is predictable. It's a topic deep in my heart and I've been working for years find a set of steps to follow in the development process to ensure that only the highest quality code can be published.
Just follow these seven steps to dramatically improve the quality of your JavaScript project. With this workflow, errors will be reduced and many processes optimized to give users a pleasant browsing experience.

01. Code

First, in your function, call ECMAScript5 's strict mode and a "strict mode" declaration, and using the module design mode, in the function closure of the operation, through the sandbox independent code module to minimize the use of global variables, through any external dependencies to maintain the global variable module is clear and concise. Only use established, proven functional libraries and frameworks that have been tested and results good, and keep your functions in a small value, separating any business logic or data from your module operations and other view-layer code.
Larger projects with multiple developers should follow a set of coding principles, such as Google's JavaScript style guide, and require stronger code management rules, including a strict dependency management using asynchronous Module definitions (AMD) using package management via a library such as Requirejs , use bower or jam (client library management tools) for packaging management to refer to specific versions of your dependency files and to adopt structural design patterns, such as observer mode, to facilitate loosely coupled communication between your different code modules. It's also a sensible idea: use a code repository system, such as Git or subversion, to allow your code to be backed up in the cloud with some services such as source code or Magic beans, to provide the ability to revert to a previous version, and to create a branch of code for more advanced projects to perform different functions, Merge them together before you finish.

02. Document

Using structured annotation block formats such as Yuidoc or JSDoc to a file, any developer can understand its purpose without having to study its code, which can reduce misunderstandings. With markdown syntax you can have richer, lengthy comments and explanations. Use the associated command-line tool to automatically generate a Web site for a document: Based on these structural comments, it is consistent with the latest state of any modifications made in your code.

03. Performance Analysis

Run a regular static code analysis tool for your code, such as Jshint or JSLint. 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. Correct any problems with the tool controls to improve your code quality. Try setting the default options for your project team to enforce coding standards, such as where the spaces are indented for each row, where curly braces are placed, and the number of single or double quotes in the entire code file.

04. Test

A unit test is one of the functions performed by a single piece of independent functionality: perform one of the functions from the main code library of your particular input to confirm that it outputs an expected value. To increase your confidence, the code will, as expected, write the frameworks used, such as Jasmine or qunit, for each functional unit test, using expected and unexpected input parameters. But don't forget the edge of the situation!
Running these tests on multiple browsers across multiple operating systems allows you to speed up virtual machine testing on the cloud in demand by leveraging this service, such as browserstack or sauce Labs. These two services provide an API that allows your unit tests to automatically run at the same time in multiple browsers, and once they are done, the results are fed back to you. As a bonus, if your code is stored on GitHub, you can take browserswarm, which is a tool that will automatically run your unit tests when you submit your code.

05. Measuring

Code coverage tools, such as Istanbul measurement when your unit test runs against your function, which lines of code are executed, and it is reported as a percentage of the total number of lines of code. Run a code coverage tool on your unit test and add additional tests that can increase your coverage score to 100% and give you more confidence in your code.
The complexity of a function can be measured with Halsted complex metrics: the equation that computer scientist Morris Holsted set up in the 1970s, the complexity of a function is based on the loop, The branch and the function that it contains are used to quantify: the complexity of the function can be measured using Halstead complexity measures. When this complex fraction is lowered, the easier it is for us to understand and maintain this function, reducing the likelihood of error. command-line tools The visualization of the generated data of Plato's measures and JavaScript code complexity helps to identify the features that can be improved while storing the previous results, allowing you to track quality progress over time.

06. Automation

Using task runner such as grunt from dynamic operation file preparation, analysis, testing, coverage, and complexity report generation process, this saves you time and effort and increases the chances of resolving any quality problems that arise. Most of the tools and test frameworks highlighted in this article are associated with grunt, which can help you improve your workflow and code quality without having to move a finger.

07. Handling Exceptions

Coincidentally, at some point, your code will have an error while it is running. Use the Try ... Catch "statement to properly handle run-time errors and limit the impact of behavior on your site. Use the Network service to log errors that occur at run time. and use this information to add new unit tests to improve your code and eliminate these errors one by one.

Steps to Success

These seven steps help me create some of the most proud code I've had in my career so far. They are also a good foundation for the future. In your own project, commitment to use these steps to produce high-quality JavaScript code, let us work together to improve the network, step-by-step to success.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.