I was trying to fix a bug a few days ago and my initial fix involved nested loops. The code pulled some random values out of a list and added them to another list (creating wrong answers for a multiple choice question). I of course forgot to check for duplicate values, introducing a bug. The humbling thing about software development is that I still routinely make mistakes like this after ten years of getting paid to write software.
Anyway, I went to fix the bug, and so I just put another loop inside the loop that added the answers to the list which made sure that the value I picked wasn’t already assigned to an answer I had added. If the value was already used, I just did this:
continue;
Guess whose fix didn’t work? This statement was applied to the inner loop that checked for duplicates rather than the outer loop that added answers to the list and the duplicate answer kept getting added as before. The next logical step was to use a label, so I could label the outer loop with something helpful like outer and then change the statement above to:
continue outer;
This would work, but I don’t think it would add much in the way of clarity. Instead I added a method called hasAnswerWithValue() to my Question class and replaced the inner loop with:
if (question.hasAnswerWithValue(newAnswer)) { continue; }
I may even use the new method somewhere else at some point. In some cases, there’s no way to avoid the use of nested loops, but I’ve come to think that you should always do so whenever it’s practical.
In fact, I’m coming to think that “minimize the number of loops and conditional statements” is a good principle for programmers. That’s another post though, because that’s not what I did in this case. I just hid the loop in a method instead of nesting it.