Java: How to check for null pointers efficiently

前端 未结 14 1719
清歌不尽
清歌不尽 2020-12-03 01:17

There are some patterns for checking whether a parameter to a method has been given a null value.

First, the classic one. It is common in self-made code

相关标签:
14条回答
  • 2020-12-03 02:01

    Aren't you optimizing a biiiiiiiiiiiiiiit too prematurely!?

    I would just use the first. It's clear and concise.

    I rarely work with Java, but I assume there's a way to have Assert only operate on debug builds, so that would be a no-no.

    The third gives me the creeps, and I think I would immediately resort to violence if I ever saw it in code. It's completely unclear what it's doing.

    0 讨论(0)
  • 2020-12-03 02:02

    The first method is my preference because it conveys the most intent. There are often shortcuts that can be taken in programming but my view is that shorter code is not always better code.

    0 讨论(0)
提交回复
热议问题