ProtossShuttle :
I used to use lots of @NotNull/@Nullable
annotations to enable IDE to help me find out potential NPE
at compile time. However, my new team doesn't allow any use of @NotNull/@Nullable
annotations, and nor are custom annotations like those allowed. As a result, I become much more likely to write bugs caused by NPE
than before.
I have tried several solutions:
- Use
Optional<T>
in java 8. However, this is not doing well for every case. It's usually not recommended to useOptional<T>
as the type of fields or arguments. It's also very frustrating thatOptional<T>
instance itself could benull
. Also, it's difficult to operate control flows inside lambda expressions when callingifPresent(obj->...)
(it's easier in Java 9). And using too manyOptional<T>
s makes the code a little verbose.(UPDATE: Unfortunately,Optional<T>
is also banned from using now) - Make IDE treat every unannotated instance as
@Nullable
. This solution does help me find out some potential bugs, however, IDE would suggest me to check almost every method invocation, which is really annoying, since many methods are designed intentionally not returningnull
. - Check every method invocations. This is a viable solution, however it has a severe influence that the possibility of being
null
would be passed everywhere through method invocations. Under such circumstances, every argument of a method is possible to benull
, and when an argument is checked for nullity, the method would usually return anull
continuously. Finally, every method is "infected" with the possibility of receivingnull
arguments and returningnull
. - Call
Objects.requireNonNull()
to prevent the problem aforementioned. It slightly reduces the pain of checkingnull
everywhere. However, it provides no guarantee that the caller won't pass anull
to the cases whennull
is not allowed. And oncenull
is passed, the runtimeNPE
thrown is much more likely to ruin your application. - Switch to kotlin. Of course, it's not allowed:)
Is there other suggestions about detecting NPE at compile time (and save my job)? I think a solution to this problem could be widely used, not only helping myself, since not all teams allow usage of @NotNull/@Nullable
annotations and Optional<T>
s.
Algiz :
Not a definitive answer but a few way to explore:
- Tools like findBug, PMD , Coverity ... are quite good at this exercice.
- Check the reason why you cannot use the annotation in your team. Maybe you can make your team change its mind? (There might be a good reason too.)
- Eclipse IDE is reasonnably good at this exercice. Did you try the options in 'Windows > Preferences > Java > Compiler > Errors/Warnings > Null analysis'?
- Unit testing. Cover the 'should-not-happen' situation in a few test cases. Code coverage tools (like JaCoCo) might be helpfull as wel.
- Defensive programming (plain 'if' statement, assertions ...)
- A mix of the previous items
Hope this helps.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=430216&siteId=1