Where to check if an object is null or not?

前端 未结 13 1453
广开言路
广开言路 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.

    0 讨论(0)
  • 2020-12-17 10:54

    You can design a method to work with valid objects only.

    That means you are expect to receive valid objects ( not null in your case ).
    That means you don't know how to react and what to do with invalid objects:

    • returning silently from the function is not a solution;
    • throwing an exception will mean you move responsibility to the upper methods where they can check the value already before passing to you.

    So if your method don't know exactly how to handle invalid object and the method won't follow additional logic in the invalid case you should put

    Debug.Assert( Person );
    

    at the PrintAge begin and this will force you to make checks upper by call stack.

    The lower function in hierarchy is the less checks it should do. The following is disadvantages of doing checks in the functions that do the work.

    • Function that is doing actual work has to be as clear as possible without mass of ifs
    • Function will be called more then many times
    • Such function can call such functions and they can call such functions again. Each of them will perform the same validation
    0 讨论(0)
  • 2020-12-17 10:54

    Definitely check in PrintAge, it's a right place to check. It can be redundant but won't hurt anyone unless you execute it 1000 times per second. (Depending on the check throw an exception or fix it if you can)

    Other check is depend on your actual flow, in this example you don't have a flow so I can't comment on that bit. But generally consider your parameters as tainted.

    0 讨论(0)
  • 2020-12-17 10:56

    There is only one occasion that a constructor can return null [new() on a Nullable<T>] - so the calling code doesn't need to check.

    The callee probably should check; throwing an ArgumentNullException if it was null. In .NET 4.0 this will be better served by code contracts. But not yet ;-p

    0 讨论(0)
  • 2020-12-17 10:57

    I prefer null checks inside methods for two reasons.

    1. I think functions should be 'complete', ie handle null values/'edge cases' and not rely on callers. This is for two reasons,

      • someone calling the method later might forget to add null checks
      • it's easier to test the method with edge cases in unit tests.
    2. having null checks inside the method reduces overall number of null checks inside the code, which usually means more readable code

    0 讨论(0)
  • 2020-12-17 10:59

    As I understand your question it is more general than illustrated by your example. My preferences are as follows:

    • All publicly accessible methods must check for NULL input and throw exceptions as appropriate. So if you're building a framework for others to use, code defensively.
    • Private methods may omit the NULL check if you know that this is done elsewhere or that arguments will never be NULL, but in general I prefer the explicit ArgumentNullException to NullRefereceException.

    Brad Abrams has some more input here: http://blogs.msdn.com/brada/archive/2004/07/11/180315.aspx

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