3 ms·
Without a proper context, this is fairly strange, and possibly even bad advice. For loops and if statements are both control flow operations, so some of the ar
by smokel 3y ago
Without a proper context, this is fairly strange, and possibly even bad advice.
For loops and if statements are both control flow operations, so some of the arguments in the article make little sense. The strongest argument seems to be about performance, but that should typically be one of the latest concerns, especially for rule-of-thumb advice.
Unfortunately, the author has managed to create a catchphrase out of it. Let's hope that doesn't catch on.
- actionfromafar 3y agotry let’s hope catch not on
- koonsolo 3y ago> Unfortunately, the author has managed to create a catchphrase out of it. Let's hope that doesn't catch on. In you next pull request: "Hey can you push this if up?" :D.
- aktenlage 3y ago> The strongest argument seems to be about performance It may be an argument, but it's not a strong one. If the improved code can be written like the author puts it in their example (see below), the condition is constant over the runtime of the loop. So unless you evaluate an expensive condition every time, you are good. Branch prediction will have your back. If condition is just a boolean expression using const values, I'd even guess the compiler will figure it out. if condition { for walrus in walruses { walrus.frobnicate() } } else { for walrus in walruses { walrus.transmogrify() } } Branch prediction should have you covered here. If you can easily rewrite it in