Reputation: 2917
I have the following code:
Pattern p1 = Pattern.compile("foo1");
Pattern p2 = Pattern.compile("foo2");
Pattern p3 = Pattern.compile("foo3");
Pattern p4 = Pattern.compile("foo4");
Pattern p5 = Pattern.compile("foo5");
if (p1.matcher(kvTarif.getkey()).matches() || p2.matcher(getkey()).matches() ||
p3.matcher(getkey()).matches() || p4.matcher(getkey()).matches() ||
p5.matcher(getkey()).matches())
checkstyle says that the boolean complexity is 4 (max allowed is 3).
How can I reduce the complexity?
Upvotes: 1
Views: 1552
Reputation: 521339
You can reduce the number of patterns to 2, according to the logic with which you are matching:
Pattern p1 = Pattern.compile("foo1");
Pattern p2 = Pattern.compile("foo2|foo3|foo4|foo5"); // match foo2 through foo5
if (p1.matcher(kvTarif.getkey()).matches() || p2.matcher(getkey()).matches()) {
// handle this case
}
As user @JonnyHenly mentioned, you could simplify the second pattern even more by using this:
Pattern p2 = Pattern.compile("foo[2-5]");
Upvotes: 5