Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether to check for null

I know that you should always check incoming params to a method for null. But what if I have this scenario with a try/catch referring to a local variable. Do I really need to check for null below? Because it's gonna catch it anyway if it's null and the next line of code tries to use the refundResponse variable:

    public string DoRefund(...)
    {
        try
        {
    ......
            string refundTransactionID = string.Empty;
    ......

            RefundTransactionResponseType refundResponse = transaction.DoRefund(...);

            if (refundResponse != null)
                refundTransactionID = refundResponse.RefundTransactionID;
    .....
        }
        catch (Exception ex)
        {
            LogError(ex);
            return ex.ToString();
        }
    }

Remember I'm talking specifically about local variables and checking those inside a method, not incoming params to a method.

All I'm asking here is do I need to check for null before setting refundTransactionID or do I just set it without the if assuming that the compiler will handle and throw if it is null which will be caught and thrown back as a string to the caller in this case.

or should it be

if (refundResponse == null)
                return null;

or just take the check out completely for this local variable assignment and then since in this case I have a try/catch I'm handling any exceptions picked up by the compiler naturally by returning the exception as a string to the caller (it was not my decision to send back a string, it was a requirement by my boss...so bypass that debate for now):

 refundTransactionID = refundResponse.RefundTransactionID;

ultimately the rest of the code further down the line in the method is dependent on a valid refundTransactionID.

like image 760
PositiveGuy Avatar asked Mar 24 '10 19:03

PositiveGuy


People also ask

When should you check for null?

Check for nulls if your code can offer a reasonable handling of null cases. If you simply throw another exception upon detecting a null, there is little value in handling the null explicitly. Document all instances of a function returning nulls in the function's javadoc.

Is it good practice to check for null?

Avoiding Null Checks Through Coding PracticesIt's usually a good practice to write code that fails early. So, if an API accepts multiple parameters that aren't allowed to be null, it's better to check for every non-null parameter as a precondition of the API.

How do you check for null?

The simplest way to check for null is to know that null evaluates to false in conditionals or if coerced to a boolean value: Of course, that does not differentiate null from the other falsy values. Next, I explore using the == or === equality operators to check for null.

How do you know if a value is not null?

You can easily check if a variable Is Null or Not Null in JavaScript by applying simple if-else condition to the given variable.


2 Answers

Exceptions are for exceptional conditions. If you can check for a continuable error, do so, please!

like image 86
leppie Avatar answered Oct 03 '22 18:10

leppie


I know that you should always check incoming params to a method for null.

No, not necessarily. What you should specify is the contract of your method. It's perfectly acceptable (and common) to specify that you'll throw a NullPointer/NullReferenceException for a null parameter. Then you don't need any checking.

You can also check for null, but this only makes sense if you can actually handle a null usefully (e.g. substitute a default value).

like image 45
sleske Avatar answered Oct 03 '22 19:10

sleske