Simplify Your Code

When a loop is doing multiple things, split it into multiple loops with each doing just one thing.  If you need to modify one of the loops, this will ensure that you need to understand only the behavior that you need to modify.  This also sets you up for the next refactor — usually Slide Statements or Extract Function.  While this forces iterations of multiple loops, it is important to first Refactor, Then Optimize.  If the loops turn out to be a performance bottleneck (which is rarely the case), it is easy enough to merge them back together.

Refactoring loops to do just one thing will facilitate the Single Responsibility Principle when applying Extract Function.  This will help achieve separation of concerns, promoting a simple, loosely-coupled architecture, while clarifying the intention of your code.

^ Pictured above is a sculpture designed by Gayle Hermick, entitled Wandering the Immeasurable, that is located in front of The Globe of Science & Innovation at CERN.