Software Engineering discussion

7 views
Code Complete > Controlling Loops

Comments Showing 1-3 of 3 (3 new)    post a comment »
dateUp arrow    newest »

message 1: by [deleted user] (new)

Here is another chapter with pretty straightforward information and advice. The one that surprised me was the recommendation (and experimental results) for the Loop-With-Exit approach, in which a test is done in the middle of the loop and then a 'break' exits the loop from the middle. It seems very "goto-ish" to me, but it does still have the structured-programming goal of a single exit point. I might agree with this recommendation after thinking about it and trying it out in the future.

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


message 2: by Aleksander (new)

Aleksander Shtuk | 84 comments 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.


message 3: by Erik (new)

Erik | 165 comments 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.


back to top