-
Notifications
You must be signed in to change notification settings - Fork 93
Closed
Labels
Description
What problem are you trying to solve?
Simplify code and improve readability.
There are cases where while (true) {} loops make sense. But not all of them do.
In cases of while loop body starts with an if statement which decides to break the loop, the logic can be moved to the while loop condition.
What precondition(s) should be checked before applying this recipe?
Probably to start with:
- the
ifis the first statement in thewhileloop - the
ifbody has onlybreakand nothing else
I am not sure whether this recipe should kick in if there are multiple break statements in the loop. Probably not. EDIT: why not?
Describe the situation before applying the recipe
int counter = 0;
while (true) {
if (counter >= 5) {
break;
}
System.out.println("Counter: " + counter);
counter++;
}Describe the situation after applying the recipe
int counter = 0;
while (!(counter >= 5)) {
System.out.println("Counter: " + counter);
counter++;
}Possibly the negation in the while condition could be simplified by delegating to other recipe.
OSS repro
- https://github.com/AutoMQ/automq/blob/6143ec682c45edc6660e08833a5dd8239a29da98/generator/src/main/java/org/apache/kafka/message/SchemaGenerator.java#L133-L136
- https://github.com/ismartcoding/plain-app/blob/ad7cbf6bafd47596351cb3c9fc67437d047444f6/lib/src/main/java/com/ismartcoding/lib/androidsvg/utils/CSSFontFeatureSettings.java#L251-L253
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done