2

I've stumbled across both of these methods however I can't decide which one to ultimately go for.

public void myMethod() {
    if(isTrue())
        return;
    // code...
}

Vs.

public void myMethod() {
    if(!isTrue()) {
        // code...
    }
}

Anyone got some exciting news as to which is better practice? I'm just curious as to how people approach this.

Taking a look at Invert "if" statement to reduce nesting explains the readability, however Java != C#, is there any difference?

Community
  • 1
  • 1
basickarl
  • 37,187
  • 64
  • 214
  • 335
  • I think this is mostly opinionated and will probably get down voted and closed, I always go for the latter option in any case – Leon May 13 '15 at 04:52
  • 5
    http://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html – Andrew May 13 '15 at 04:53
  • The one that makes nested code shorter. – Maroun May 13 '15 at 04:54
  • Completely opinion based. It doesn't harm in choosing any of the way. First one is a little more readable to me as I will know in advance that the method returns if condition is false. – Aakash May 13 '15 at 04:54
  • @MarounMaroun I agree – basickarl May 13 '15 at 04:55
  • 1
    possible duplicate of [Invert "if" statement to reduce nesting](http://stackoverflow.com/questions/268132/invert-if-statement-to-reduce-nesting) – Sachin Gupta May 13 '15 at 04:56
  • Both are ok. Use one which suits better to your situation. – akhil_mittal May 13 '15 at 04:56
  • @SachinGupta Indeed it is a duplicate! Didn't know what to search for. – basickarl May 13 '15 at 04:57
  • Similar to @Andrew Piliser's comment, the idea of having guard clauses helps reduce the ["Arrowhead Anti Pattern"](http://blog.codinghorror.com/flattening-arrow-code/). I think opting for your first approach is best. See Step 4 of the link provided - "Always opportunistically return as soon as possible from the function" – Mr Moose May 13 '15 at 05:01

3 Answers3

1

Dear Karl for readbility and complexity reasons I would say the first way is better:

  1. the first option is more clear, it avoids to have a method that is two level of indentation when there are not other statues: isTrue() is the condition of return, and should highlighted instead of having to read the whole method code.
  2. with the first option, in general, the ciclomatic complexity (https://en.wikipedia.org/wiki/Cyclomatic_complexity) is lower, epecially if you have a lot of code in the if clause.
  3. having an if with negation and then 'isTrue' is less clear than without the negation. It's simpler to understand.
Luca Foppiano
  • 157
  • 12
0

I would prefer the second one, the reason being, we are focusing on the actual business logic. It also comes with the pre-requisites that are required to make sure our application works as expected.

public void myMethod() {
    if(!isTrue()) {
        // code...
    }
}

Saying this I don't mean the the first way is not a got practice. Instead you can say we can log / follow the end-users flow, how they use the application, what are the trends and where they usually go. This also eases code readability.

public void myMethod() {
    if(isTrue())
        return;
    // code...
}

Coming to the memory management, it does not really matter to JVM as both is ideally going to take the similar execution time.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
0

Sometimes, we may have to return something in both case - whether the condition is true or false. Then we can do something like this:

public someReturnType someMethod(){

   boolean isTrue = true;

   if(!isTrue){
      //do something which 
      //you want to do when the condition is false

      return;
   }

   //do other thing which you want to 
   //do if the condition is true

   return;
}

And more over sometimes we return an ArrayList or a String from a method then we can do this -

public ArrayList<Client> getClient(String city){

   List<Client> clients = //some query that generate ArrayList<Client>
   //do something

  return (null != clients ? clients : Collections.EMPTY_LIST);

  //While returning 'String' then you can do something like -
  //return (null != someString ? someString : ""); 

}

The advantage of this approach is - the code using getClient() method don't require to check against null each time it get a ArrayList<Client> from the getClinet() method.

Razib
  • 10,965
  • 11
  • 53
  • 80