Stop breaking for/while loop!

I've so often seen break (or continue) improperly used to manage the end condition of for (or while) loops that I came to see it as an immediate hint toward a poorly designed code. I'm not saying there are absolutely no case where it's the correct way to do, but I've seen so many counter examples and I personnally had so few occasions where there was no better way that "breaking loop is bad" became one of my rules of thumb. In this article I want to show a few examples I've encountered at work to illustrate why I came to think that way. Examples below are only edited for shortening and converted to pseudo-code when the language is irrelevant. Except for that they are as I've found them.

2022-04-23
in All, Pub talk,
124 views
Copyright 2021-2024 Baillehache Pascal