Where to check if an object is null or not?

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

    0 讨论(0)
  • 2020-12-17 11:03

    You mean checking in both methods? I'd check in PrintAge for sure and if it makes sense within Main as well. I don't think there is a definite answer in general. It depends :-)

    0 讨论(0)
  • 2020-12-17 11:07

    I would say that checking it n PrintAge seemed to make more sense as that is fulfiling the contract for the routine. You could, of course, replace the null checks with Debug.Assert() code to check at test time, but not at release time.

    0 讨论(0)
  • 2020-12-17 11:07

    What would you want to do, if instance is null?

    I think it depends on the API you provide & define contract (the way .net framework classes do). Having said that, you need not do a check for null (in main), if the method defines what is expected outcome in case of null reference passed to it.

    0 讨论(0)
  • 2020-12-17 11:08

    If you design a library, there will be methods exposed to the outer world. You should check the incoming data in this methods. No checks are required in methods that you do not expose, because only your code calls them and its logic should handle all cases you accepted in the exposed method called.

                        --------------------------
                       |                          |
                       |         Library          |
                       |                          |
     -------        ---------        ----------   |
    |       |      |         |      |          |  |
    | Outer |      | Library |      | Library  |  |
    |       | ===> | Entry   | ===> | Backend/ |  |
    | World |      | Method  |      | Helpers  |  |
    |       |      |         |      |          |  |
     -------        ---------        ----------   |
                       |                          |
                       |                          |
                        --------------------------
    

    If you have accepted the supplied data in the entry method, you should perform the requested action and return the exspected result, that is handle all remaining cases.

    UPDATE

    To clearify the situation inside the library. There might be null checks, but only because of the logic, not because of parameter validation. There are two possibilties for the location of null checks inside the library. The first one if the called method knows how to handle null values.

    private CallingMethod()
    {
       CalledMethod(someData);
    }
    
    private CalledMethod(Object parameter)
    {
       if (parameter == null)
       {
          // Do something
       }
       else
       {
          // Do something else
       }
    }
    

    And the second situation if you call a method that cannot handle null values.

    private CallingMethod()
    {
       if (someData == null)
       {
          // Do the work myself or call another method
       }
       else
       {
          CalledMethod(someData);
       }
    }
    
    private CalledMethod(Object parameter)
    {
       // Do something
    }
    

    The whole idea is to reject cases you cannot handle immideatly and handle all remaining cases properly. If the input is not valid you throw an exception. This forces the library caller to supply only valid values and does not allow the caller to continue execution with meaningless return values (besides the caller shallows the exception an continues).

    0 讨论(0)
  • 2020-12-17 11:08

    You've got nothing to check in Main - you're using the new operator which never returns null (except for Nullable<T>).

    It would be entirely reasonable to check in PrintAge, particularly if it were made public. (For private APIs it's less important to do argument checking, but it can still be very useful.)

    if (person == null)
    {
        throw new ArgumentNullException("person");
    }
    

    These days in C# 3.0 I usually use an extension method for this.

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