for quality
X 2+3x+1==(x+1)(x+2)

 Improved code structure
 Same executable behavior
 Easy write, change
 Deferred complexity

 Quality

 Velocity!
 Understandable

 Predictable

 Changes   Localized
 We  dig design patterns
 We    TDD
 Incremental
 Ensure working version
 Create/Ensure tests

 Use version control
 +:Variable, Method
 ++: Class, Class Interaction
 Var rename; method
  rename; method extract
 Interface/class extract
 Safeand easy
 Limited to simple ref’
 Distinguish             Good Design
To the point   Readable       Simple

Predictable/   Modifiable     Testable
Uncoupled
 Where             to refactor first?
Point         Readable    Simple   Predictable Modify
                                   /uncoupled
Dup code      Renaming    Switch   Refused    Parallel
Large class   Long method          bequest    hierarchy
Lazy class    Large class          Feature
Speculative                        Envy
generality                         Intimacy
 Sharing

 Consuming
 Takean example of smell
 and a change
 Demo

 Identifysmells
 Propose moves
 Demo

 Identify   the qualities
 Discussion
 Minor: As you go
 Major: At mini-
  feature/class completion
 Not as a separate milestone
 Quality

 Adaptability

 Velocity   Impact?
 Refactoring projects
 Code and fix

 Performance

 Procastination
 Your ref’experience
 Favorite ref’ moves
 Hopefully,   answers
 ???
 Yaniv@YanivPessach.com
 Before and after refactoring
 Refactoring tools menus
int ProcessX(int x) { return x+1; }
Ren method



             int IncrementPosition(int x) {
               return x+1; }

             int IncrementPosition(int
  Ren var




               oldPos) { return oldPos+1; }
float Distance(float x1, float y1, float x2,
                         float y2) { return Math.Sqrt((x1 - x2) *
                         (x1 - x2) + (y1 - y2) * (y1 - y2));}
Param list to class




                       class FloatPoint { public float x; public
                          float y;}
                      float Distance(FloatPoint p1, FloatPoint
                      p2) { return Math.Sqrt((p1.x - p2.x) *
                      (p1.x - p2.x) + (p1.y - p2.y) * (p1.y -
                      p2.y)); }
float Distance(float x1, float y1, float x2,
                            float y2) { return Math.Sqrt((x1 - x2) *
Simplify subexpression


                            (x1 - x2) + (y1 - y2) * (y1 - y2));}
                          class FloatPoint { public float x; public
                             float y;} ….
                         float Distance(FloatPoint p1, FloatPoint
                         p2) { return Math.Sqrt(p1.DeltaX(p2) *
                         p1.DeltaX(p2) + Math.Sqrt(p1.Deltay(p2)
                         * p1.DeltaY(p2)); }
class ShapePainter {
      public void Paint(int scale); }
class AreaRenderer {
      public void Render(int multiplier); }
class DrawController {
void DrawObject(object o) {
if (o is ShapePainter)
   ((ShapePainter)(o)).Paint(1); } }
Extract interface
                         interface IShapePainter {
                            void Paint(int scale); }
                         class ShapePainter : IShapePainter {
                              public void Paint(int scale); }

                         class AreaRenderer : IShapePainter{
Use iface




                              public void Paint(int multiplier); }
ren
Use iface




                         void DrawObject(IShapePainter o) {
                         o.Paint(1); }
class ShapePainter {
                          public void Paint(int scale); }
                    class AreaRenderer {
Extract interface




                          public void Render(int multiplier); }
                    class DrawController {
                    void DrawObject(object o) {
                    if (o is ShapePainter)
                       ((ShapePainter)(o)).Paint(1); } }
class TaxCalculator { float rate;
public float GetTax(float income,
   Deductions deductions) { return
   (income - deductions) * rate; } }
class PayRoll { TaxCalculator tax;
Deductions deduct; float income;
float IssueCheck() { if (tax != null)
 return tax.GetTax(income, deduct);

 Else return income; } }
class NullTaxCalculator : TaxCalculator{
              public float GetTax(float income,
                 Deductions deductions) { return 0; } }
              class PayRoll2 { TaxCalculator tax;
Null object




                 Deductions deduct; float income;
              float IssueCheck() { return
                 tax.GetTax(income, deduct); } }
Refactoring
Refactoring
Refactoring
Refactoring

Refactoring

  • 1.
  • 2.
    X 2+3x+1==(x+1)(x+2)  Improvedcode structure  Same executable behavior
  • 3.
     Easy write,change  Deferred complexity  Quality  Velocity!
  • 4.
  • 5.
     We dig design patterns  We TDD  Incremental
  • 6.
     Ensure workingversion  Create/Ensure tests  Use version control
  • 7.
     +:Variable, Method ++: Class, Class Interaction
  • 8.
     Var rename;method rename; method extract  Interface/class extract
  • 9.
     Safeand easy Limited to simple ref’
  • 13.
     Distinguish Good Design To the point Readable Simple Predictable/ Modifiable Testable Uncoupled
  • 14.
     Where to refactor first? Point Readable Simple Predictable Modify /uncoupled Dup code Renaming Switch Refused Parallel Large class Long method bequest hierarchy Lazy class Large class Feature Speculative Envy generality Intimacy
  • 15.
  • 16.
     Takean exampleof smell and a change
  • 17.
  • 18.
     Demo  Identify the qualities
  • 19.
  • 20.
     Minor: Asyou go  Major: At mini- feature/class completion  Not as a separate milestone
  • 21.
  • 22.
     Refactoring projects Code and fix  Performance  Procastination
  • 23.
     Your ref’experience Favorite ref’ moves
  • 24.
  • 25.
  • 26.
  • 27.
     Before andafter refactoring  Refactoring tools menus
  • 28.
    int ProcessX(int x){ return x+1; } Ren method int IncrementPosition(int x) { return x+1; } int IncrementPosition(int Ren var oldPos) { return oldPos+1; }
  • 30.
    float Distance(float x1,float y1, float x2, float y2) { return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));} Param list to class class FloatPoint { public float x; public float y;} float Distance(FloatPoint p1, FloatPoint p2) { return Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); }
  • 31.
    float Distance(float x1,float y1, float x2, float y2) { return Math.Sqrt((x1 - x2) * Simplify subexpression (x1 - x2) + (y1 - y2) * (y1 - y2));} class FloatPoint { public float x; public float y;} …. float Distance(FloatPoint p1, FloatPoint p2) { return Math.Sqrt(p1.DeltaX(p2) * p1.DeltaX(p2) + Math.Sqrt(p1.Deltay(p2) * p1.DeltaY(p2)); }
  • 33.
    class ShapePainter { public void Paint(int scale); } class AreaRenderer { public void Render(int multiplier); } class DrawController { void DrawObject(object o) { if (o is ShapePainter) ((ShapePainter)(o)).Paint(1); } }
  • 34.
    Extract interface interface IShapePainter { void Paint(int scale); } class ShapePainter : IShapePainter { public void Paint(int scale); } class AreaRenderer : IShapePainter{ Use iface public void Paint(int multiplier); } ren Use iface void DrawObject(IShapePainter o) { o.Paint(1); }
  • 35.
    class ShapePainter { public void Paint(int scale); } class AreaRenderer { Extract interface public void Render(int multiplier); } class DrawController { void DrawObject(object o) { if (o is ShapePainter) ((ShapePainter)(o)).Paint(1); } }
  • 37.
    class TaxCalculator {float rate; public float GetTax(float income, Deductions deductions) { return (income - deductions) * rate; } } class PayRoll { TaxCalculator tax; Deductions deduct; float income; float IssueCheck() { if (tax != null)  return tax.GetTax(income, deduct);  Else return income; } }
  • 38.
    class NullTaxCalculator :TaxCalculator{ public float GetTax(float income, Deductions deductions) { return 0; } } class PayRoll2 { TaxCalculator tax; Null object Deductions deduct; float income; float IssueCheck() { return tax.GetTax(income, deduct); } }