loading...
Cover image for Code Complete 2: Loops (Part 4)

Code Complete 2: Loops (Part 4)

iyvonnebett profile image Iyvonne Bett Updated on ・2 min read

Loop is an informal term that refers to any kind of iterative control structure—any structure that causes a program to repeatedly execute a block of code.

Common loop types are for, while, and do-while in C++ and Java, and For-Next, While-Wend, and Do-Loop-While in Microsoft Visual Basic. Go has only one looping construct, the for loop.

Alt Text

Chapter 16: Controlling Loops

Selecting the Kind of Loop

  • If you don't know ahead of time exactly how many times you’ll want the loop to iterate, use a while loop.
  • Don't code a "loop and a half"; instead, loop forever and break in the middle.
  • Keep for loops simple; if you're explicitly changing the index value, consider a while loop. **Controlling the Loop**
    • Put initialization code immediately before the loop.
    • Use for (;;) or while (true) to write an infinite loop; don't fake it by iterating to a large number.
    • Reserve the for loop header for initializing the loop, terminating it, and moving toward termination.
    • Keep statements that control the loop, or move it toward termination, near its beginning or end.
    • Don't change the index of a for loop to make it terminate. Avoid using the loop index value after the loop; instead, assign a final value to a variable at the appropriate point inside the loop.
    • Using a break forces the person reading your code to look inside the loop for an understanding of the loop control.
    • Inefficient programmers experiment randomly until they find something that works, perhaps replacing a bug with a more subtle one.

    Chapter 17: Unusual Control Structures

    Recursion

    For most situations, recursion produces very complicated solutions that chew up stack space. Use it selectively, and prefer iteration.
    For local variables in recursive functions, use new to create objects on the heap as opposed to on the stack.

    Goto
    The use of a goto statement defeats some compiler optimizations, which rely on orderly flow control.
    The try-finally construct can sometimes be used to perform the error cleanup that a goto sometimes performs.
    Measure the performance of any goto statement used to improve efficiency.

    Key points to note;

    • Loops are complicated. Keeping them simple helps readers of your code.
    • Techniques for keeping loops simple include avoiding exotic kinds of loops, minimizing nesting, making entries and exits clear, and keeping housekeeping code in one place.
    • Loop indexes are subjected to a great deal of abuse. Name them clearly, and use them for only one purpose.
    • Think through the loop carefully to verify that it operates normally under each case and terminates under all possible conditions.
    • Multiple returns can enhance a routine’s readability and maintainability, and they help prevent deeply nested logic. They should, nevertheless, be used carefully.
    • Recursion provides elegant solutions to a small set of problems. Use it carefully, too.
    • In a few cases, gotos are the best way to write code that’s readable and maintainable.
    • Such cases are rare. Use gotos only as a last resort.

Discussion

pic
Editor guide