I have following code. I am getting "Avoid using Literals in Conditional Statements." warning in PMD on line number 5.
List<Object> listObj = getData();
if (listObj.isEmpty()) {
throw new NoEntity("No entity found for given Device.");
}
if (listObj.size() > 1) {
throw new MultiEntity(
"Multiple entity record found for given Device.");
}
I prefer not to put global static final int variable having value as 1 and use it in if condition. Any other solution for this ?
You can insert an assignment before the conditional, with a nice variable name that will serve as documentation. You'll improve the readability and make the warning go away.
boolean multiEntityDevice = listObj.size() > 1;
if (multiEntityDevice) {
throw new MultiEntity(
"Multiple entity record found for given Device.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With