Two cases of "conditional progress programming"

zhaozj2021-02-16  174

What is "conditional progress programming"? Oh, this is my name. The so-called conditional progressive programming is to refer to the need for a series of related function combinations, and the necessaryity of the subsequent function execution is determined by all of the previous functions. For example, there is a series of functions as followmething1, dosomething2, dosomething3 ... dosomethingn; DOSMETHING2 function premise is that the DOSMETHING1 function must perform success, the premise of the DOSMETHING3 function execution is that DOSMETHING1 and DOSMETHING2 are executed, the DOSMETHINGN function is executed It is dosomething1, dosomething2, dosomething3 ... DOSMETHING (N-1) has been successful.

How would you write this program? If you don't think about it, you may write as follows:

Bool Dosomething () {if (dosomething1 ()) {if (dosomething2 ()) {f (dosomething3 ()) {// ... return true;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

This layer is nestled, logically is not wrong, but the readability of the code is a bit. If the number of series functions (ie, the value of the above N is large), the width of the entire code structure will become very long, the readability of the code will be further reduced, and it is easy to make mistakes when modifying the code in the future.

Below, I will give two kinds of program structure that I think is very good. These two program structures are not my homemade, but I can see the light of their wisdom. In addition, I don't want to give too much explanation, readers slowly experience!

Program structure 1:

Bool Dosomething () {bool pass = dosomething1 (); if (pass) {pass = dosomething2 ();} if (pass) {pass = dosomething3 ();} if (pass) {/// ...} return pass; }

Program Structure 2:

Bool Dosomething () {bool pass = false; do {if (! DOSMETHING1 ()) {Break;} if (! Dosomething2 ()) {Break;} if (! Dosomething3 ()) {breaf;} // ...

Pass = true;} while (false); return pass;}

转载请注明原文地址:https://www.9cbs.com/read-8685.html

New Post(0)