Split Loop

This is one of the refactorings in Martin Fowler’s book. Often this refactoring opens up for a lot of other useful changes.

Examine

A loop is of itself a code smell, but even more so when it does more than one thing, which implies Diverent Change.

Prepare

Identify the loop you want to split, and in particular which lines within it that hang together and need to be separated from the others. You may need to do some ‘introduce variable’ refactorings and ‘slide statement’ refactorings to gather all the relevant parts of the loop together.

Implement

Clear

Double-check the remaining logic in the original loop makes sense and that you didn’t delete too much by mistake.

Follow up

Often you now want to extract a method for the new loop, or turn it into a pipeline. You might identify another part of the old loop that also needs to be split.

Prompted by Code Smells

Relevant Learning Hours

Sources

Back to All Refactorings