2

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
    }
}
Dynamic
  • 5,746
  • 9
  • 45
  • 73
  • 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
    possible duplicate of [Should I return from a function early or use an if statement?](http://programmers.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement) and http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from – Doc Brown Jun 12 '13 at 15:20

4 Answers4

5

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.

MrWhite
  • 272
  • 2
  • 10
2

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.

James Snell
  • 3,168
  • 14
  • 17
2

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.

Uwe Plonus
  • 1,310
  • 9
  • 16
  • 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
  • No, it's still an interesting question for the parameter check. I think I'll write another question focusing on what you have pointed. – Seraphim's host Jun 12 '13 at 15:05
  • 1
    @Seraphim'shost Please see [here](http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from) for a complete answer to your question then. – Uwe Plonus Jun 12 '13 at 15:06
1

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.

SRKX
  • 1,939
  • 1
  • 15
  • 24