PMD rule conflict: the method should have only one exit point, and this should be the last expression in the method

sometimes I find that several PMD rules conflict with each other, so you cannot write code that satisfies all the PMD rules.

For example, it seems that the following two rules are mutually exclusive: "Assigning a null object is the smell of code. Consider refactoring." as well as "The method should have only one exit point, and this should be the last expression in the method"

Below is my sample code:

enter image description here

get1(), , get2(), . , , , PMD, " null - ", - ? :)

+5
3

:

return condition > 5 ? Integer.valueof(123) : null;

. , - , (, - , )... ? , " ", , .

+7

return (condition > 5) ? Integer.valueOf(123) : null;

, " "...:)

+1

Just remove the else for the first method (get1). In Java objects, it is empty by default; this assignment (result = null) is not required.

+1
source

All Articles