rain1024 / gVim-Pathogen

My Coding Life
http://rain1024.github.io/gVim-Pathogen
2 stars 0 forks source link

Refactoring #31

Open rain1024 opened 10 years ago

rain1024 commented 10 years ago

The Rule of Three

Fowler, M. and Beck, K. (1999). Refactoring. 1st ed. Reading, MA: Addison-Wesley.

  • The first time you do something you just do it.
  • The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway.
  • The third time you do something similar, you refactor.

    When

  • add funciton
  • need to fix a bug
  • do code review

    When not

  • the existing code is such a mess
  • close to deadline

    Bad Smells

Duplicated Code | Long Method | Large Class | Long Parameter List | Divergent Change | Shotgun | Surgery | Feature Envy | Data Clumps | Primitive Obsession | Switch Statements | Parallel Inheritance | Hierarchies | Lazy Class | Speculative Generality | Temporary Field | Message Chains | Middle Man | Inappropriate Intimacy | Alternative Classes with Different Interfaces | Incomplete Library Class | Data Class | Refused Bequest | Comments

Composite Methods

Moving Features Between Objects

Organizing Data

Simplifying Conditional Expressions

Making Method Calls Simpler

Dealing with Generalization

Big Refactorings

Process

First Step: build a solid set of tests

References

rain1024 commented 10 years ago

Methods

M1. Extract Method

//  Before refactoring
void printOwing(double mount){
   printBanner();
   // print details
   WriteLine("name: " + name);
   WriteLine("amount: " + amount);
}
//  After refactoring
void printOwing(double mount){
   printBanner();
   printDetails();
}

void printDetails(double mount){
   WriteLine("name: " + name);
   WriteLine("amount: " + amount);
}

M2. Inline Method