Roll Up Loop

This refactoring doesn’t seem to have an official description anywhere, but it’s one I often use. The sign it could be useful is a long method with lots of if statements. Each if statement essentially returns a value of the same type. A loop is really just a sequence of if statements, so often you can make the code cleaner and easier to read by rolling up the if statements into a loop. The trick is to identify an abstraction to loop over.

Session Outline

Connect: Code smells and refactorings

Ask the group to name some refactorings that they have used, and the corresponding code smells.

Concept: Roll Up loop

Explain what this is. Demonstrate a really simple case, like one of the examples in RollUpLoop.

Demo: show them the code duplication & go through strategy

There are two exercises I use for this - TimerExpiry and ClarifyException. Both work equally well.

Show them the duplication in each if statement. Explain the goal - for TimerExpiry this a list of function pointers you loop over. Each function returns the time until the timer goes off. Then just find the smallest value in one place rather than 6 places. For ClarifyException you can have a small class rather than a function pointer - with two methods “matches” and “build”.

Concrete: refactor away the duplication

Have them work on the code to achieve the refactoring goal - roll up loop.

Conclusions: note down learnings

Describe this refactoring in your own words. Include