You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description:
Using control flow statements such as if, switch or throw-catch as well as ternary conditional operator in tests makes it hard to reason about the correctness of tests. It increases the chance that the test contains the same logical errors as the code under test and it can obscure the fact that some assertions are not executed. Arguably the same is true about loops.
The ifs and ternary operators can be avoided by using concrete examples. The try-catch statements should be replaced by assertThrows or assetDoesNotThrow.
Code Sample:
// bad: not obvious that years divisible by 400 are not covered@TesttestLeapYearBad() {
for (inty = 0; y < 3000; y++) {
if (y % 100 == 0 && y % 400 != 0) { // violationassertFalse(leapYear(y));
} elseif (y % 4 == 0) { // violationassertTrue(leapYear(y));
}
}
}
// good: using concrete numbers@TesttestLeapYearGood() {
assertTrue(leapYear(2000));
assertFalse(leapYear(1000));
assertTrue(leapYear(2004));
assertFalse(leapYear(2005));
}
Possible Properties:
Turn on/off for specific kinds of control flow: allowIf, allowTernary, allowSwitch, allowLoops, allowThrow -- most should be false by default, except for allowLoops.
Hi! I'm interested in implementing this rule, including tests and documentation. I'm currently getting familiar with the codebase. Is this proposal still available to work on, and would you be open to a PR implementing it? Are there any scope decisions we should agree on first?
What's the use case for allowing some, but no all flow control statements?
I tried to apply this rule to PMD's own tests and while I was able to reduce ifs and trys quite a bit, resulting in IMHO more readable code in most cases, but I don't think removing loops would improve things (it's possible to replace them with streams and lambdas.
But maybe the proposed properties are too granular and allowLoops is the only one needed for the first iteration and more can be added if someone finds a use-case?
Activity
MichalKollarcik commentedon Oct 8, 2026
Hi! I'm interested in implementing this rule, including tests and documentation. I'm currently getting familiar with the codebase. Is this proposal still available to work on, and would you be open to a PR implementing it? Are there any scope decisions we should agree on first?
zbynek commentedon Oct 8, 2026
Yes, AFAIK nobody else is working on this. I think this would be better implemented as a Java rule, since XPath rule could get too complex.
Maybe @UncleOwen or @adangel could confirm that the proposed scope outlined in issue description makes sense.
UncleOwen commentedon Oct 8, 2026
What's the use case for allowing some, but no all flow control statements?
zbynek commentedon Oct 8, 2026
I tried to apply this rule to PMD's own tests and while I was able to reduce
ifs andtrys quite a bit, resulting in IMHO more readable code in most cases, but I don't think removing loops would improve things (it's possible to replace them with streams and lambdas.#7173
But maybe the proposed properties are too granular and
allowLoopsis the only one needed for the first iteration and more can be added if someone finds a use-case?UncleOwen commentedon Oct 9, 2026
Yes, only
allowLoopssounds like a good idea to start with.(I'd even argue that loops should be replaces with parameterized tests... but maybe that's not possible in all cases)
[-][java] new rule: ControlFlowInTest[/-][+][java] New rule: ControlFlowInTest[/+]