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.
Ask the group to name some refactorings that they have used, and the corresponding code smells.
Explain what this is. Demonstrate a really simple case, like one of the examples in RollUpLoop.
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”.
Have them work on the code to achieve the refactoring goal - roll up loop.
Describe this refactoring in your own words. Include