How to avoid null checking in Java?

后端 未结 30 3271
失恋的感觉
失恋的感觉 2020-11-21 04:43

I use object != null a lot to avoid NullPointerException.

Is there a good alternative to this?

For example I often use:



        
30条回答
  •  悲&欢浪女
    2020-11-21 05:37

    I like articles from Nat Pryce. Here are the links:

    • Avoiding Nulls with Polymorphic Dispatch
    • Avoiding Nulls with "Tell, Don't Ask" Style

    In the articles there is also a link to a Git repository for a Java Maybe Type which I find interesting, but I don't think it alone could decrease the checking code bloat. After doing some research on the Internet, I think != null code bloat could be decreased mainly by careful design.

提交回复
热议问题