Where to check if an object is null or not?

前端 未结 13 1494
广开言路
广开言路 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:59

    PrintAge should be a method on Person, not a static taking a Person as parameter. No check needed.

    Checking for null values makes the code unnecessarily complex. Structure your code so as to limit (or eliminate) the occasions where null is a possible value, and you'll have much fewer checks to write.

提交回复
热议问题