Best way to handle a NULL

2019-03-23 12:30发布

At the top of my functions I'm trying the best way to handle a null coming into my procedures in C#. Which is the best way for checking and handling the null and why? I've added the complete code of what I'm using right now and Resharper is telling me to use Option #1. Normally I do what it says as I understand why it makes it more efficient. This time though I'm not sure so I must ask.

Option #1
if (sender == null) return;

// Code goes here

or

Option #2
if (sender != null)
{ 
     // Code goes here
}

Complete Code
        private void EmployeeMouseHoverToolTip(object sender, EventArgs e)
        {
            if (sender != null)
            {
                var sUserIdentifier = ((C1TextBox)sender).Text;
                var userIdentifier = Guid.Empty;
                if (Utilities.IsGuid(sUserIdentifier))
                {
                    userIdentifier = new Guid(sUserIdentifier);
                }

                var toolTipText = Utilities.UserIdentifierToName(userIdentifier);
                c1SuperTooltip.SetToolTip(sender as C1TextBox, toolTipText);
            }
        }

18条回答
Emotional °昔
2楼-- · 2019-03-23 12:44

Why not just pretend that a null reference never occurs, and don't catch the NullPointerException?

You get a stack trace, plenty of information, and it's handled as an exception.

查看更多
等我变得足够好
3楼-- · 2019-03-23 12:44

Don't check for it.

If you get nulls, you've added the handler to something you shouldn't have. And if some other bug causes it, you should be handling it with WinForms' global exception handler so the program doesn't bomb, logging it, and uploading the logs to your site whichever way you can to check for such errors.

查看更多
forever°为你锁心
4楼-- · 2019-03-23 12:44

Resharper likes option 1 as it is a pre-condition checker. When pre-conditions are not met, an early return is done.

Usually an early return is destructive for code-readability, but in this case it is very readable.

This way you can easily add extra pre-condition checks, like checking the contents of the EventArgs e, without having to rework the main function code.

查看更多
▲ chillily
5楼-- · 2019-03-23 12:44

Similar to @Konrad but I like the idea of adding the exception in the end if the code

if( sender != null )
{
    // Code goes here
    ....
} else
    throw new ArgumentNullExcpetion("sender");

So I vote for Option #2

查看更多
SAY GOODBYE
6楼-- · 2019-03-23 12:46

I do not handle null for private methods, as I always make sure no null value will be sent to my private methods. If something went wrong and null has been passed to private method, so the exception will be thrown from as it should be and I will know that I did something wrong. If you always check for null value for private method, you might be skipping some logical error at run-time and you will never know you got one in your code until it hits you in the production.

查看更多
该账号已被封号
7楼-- · 2019-03-23 12:47

you can use DBNull Class while assigning the value to an object...

UserName = DBNull.Value != reader["UserName"] ? reader["UserName"].ToString() : default(string);
查看更多
登录 后发表回答