Use Built In Loops

Pattern: Use Built-in Loops

...this pattern helps satisfy MakeLoopsApparent.


A complicated loop can lead to disorientation and confusion.

Loops are one of the hardest code fragments to understand, because people can best understand code when they can read that code from top to bottom visually in the text and when the important pieces all fit on one page or screen.

Often, people writing loops think in terms of low-level concepts or anthropomorphic concepts such as "just do it again until I'm done" and are tempted to use low-level programming language constructs to mimic this logic.

But, not everyone shares this private language of iteration, and so it will not always be easy for a code reader sometime later to comprehend the loop. Such a code reader will become disoriented and perhaps will cause an error while modifying that code.

Many programming languages have a variety of loop constructs.

Therefore, Use a programming language loop construct where it makes sense.


Deciding where it makes sense is the hard part. Some people will try to use this pattern at all costs, and the result might be code that is extremely disorienting or confusing.

Here are some simple tests to apply to see whether a loop construct is right for a complicated task - it is usually easy to see whether it works for a simple task.

If one of these tests fails, try the pattern FunctionsForLoops or BuildLoopsYourself.


CategoryLoops


EditText of this page (last edited November 2, 2014) or FindPage with title or text search