Rob's co-worker needed to write a loop that iterated across every element in an array. This very common problem, and you'd imagine that a developer would use one of the many common solutions to this problem. The language, in this case, is JavaScript, which has many possible options for iterating across an array.
Perhaps that buffet of possible options was too daunting. Perhaps the developer thought to themselves, "a for each loop is easy mode, I'm a 10x programmer, and I want a 10x solution!" Or perhaps they just didn't know what the hell they were doing.
Regardless of why, this is the result:
try {
var index = 0;
while (true) {
var nextItem = someArray[index];
doSomethingWithItem(nextItem);
index++;
}
} catch (e) { }
This code iterates across the array in an infinite while loop, passing each item to doSomethingWithItem
. Eventually, they hit the end of the array, and someArray[index]
starts returning undefined. Somewhere, deep in doSomethingWithItem
, that causes an exception to be thrown.
That is how we break out of the loop- eventually something chokes on an undefined value, which lets us know there's nothing left in the array.
Which puts us in an interesting position- if anyone decided to add better error handling to doSomethingWithItem
, the entire application could break, and it wouldn't be obvious why. This is a peak example of "every change breaks somebody's workflow", but specifically because that workflow is stupid.