Software Engineering discussion
Code Complete
>
Controlling Loops
date
newest »
newest »
I, personally, don’t like using WHILE loop at all unless it’s necessary for infinite loops. To me almost any kind of FOR loops is better. The reason for that is simple – to me WHILE loop is a good candidate for hanging software. And if that happens, sometimes finding a problem gets very time consuming especially in software with dozens of modules and libraries. Or may be I need to improve my debugging skills. Please let me know if you know a good strategy or a tool. Until then my suggestion to myself – try to avoid WHILE loops unless it cannot be substituted with other kind of loop. To fix a problem in “Limit the scope of loop-index variables to the loop itself” I usually put an extra pair of curly braces around FOR loop. It allows me to keep a standard FOR loop construction. Otherwise, I’d rename controlling variables in second loop.
I, also, really like Foreach loops when I'm writing C#.I tend to not use unbounded for loops like the FOREVER macro. My while loops tend to check for things without known boundries like "while not user abort" or "while prime number not factored".
I was very suprised to see the recomendation for "safety checks" in loops. I had thought I made them up about 5 years ago and have been using them since. I also thought they are terrible (except they seem to be practicle). My code often has saftey checks in loop areas that parse an infinite stream of data for signature bytes.


I think that 'foreach' is a terrific language addition. And, once again, I think a comment about functional languages and the natural handling of loops without these control structures would have provided a nice ending to this chapter (although the APL example hints at this).