JavaScript "failure as soon as possible" principle

Source: Internet
Author: User

The first time I heard about the "failure as soon as possible" rule in coding , I found it strange why the code failed? It's the right to be successful. But in fact, when the code encounters an error, it should be terminated as soon as possible. In order to detect various states, we need to create if statements and conditional branches frequently, and the result of these conditions is either success or failure (true&&false). There are so many conditional detection statements, because if you do not implant these monitoring points (checkpoint) during 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 site loading.

Depending on the location of the detection block placement where the result is false, there are cases where an error occurs quickly, and in other cases it is terminated after a long period of useless code has been executed. If we detect the length of the array before traversing it, or if we are working with the DOM to see if we have the class attribute we need, we can abort the execution of the code immediately if the condition is not met. Because traversing the array information and parsing the Dom is a time-consuming task, it is best to detect it before performing these tasks and terminate it as soon as it is satisfied with the execution condition. It is for these reasons that I would like to advocate the "failure as soon as possible " code.

Here is my sample code:

1 //to create code that terminates as soon as an error is encountered2(functionSalad (totalslices,peoplecount) {//I'm going to create a salad function to return the total number of salads needed for the party.3"Use Strict";4 5     varFairness = totalslices *Peoplecount;6 7     returnfairness;8 })();9(function () {Ten"Use Strict"; One  A     varBODY = document.getElementsByTagName ("body") [0], -         //I created some variables in this closure function and passed them to the salad function for calculation -          thePartystarter = "Starlen", -Peoplecount = 18, -Salad = 6, -Slicecount = Salad * 3; +  -     if(Peoplecount > 0 && Salad >0){ +         //Check our party first. There are no people or salads to avoid JS consuming unnecessary computational performance Abody.innerhtml + = "' <p> '" + Partystarter + "," + salad (Peoplecount,salad) + "' </p> '" at}Else { -body.innerhtml + = "<p> The number of people attending the party or salad bowls are insufficient! </p> " -     } -  -})();

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

JavaScript "failure as soon as possible" principle

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.