"Refactoring: Improving the Design of Existing Code" study notes 5 to simplify the function calls

(1) Rename Method function rename

A true master of programming, named levels is essential.

(2) Add Parameter add parameters

(3) Remove Parameter remove parameters

(4) Separate Query From Modifier query and modify the function separation function

That is a function returns an object state, and modify the state of the object; create two different functions, one of which is responsible for querying the other for modification.

Examples: (Example) 

has such a function: when someone intrusion security system, it will tell me the name of the intruder and sends an alert. If an intruder is more than one only send an alert: 

  String foundMiscreant (String [] people) { 

      for ( int I = 0; I <people.length; I ++ ) { 

          IF (people [I] .equals ( "Don" ) ) { 

             SendAlert (); 

             return "Don" ; 

          } 

          IF (people [I] .equals ( "John" )) { 

             SendAlert (); 

             return "John" ; 

          } 

      } 

      return "" ;



  
 foundMiscreant(people);

      someLaterCode(found);

  }

-》

 String foundPerson(String[] people){

       for (int i = 0; i < people.length; i++) {

           if (people[i].equals ("Don")){

              return "Don";

           }

           if (people[i].equals ("John")){

              return "John";

           }

       }

       return "";

   }


  void sendAlert(String[] people){

      if (! foundPerson(people).equals(""))

          sendAlert();

  }

  void checkSecurity(String[] people) {

      foundMiscreant(people);

      String found = foundPerson(people);

      someLaterCode(found);

  }

Complete the inspection and evaluation in the same action. Does this Separate Query from Modifier and contradictory it? Doug Lea and I have discussed this issue, and concluded: the two are not contradictory, but you need to do some extra work. The action query and modify separate action is still of great value. But you need to keep a third function to do both. The "Query - Modify 'function will be called independent inquiry function and modify the function, and was declared when synchronized.

 

Guess you like

Origin www.cnblogs.com/zdcsmart/p/12511096.html