Vaguely with Uncle Bob explanation on handling null objects in a book Clean Code

I read Uncle Bob a book about handling Exceptions today, and I could remember how to pass null values, so that methods should not handle null values ​​because they clutter up the code. I am a little confused by this. I always thought that a method should always ensure that its dependencies are not null (unless they are injected into the constructor and the constructor are sure of validity). For example, if I have a method

public void SendMessage(IEmailSender emailSender, contactList list)
{
    if(emailSender == null)
    {
         throw new ArgumentNullException("Failed to send  
                message.",MethodBase.GetCurrentMethod().GetParameters[0].Name);
    }
    if(list == null)
    {
         throw new ArgumentNullException("Failed to send  
                message.",MethodBase.GetCurrentMethod().GetParameters[1].Name);
    }

    // rest of code goes here

}

Did I miss something?

+3
source share
3 answers

, , Null Object Pattern, .

if(log != null)
  log.Write("My log message");

ILogger, Write, , : a NullLogger a FileLogger. NullLogger Write.

, ,

+3

:
, , , . , , . , API, .

, , , catch ! , ? , , - , GlobalExceptionHandler.

, , : , API-, .

API, , , ; -)

, CleanCode, :

1. , null ( ). :

var myObject = GetObjectThatDoesSomthing();  
if(myObject != null)  
{  
myObject.DoSomething();  
}

... :

var myObject = GetObjectThatDoesSomething();  
myObject.DoSomething();  

.

2. null , , :

public Point Add(Point p1, Point p2)  
{  
    if(p1 == null) throw ArgumentException();  
    if(p2 == null) throw ArgumentException(); 
    ... 
}

: , , , . , , API, . , API : API , ...

+3

, . , , .

, - , , , . , .

And if the private / internal method parameters still get null values, it's still too late. The Throwing ArgumentNull exception from the private / internal method does not help the external user to correct the cause, so it does not make any difference to him to get an ArgumentNull or NullReference exception.

+1
source

All Articles