Refactoring Grid Game (http://xp123.com/g4p/0212a/index.shtml)

 

Start with a ‘grid’ of 6 ‘windows’, each window having 4 ‘panes’ (the panes are numbered as shown):

3

2

4

1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Each pane can contain ‘X’ and ‘O’ tokens (any number – order does not matter)

 

Define the following ‘smells’ (The goal of the game will be to remove smells)

  1. Basement – there is a token in one of the panes in the one of the lower 3 windows
  2. Duplication – two tokens appear in any pane
  3. Conflict – the grid contains two types of tokens (an X and an O)
  4. Sou’Wester – any window has a token in the lower left pane and a token in one or both of the upper panes

 

The following grid illustrates each of these smells:

 

 

 

XX

 

XO

 

 

 

O

O

 

 

 

 

 

 

 

O

 

 

 

 

 

 

The following grid is ‘smell-free’:

 

 

O

O

 

O

 

O

O

O

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Refactoring means to move tokens between panes according to refactoring rules. We will apply a sequence of refactoring rules to remove as many smells from a grid as is possible.

 

Refactoring rules:

 

Vertical Refactor: Vnt (n=1, 2, 3, or 4 and t=X or O): Any token (t) may be moved from pane n in one window to pane n in the other window in the same column (i.e. directly above or below in the grid).

 

The following grid…

 

 

 

 

 

 

O

 

 

 

 

 

 

 

XX

 

 

 

 

 

 

 

 

XO

 

Can be refactored to this grid…

 

 

X

 

 

 

 

 

 

 

 

X

 

 

X

 

 

 

O

 

 

 

 

O

 

by applying these rules in sequence: V4X, V4O, V1X

 

Note – the order of applying these refactorings does not affect the final result. A ‘refactoring group’ named V might be defined to represent any contiguous sequence of vertical refactorings.

 

Exercise 1: Describe a unit test that could be applied to the before and after grids to reassure that a group of vertical refactorings was done properly. (Hint – something is true about the grids before and after each V rule is applied).

 

Exercise 2: Is your unit test ‘foolproof’? That is, if the grids pass your test, are you certain that the refactoring was valid? Explain, possibly with an example.

 

Pane Refactor: PnXO (n=1,2,3, or 4): In an X and an O token appear together in pane n, they may be removed from that pane OR (the reverse) an XO pair may be inserted into pane n.

 

Exercise 3: Remove as many smells from the following grid as possible using the two types of refactorings introduced so far. Draw each intermediate grid and name the refactoring rule used. What smells can be removed. Which cannot?

 

 

 

XX

O

 

 

 

 

 

OO

 

 

 

O

O

X

 

X

 

 

 

XXX

 

 

Rotation Refactor: Rntt (n=1,2 or 3 and t=X or O): Within a single window, two t tokens in pane n may be removed if a single t token is placed in pane n+1 OR (the reverse) a single t token found in pane n+1 may be removed if two t tokens are placed together in pane n.

 

Horizontal Refactor: H24t (t=X or O): Within a single window, if panes 2 and 4 each contain a t token, they may be removed if a t token is placed into pane 1 OR (the reverse) a single t token in pane 1 may be removed if t tokens are placed in pane 2 and pane 4.

 

These rules are illustrated in the following grid refactoring:

 

X

X

 

 

 

 

 

 

 

O

 

 

 

 

 

 

 

 

O

 

O

 

XXX

By applying these refactoring rules in this order: H24X, H24O, R3OO, R1XX, P2XO

 

 

 

X

 

 

 

 

OO

 

 

 

 

 

 

 

 

O

O

 

 

 

 

X

 

Exercise 4: Remove all smells (if possible) from each grid. Draw each intermediate grid showing the refactoring rule applied at each step (Feel free to use the V-group refactoring rule).

4-a:

 

X

 

X

 

X

 

X

 

X

 

X

 

X

 

 

X

X

 

 

X

X

 

X

 

4-b:

 

X

 

X

 

 

 

 

 

 

 

X

 

 

 

O

O

O

 

 

O

O

 

O

 

4-c:

X

X

 

X

OXO

O

XXX

 

 

 

X

X

 

X

O

O

O

XX

X

 

O

XO

O

O

 

Exercise 5: Describe your strategy for removing smells from a grid

 

Exercise 6: Suggest new names for the refactoring rules that are more closely related to the smells in this system. Explain your reason for each name.

 

Exercise 7: The H24t rule can be used to eliminate some types of Sou’Wester smells from a window, but not all. Draw a window with such a smell that cannot be eliminated by only applying H24t refactorings. Devise a strategy (a sequence of refactorings – you may include if statements) for eliminating Sou’Wester smells from any window. The resulting grid must not have any new smells in any other window. Does your strategy add new smells to the same window? Is this good or bad in the overall goal of removing all smells from the grid? Explain.

 

Exercise 8: The following refactoring rule has been proposed to be added to the system: Diagonal Refactor: D24t (t=X or O): Within a window, a token t in pane 4 may be moved to pane 2 OR (the reverse) a token t in pane 2 may be moved to pane 4. Is this a “new” refactoring rule, or can it be seen as a ‘shortcut’ for a sequence of already defined transformations? Explain.