Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

When checking for parameter consistency a the top of a function body, what is the best strategy?

This one:

protected void function(Object parameter)
    if (parameter == null) 
        return;

    //... do things
}

...or this one:

protected void function(Object parameter)
    if (parameter != null) {    
        //... do things
    }
}
share|improve this question
1  
The best strategy is to be consistent in checking for parameter consistency. The rest is a matter of taste. –  superM Jun 12 '13 at 13:52
1  
add comment

marked as duplicate by Doc Brown, Jalayn, GlenH7, Kilian Foth, MichaelT Jun 12 '13 at 23:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

It is a matter of preference which style you choose, but I would go for your first option, for the following reasons. In my opinion....

  • It is cleaner and easier to read if you return early from the function when the function cannot be executed because of some condition.

  • Checking for equality is generally less prone to simple error than checking for inequality. For example, it is certainly easier to miss if you had written the inequality as if (!parameter)

  • The second example means you need to indent your code an additional level.

share|improve this answer
add comment

I would recommend doing the first one because it is clearer but there is a school of thought that you should only have one exit point from a function. This is perhaps a good opportunity to use Exceptions if your language supports them usually via the throw command. Such as...

protected void function(Object parameter)
    if (parameter == null) 
        throw new ArgumentNullException("parameter must not be null.");

    //... do things
}

This will tell the code calling the function that the problem occurred rather than allowing the application to continue under the assumption the function has completed correctly.

share|improve this answer
add comment

The first one is the better one because if you have more than one parameter to check it is much simplier to follow the flow. E.g.:

protected void function(Object a, Object b, Object c) {
    if (a == null) {
        return;
    }
    if (b == null) {
        return;
    }
    if (c == null) {
        return;
    }
    // Do stuff
}

Also as James noted I would use Exceptions if you language supports them.

share|improve this answer
    
My question is not complete. I used the parameter example to explain the point, the return; strategy apply in any other part of the function. –  Seraphim's host Jun 12 '13 at 14:43
    
@Seraphim'shost I don't understand you then. Do you mean that you spread different return statements throughout you function? –  Uwe Plonus Jun 12 '13 at 14:45
    
yes, just wondering if it's a correct coding style... –  Seraphim's host Jun 12 '13 at 15:00
1  
@Seraphim'shost I vote against multiple return statements as it is very difficult to follow the control flow. I think that any answer here missed then your real question... –  Uwe Plonus Jun 12 '13 at 15:04
1  
@Seraphim'shost Please see here for a complete answer to your question then. –  Uwe Plonus Jun 12 '13 at 15:06
show 1 more comment

I think there is not change in terms of performance because in both cases the main code of the function is not evaluated if the parameter is deemed inconsistent.

From this point of view, as superM says in his comment, there rest is pretty much up to you and should be as consistent as possible across your project.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.