Code Smells: Null

:star: :star: :star:


  • Scattered null-checks: sad!
  • Things null can mean:
    • Value was never initialised (whether accidentally or on purpose)
    • Value is not valid
    • Value is not needed
    • No such value exists
    • Something went terribly wrong and something that should be there is not
    • Probably dozens of other things. Some avoidable if you’re disciplined/use linter
  • Optional solved the case of: no such value exists
  • Smell: return null. Solution: @NotNull annotation for guard clause based null return. Explicit null return solvable with Optional
  • Return an exception in exceptional case! Don’t return null!
  • Field-level null can be acceptable. Make sure it’s well understood and well-documented what can be null and why