Granted, there are definitely different thinking, in my world, I cannot enforce "Never pass a null" because I am dealing with uncontrollable third parties like API callers, database records, former programmers etc... so I am paranoid and defensive in approaches. Since you are on Java8 or later there is a bit cleaner approach than an if
block.
public String foo(@Nullable String mayBeNothing) {
return Optional.ofNullable(mayBeNothing).orElse("Really Nothing");
}
You can also throw some exception in there by swapping .orElse
to
orElseThrow(() -> new Exception("Dont' send a null"))
.
If you don't want to use @Nullable, which adds nothing functionally, why not just name the parameter with mayBe...
so your intention is clear.