58
Replace Control Flag with Break
Goal
Loops that maintain a boolean to decide when to stop replace it with a direct `break`, `return`, or `continue`.
Before the refactoring
let found = false;
for (const p of people) {
if (!found && p.name === target) {
matched = p;
found = true;
}
}After the refactoringfor (const p of people) {
if (p.name === target) {
matched = p;
break;
}
}Savings
The exit condition appears at the moment it's decided, not as a delayed effect of a flag check; the loop's intent becomes literal.
Note
If the loop body is large, the break can hide the early-exit semantics — extract a function around the loop's body to keep the exit obvious.