Software Engineering discussion
Code Complete
>
Controlling Loops
date
newest »


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 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).