JavaScript "fail as soon as possible" principle examples of detailed _javascript skills

Source: Internet
Author: User

The first time I heard that there was a "fail as soon as possible" rule in the coding principle, it was strange why the code failed? We should be successful. But in fact, the code should terminate as soon as possible when it encounters an error. In order to detect various states, we need to frequently create if statements and conditional branches, and the result of these conditions detection is either success or failure (True&&false). There are so many conditional detection statements because if they are not implanted in the build process, the browser kernel executes a lot of useless code and consumes a lot of valuable CPU performance and processing time, slowing down the load of the site.

Depending on the location of the block placement of the detected statement that evaluates to false, in some cases the error is quickly aborted, and in others it is terminated after a long period of useless code execution. If we examine the length of the array before traversing it, or if we look at the class attribute we need before we process the DOM, we can abort the execution of the code as soon as the condition is not satisfied. Because traversing the array information and parsing the DOM is time-consuming, it is a good idea to test it before performing these tasks and terminate it as soon as you are satisfied with the execution criteria. It is for this reason that I am advocating the "failure as soon as possible" code.

Here's my sample code:

Create code that terminates as soon as possible when an error is encountered
(function salad (totalslices,peoplecount) {//I want to create a salad function that returns the total salad number required for the party
"use strict";
var fairness = totalslices * PEOPLECOUNT;
return fairness;
}) ();
(function () {
"use strict";
var BODY = document.getElementsByTagName ("body") [0],
//I have set up some variables in this closure function and passed them into the salad function for calculation
Partystarter = " Starlen ",
peoplecount =
Salad = 6,
slicecount = salad * 3;
if (Peoplecount > 0 && Salad >0) {
//first check our party there is no person or salad to avoid the JS consumption of unnecessary computational performance
body.innerhtml = "' <p > ' + partystarter + ', ' + salad (peoplecount,salad) + ' ' </p> '
} else {
body.innerhtml = ' <p> The number of people attending the party or salad bowls is insufficient! </p> "
}
}) ();

Before outputting certain variable information, we first check to make sure that the variable information is stored in memory or whether the variables in some arrays are greater than 0, thus avoiding those that do not need to execute those computational code. This is the basic and important problem of easy performance in the daily development work, so pay attention to it.

The above is a small set to introduce the JavaScript "as soon as possible failure" principle examples of detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.