Reconstruction - understood in practice

  Martin Fowler bought at the recommendation of the boss <Refactoring improving the design of existing code>, in fact, this book is the author of the reconstruction of the voice of experience, a brief look casual, despite how carefully you look, if you do not practice it felt or applied to it, in fact, it's hard to spend when needed.

  Yesterday on the line when serving, the boss gave me a lesson, first locate a colleague to my query interface a timeout, so the boss and I looked at it with a block of code timeout, see code like this:

B () {// found in the B block BlockA redundant code is, in fact, you can use certain methods deleted, but in order to save me, there is no optimization, and had no idea how to remove the redundant 
 .. // but this is BlockA database query code, it is very time-consuming, taking into account the performance, in respect removed, if non-consuming operations, no problem, but this takes affect performance problems. 
 BlockA // In fact, I started to think of how reconstruction, because I still believe in the latent meaning B should call A, there is this premise, it can prove that it is impossible to remove redundant code BlockA of. 
 ... // see Chapter VI reconstruction, the first is to talk to some of the block it out, create a function, this BlockA have been repeated calls, and so should be out of a function. 
  A (); // I think latent meaning in B necessary to be called A, is because I first written A, has the A as the most basic unit operations, in fact, not, as well as more detailed unit operations, after write B, not the A, B on the look together. 
 ... // haha, feel the chemical reaction mechanism of chemical engineering unit operations in the learning concept used in the programming, as a very 6 ah, this is something 
} 
A () { 
  ... 
  BLOCKa; 
  ... 
}

  Boss optimized code:

B () {// BlockC due to reduced redundant operation in B, about 1/3 of the time performance, from the interface, the calendar is very significant performance increase. 
   ... // But do not say so without a price, of course, understood from the point of view, a small number of function code is usually better to understand than the number of function code and understand A, it can be easily understood B. 
  C (); // I should belong to the large size of the programming, in the case have A, B written in very simple, simple is also an advantage. 
  ...   
} 
A () { 
  ... 
  C (); 
 ...     
} 
C () { 
  ...   
}

  And then tell you the code bad taste, that is, some of the content <Refactoring> Chapter III mentioned, one is related to language, and one is language-independent. I put my time before the code view and conduct a reconstruction of this summary, to say related to language, java

  

A () {// This code does not know the code people looked at doing a little superfluous taste, do not trust ProjectDao, ProjectDao will return null or empty list? 
// return the vast majority of cases are empty list is good, what is this return, you can test it, had a word to get things so much bend Rao, simple things complicated
... List <String> = ProjectDao.getUsers the Users (String the groupId); List <String> = new new usersCopy the ArrayList <String> (); IF (users.isEmpty Users == null &&!) { usersCopy.addAll (Users); } usersAssign.add (usersCopy); ... } B () {// this is the code is repeated, though not entirely repeating (d conditions are not the same), however, can use the list is determined, and then execute the statement to get an
IF (A) { BLOCKa } IF (B) { BLOCKa } IF (C) { BLOCKb } IF (D) { BLOCKb } ... } class c {// this is the English SimpleDageFormat is not thread-safe, use web application concurrency in this natural, very inappropriate. More elegant method is twofold: 1 SimpleFormatDate use of local variables in a method, priate static SDF Final the SimpleDateFormat the SimpleDateFormat new new = (); // 2 LocalDateTime concurrent use DateTimeFormmter and security class, that is, some not used. } IF (A B && &&! C) { } {} // this is shorthand for beginning else is on if the condition is negated, you can see, the situation is not so much what you want, for a, B defaults to true, you can remove the class Controller {// Note to be processed after an exception }

  And is language-independent variable naming, comments no updates. Therefore, software engineering, practice is very important to do a combination of theory and practice more efficiently.

Guess you like

Origin www.cnblogs.com/Robin008/p/11566839.html
Recommended