Good programming practice with regard to conditional return statements

I would like to know which of the following is the best programming practice:

// Below is the contents of a dummy method which is passed a boolean "condition" as a parameter.
int valueA = 3;
int valueB = 5
if (condition == true) {
return valueA
}
else {
return valueB
}

      

Alternatively, I could write the same code like this:

int valueA = 3;
int valueB = 5
if (condition == true) {
return valueA
}
return valueB

      

In both cases, B is returned only if the condition is false, so the "else" is not required, but is it better to include it anyway?

+3


source to share


8 answers


The code is clearer to read "at a glance" with an else statement to make it better.



Alternatively, some (but not all) of the coding guidelines say that you should only return once at the end of the method. In this case, you have to "return" a value, set it appropriately in the if (which in this case needs something else), and then return that value at the end of the method.

+2


source


Take the following example to see how using else

will help you understand the code better

if (inputVar == thingOne) {
    doFirstThing();
} else if (inputVar == secondThing) {
    doSecondThing();
} else {
    doThirdThing();
}

      

I could write it the same way.

if (inputVar == thingOne) {
    doFirstThing();
    return;
}
if (inputVar == thingTwo) {
    doSecondThing();
    return;
}
doThingThree();
return;

      



Now ask yourself which code looks clearer and where do you understand the most.


Indeed, it’s clear to the end what the code is doing (not necessarily which bit of code is the shortest or least indented).

+3


source


I would like to put there also for readability. However, you can also write a shorthand if / else statement:

return condition ? valueA : valueB;

      

Again, this is your own preference for how you write it.

+2


source


The operator else

provides more clarity on the flow of conditions, you can accept the first snippet and no one will blame you, but it would be better to expand your database of operations on conditional blocks.

I would even suggest using the aux value int

to store the returned result, and then using a single return statement, and this will surely provide more clarity if you care about developers landing and looking at your sources:

public int test(boolean condition)
{
  int valueA = 3;
  int valueB = 5;
  int result;
  if (condition == true) 
  {
    result = valueA;
  }
  else
  {
    result = valueB;
  }

  return result;
}

      

Alternatively, I would recommend the ternary expression mentioned by @stealthjong in his answer , as it is more readable when the condition is short and no nested instructions are wired into the second and third parameters.

+1


source


See how it works the same, but there is only one difference, your line code will be smaller in the second. If you take my advice, I personally follow the second when it comes to such state (s). or you can use this one.

return condition ? valueA : valueB;

      

0


source


I say yes, even though I'm used to it. The programming is very repetitive and the practice becomes habitual. Using the else statement is more efficient, cleaner.

0


source


I prefer to use an if-else statement, returning a value in each block when the method line number is less than 10.

However, if there are 30 or more lines, it is unlikely to read if-else statements, so using just the return value instead of using the else value might be better.

0


source


I would suggest using an if-else block (first approach) because it improves readability. However, for performance consideration, I have chosen two of your approaches in the program. The code looks like this.

public class test {
public static void main(String args[])
{
    int count1=0,count2=0;

    for(int i=0;i<50000;i++)
    {


    long timeStart=System.nanoTime();
    method1(false);
    long timeEnd=System.nanoTime();
    long result1=timeEnd-timeStart;
    System.out.println("\n\nTime taken for method 1 is :"+result1);



    long Start=System.nanoTime();
    method2(false);
    long End=System.nanoTime();
    long result2=End-Start;

    System.out.println("Time taken for method 2 is :"+result2);

    if(result1>result2)
    {
        //increment count2 when result2 execution speed is high (i.e) less time
        count2++;
    }
    if(result1<result2)
    {
        //increment count1 when result1 execution speed is high

        count1++;
    }
    }

    System.out.println("\n\ncount1 value at the end is\t"+count1);
    System.out.println("count2 value at the end is\t"+count2);

}



public static int method1(boolean condition)
{
    int valueA = 3;
    int valueB = 5;
    if (condition == true) {
    return valueA;
    }
    else 
    {
    return valueB;
    }
}


public static int method2(boolean condition)
{
    int valueA = 3;
    int valueB = 5;
    if (condition == true) {
    return valueA;
    }
    return valueB;
}
}

      

Output:

Testcase 1:

      

count1 at the end is 10707 count2 at the end is 10977

   Testcase 2:

      

count1 at the end - 10310 count2 at the end - 10225

Testcase 3:

      

Count1 at the end is 9590 count2 at the end is 10445

Testcase 4:

      

count1 at the end is 10687 count2 at the end is 10435

 Testcase 5:

      

Count1 at the end - 10670 count2 at the end - 10223

 Testcase 6:

      

Count1 at the end - 10594 count2 at the end - 10810

     Overall cumulative result of all 6 test cases is count1 =62558  count2=63115

      

therefore there is not much difference in performance on both approaches. Therefore, it is better to use an if else block as it gives equal performance (in terms of time) and improves readability.

0


source







All Articles