5 ms·
Don't people use @NonNull everywhere now? It's been a few years since I've programmed in Java but even then I feel like that was common practice.
by sanedigital 4y ago
Don't people use @NonNull everywhere now? It's been a few years since I've programmed in Java but even then I feel like that was common practice.
- tyingq 4y agoSeems common, though it only helps so much with 3rd party libraries.
- mrkeen 4y agoYes, it's always been possible to check for nulls at runtime. Personally I use notNull(..) over @NonNull since it actually fires when you expect it to (as opposed to whether your framework dispatcher interceptor trigger decided to invoke it)
- BoorishBears 4y agoDo the heuristics used for Kotlin-interop work with notNull(...)?
- lowbloodsugar 4y agoI think he means using @NonNull for compile time checking, not instrumenting it for runtime checking (though that doesn’t hurt either).
- csunbird 4y agoisn't @NonNull just a syntactic sugar for adding checkNonNull() call as first statement to the function declaration in compile time, or am I mistaken? Just like lombok, it is supposed to generate code that checks null arguments, from what I know.
- mrkeen 4y agoI tried: @Test public void foo() { bar(null); } void bar(@NonNull String param) { System.out.println(param); } All versions of that annotation let the null right through. I tried with: import lombok.NonNull; import javax.validation.constraints.NotNull; import io.micronaut.core.annotation.NonNull; import org.springframework.lang.NonNull; import reactor.util.annotation.NonNull;
- za3faran 4y agoDid you try https://github.com/uber/NullAway https://github.com/uber/NullAway?
- guelo 4y ago@Nullable/@NotNull is great when the IDE shows the warnings, basically dev time checking. There are also tools to integrate it into your builds for compile time checking.
- mrkeen 4y agoI get a little green tick in the top right of my IDE window with the following: @Test public void foo() { final Map<String, Integer> map = new HashMap<>(); map.put("present", 1); bar(map.get("missing")); } void bar(@NotNull Integer param) { System.out.println(param); } "No problems found"
- guelo 4y agoRight, that's because the system libraries don't have the annotations. That's the biggest issue with it. But it still helps a lot if you're religious about it in your own code.
- BoorishBears 4y agoIn my experience mostly people forced to use Java who are then consuming said code in Kotlin do that
- melling 4y ago“Unannotated types are considered not-nullable” Defaulting to not-nullable is a great idea. Much less boilerplate.
- krzyk 4y agoDepends on code base, at the beginning you would prefer the opposite, (not annotated are nullable).
- erik_seaberg 4y agoRather than opting into a slightly incompatible dialect for some but not all code, I would like an IDE that lets me specify what is @Nullable, and quietly inserts @NotNull everywhere else without displaying it. We can keep the boilerplate in the bytecode without rubbing our noses in it.
- joshlemer 4y agoWhich @NonNull are you referring to, Lombok's? Or javax.annotation.NonNull, or something else?