Where to check if an object is null or not?

前端 未结 13 1490
广开言路
广开言路 2020-12-17 10:18

Where do you check if an object that you are passing to a method is null or not?

Should an object need to be tested before calling a method? or within the method tha

13条回答
  •  情话喂你
    2020-12-17 10:47

    Redundant code isn't the most elegant but its safe.

    This depends on who your intended user is, if its you then your in control of how everything is used and the checks are only necessary if your unsure of what the state of your variables will be.

    If your making this for someone else to use then null checks are probably a good idea. Even if you just throw a NullPointerException its better to fast fail.

提交回复
热议问题