Stop Break’ing Your Code

Darshan Bhatt
Zappos Engineering
Published in
3 min readJan 21, 2016

To break or not to break? Is that the question?

TL;DR

Stop using break statements in your loops, opt instead for “termination” variables. These are variables that should be a part of the termination condition of a loop making it clear when the loop should…terminate. This might be obvious to good programmers.

Below is the story of how I came to this conclusion for myself.

My Story

In school I learned the two hallmarks of terrible programmers:

  1. Bad programmers don’t use getters and setters (for everything).
  2. Bad programmers pepper their code with break, continue, and goto statements.

Turns out that getters and setters are evil! But this article isn’t about getters/setters, it is about break statements.

Recently a colleague and I got into an argument over a comment he made, he said “break statements are bad”. I asked him why are break statements bad? He said, “they just are, everyone knows that”. Of course, being a bad programmer myself, I whole heartedly and passionately disagreed with him and gave him a piece of my mind! We proceeded to discuss the finer points of break statements and somehow also Scala (he really likes Scala). So, you are probably wondering, if I disagreed with him, why am I writing a post demonizing break statements? Turns out he was right, even though he couldn’t tell me why…he was right. Damn you SCALA!!!!!

I spent the rest of that day grumbling to myself and really trying to understand what is so bad about break statements. I read up on a bunch of stuff, nothing quite satisfied…here are some links if you are interested…

http://stackoverflow.com/questions/3922599/is-it-a-bad-practice-to-use-break-in-a-for-loop
http://www.misra.org.uk/forum/viewtopic.php?t=298
http://david.tribble.com/text/goto.html

Many programmers believe that having termination variables as part of the termination condition is bad and itself can cause more bugs in your code. Initially, I was with them, but the more I thought about it the more I started leaning towards having those variables. My main rationale for it came from when I was trying to teach someone, who is learning to program, about loops. I asked him to explain the following loop to me.

for(var i=0; i<arr.length; i++) {
if(arr[i].val == searchTerm) {
console.log(“found”);
break;
} else {
console.log(“not found”);
break;
}
}

He said it “iterates over the array and outputs if the searchTerm is found”. Based on the termination condition of this loop you could see why he said that. However, looking at the body, this code snippet doesn’t do much at all. It is equivalent to just checking the first element in the array for the searchTerm. People who have been programming for a while, would notice the issue right away. But it is easy to imagine veteran programmers writing a more complex loop that contains break statements and results in similar issues that would not be so clear.

Here is the above code snippet written with a termination variable. Notice how the code is clearer to read and actually makes debugging easier. The programmer would see a bunch of unwanted “not found” lines output, but still function as intended.

// loop without break statements and clearer termination condition
var searchTermFound = false;
for(var i=0; i<arr.length && !searchTermFound; i++) {
if(arr[i].val == searchTerm) {
searchTermFound = true;
console.log("found");
} else {
searchTermFound = false;
console.log("not found");
}
}

So, based on this overly simplified example, I have changed my mind and now I agree with the wisdom on break statements (even in loops). Moving forward, I’m going to save myself line after line of comments and hour after hour of debugging a complex loop by forcing myself to do away with break statements and opting rather for properly named termination variables. Again, termination variables are variables that go in the termination condition of a loop. The reason that is better is that you can see right from the first line the intent of the loop and when it should terminate. Otherwise, with more complex loops that have break statements, you can make false assumptions based on the loop’s termination condition.

Which camp are you in? Break statements, termination variables, or Scala?

--

--

Darshan Bhatt
Zappos Engineering

Understanding complex problems by applying simple solutions.