????9??False——Happy????

????Ruby??Python?????????????Щ?С????????????????????顣????е???????????????????????????????????

??????PHP?У???????????????????CodeIgniter??????????????????????????flase??????????????????????????Щ???????????????get_error()??????

????Exception-happy??????false-happy??????????????????????粻??????S3??????????????????????????????????????????????Exception??????????????????????磺


class CustomException extends Exception {}


???????????????????????????????????

????10??Use Guard Clauses

???????if??????????????????·?????????????飬???if?????true???if???????????????else??????????????????δ???


1.function someFunction($param) {  
2.    if ($param == 'OK') {  
3.       $this->doSomething();  
4.       return true;  
5.    } else {  
6.       return false;  
7.    }  
8.}
 


???????????????????????????????????????????????????????????????????????????????????????????????£?


1.function someFunction($param) {  
2.    if ($param != 'OK') return false;  
3.    $this->doSomething();  
4.    return true;  
5.}
 


????11?????While???м??????

???????for??????????????????飺


1.for (var i = 0; i < x; i++) {  
2.    ...  
3.}
 


?????????for??????????????????????Щ??????????while?????????


1.var i = x;  
2.while (i--) {  
3.    ...  
4.}
 


????12????????????????

??????????????????????????


1.class SomeClass {  
2.   function monsterMethod() {  
3.      if($weArePilots) {  
4.         $this->goAndDressUp();  
5.         $this->washYourTeeth();  
6.         $this->cleanYourWeapon();  
7.         $this->takeYourHelmet();  
8.         if($this->helmetDoesNotFit())  
9.            $this->takeAHat();  
10.         else 
11.            $this->installHelmet();  
12.         $this->chekcYourKnife();  
13.         if($this->myAirplain() == "F22")  
14.            $this->goToArmyAirport();  
15.         else 
16.            $this->goToCivilianAirport();  
17.         $this->aim();  
18.         $this->prepare();  
19.         $this->fire();  
20.      }  
21.   }  
22.}
 


??????????′???


1.class SomeClass {  
2.   function monsterMethod() {  
3.      if($weArePilots) {  
4.         $this->prepareYourself();  
5.         $this->tryHelmet();  
6.         $this->findYourAirport();  
7.         $this->fightEnemy();  
8.      }  
9.   }  
10.   private function prepareYourself() {  
11.      $this->goAndDressUp();  
12.      $this->washYourTeeth();  
13.      $this->cleanYourWeapon();  
14.      $this->chekcYourKnife();  
15.   }  
16.   private function tryHelmet() {  
17.      $this->takeYourHelmet();  
18.      if($this->helmetDoesNotFit())  
19.         $this->takeAHat();  
20.      else 
21.         $this->installHelmet();  
22.   }  
23.   private function findYourAirport() {  
24.      if($this->myAirplain() == "F22")  
25.         $this->goToArmyAirport();  
26.      else 
27.         $this->goToCivilianAirport();  
28.   }  
29.   private function fightEnemy() {  
30.      $this->aim();  
31.      $this->prepare();  
32.      $this->fire();  
33.   }  
34.}
 


??????????δ???????δ???????????????????