XPlorations

XP123XPlorations → Refactoring Demo Screencast (May, 2006)

Refactoring Demo Screencast: Extract Method May, 2006

Four ways to Extract Method.

Refactoring is "improving the design of existing code". We want to do this quickly and safely, so that we're improving the code's design but not introducing bugs while we change it.

This series of videos shows four different ways of performing the refactoring "Extract Method":

  1. Sloppy (Flash, 0.9M)
  2. Manual, but "by the book" (Flash, 1.1M)
  3. Automated, using the IDE's refactoring support (Flash, 0.9M)
  4. Automated, at full speed (Flash, 0.5M)

The code we're improving is part of a program that lets a user unscramble sentence fragments. The part in bold below shows the code we're going to extract.

public class Board extends JPanel implements PropertyChangeListener {    
    // ...
    
    public String toString() {
        Component[] cards = this.getComponents();
        List sortedResult = Arrays.asList(cards);
        Collections.sort(sortedResult, new BoardComparator()); 
        
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < sortedResult.size(); i++)
            result.append(sortedResult.get(i).toString());
        
        return result.toString();            
    }
}

[Recorded May, 2006. Tools: Eclipse, Macromedia Captivate, Audacity.]

Copyright 1994-2010, William C. Wake - William.Wake@acm.org