3

I always try to avoid nested statements.

They lead me into putting code into a long curly braces. It becomes hard to follow the code when the amount of code with in each condition becomes larger.

if(condtion) {

} else if(condition2) {

} else {

}

So if I have code like this:

void doSomething(){  
    if(condtion) {
        return;
    } else if(condition2) {
        return;
    } else {
        return;
    }    
}

I always change it to this form (I avoided the else as there is a return statement from each condition):

void doSomething() {
    if(condtion) {
        return;
    }    
    if(condition2) {
        return;    
    }
    return;
}

But someone told me mine is a bit hard to read, which one is better?

Jagger
  • 10,350
  • 9
  • 51
  • 93
dsharew
  • 10,377
  • 6
  • 49
  • 75
  • 5
    Your question is hard to read. We don't need so much vertical whitespace. Some of us read SO on a mobile device. – Andy Turner Sep 23 '15 at 07:25
  • Each one use for different purposes. There is a behavioural change in your codes. They are not the same to decide which is best practise. – Suresh Atta Sep 23 '15 at 07:26
  • 2
    I'm old school, I was taught that every function/method should have a single entry and exit point, to this end, the first would preferred. Adding `return` statements within the `if-else if` blocks can mean that people can miss them and suddenly people don't know what's going on. The last can make it difficult to understand the relationship and logic you are trying to achieve (IMHO). If your `if-else` blocks are running long, consider supplementing the functionality for methods `if (condition1) { doSomethingImportant(); }` – MadProgrammer Sep 23 '15 at 07:27
  • @AndyTurner I am updating it – dsharew Sep 23 '15 at 07:27
  • If you have code like the second example, there is no need for the return statements: all branches return when they are done. – Andy Turner Sep 23 '15 at 07:27
  • Why not use `switch` statement. If the `conditional` is not too complicated. – nitishagar Sep 23 '15 at 07:28
  • 1
    Replace conditional logic with polymorphism,Please read [this](https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism) – bsingh Sep 23 '15 at 07:28
  • And also please check [this](http://stackoverflow.com/questions/1199646/long-list-of-if-statements-in-java),Command Design Pattern to replace the long list of if statements. – bsingh Sep 23 '15 at 07:33
  • Could you take a look to [elegant-ways-to-handle-ifif-else-else](http://programmers.stackexchange.com/questions/122485/elegant-ways-to-handle-ifif-else-else) – kiuby_88 Sep 23 '15 at 07:34

4 Answers4

4

If conditions are some quick-cases or preconditions, I prefer immediate return or throw without an else statement. For example:

void processArray(int[] array) {
    if(array == null || array.length == 0) {
        System.out.println("Empty input");
        return;
    }
    // do some complex computations
}

This way the rest of your method which is the main course has no extra indent and no extra brace at the end which probably way below the else statement.

If conditional branches are quite long and has roughly the same length, then the code smells anyways. You need to refactor it extracting the branches to methods or (sometimes better) to separate classes using something like Command pattern.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
2

USUAL CASES:

  • If both results are same, all depends of person that reads the code.

  • When method starts doing something more complex than checking simple conditions , it should have only one return statement.

  • If logic of the method is really complex you must create various methods to clarify.


Oracle Java Code Conventions 7.4 does not mention about return inside if statements, but about if-else statements

7.4 if, if-else, if else-if else Statements

The if-else class of statements should have the following form:

if (condition) {
    statements;
}

if (condition) {
    statements;
} else {
    statements;
}

if (condition) {
    statements;
} else if (condition) {
    statements;
} else {
    statements;
}

So, according ORACLE CONVENTIONS, you must use if-if else-else, but you can choose between:

One single return statement

boolean doSomething(){  
    boolean var;
    if(condtion){
        var = false;
    }else if(condition2){
        var = true;
    }else{
        var = false;
    }    
    return var;
}

Or 3 returns statements.

boolean doSomething(){  
    if(condtion){
        return;
    }else if(condition2){
        return;
    }else{
        return;
    }    
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

But someone told me mine is a bit hard to read, which one is better?

There is no "better". The fact that someone told you that yours is hard to read, while obviously, you find yours easier to read, is testament to the fact that this is completely subjective.

You should do it the way it is easier for you.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • But how about when working in team? – dsharew Sep 23 '15 at 07:51
  • The team should be flexible enough to accept small variations in coding style, stemming from each developer's own preferences, and not mandate absolute conformity from everyone. People should really just relax. – Mike Nakis Sep 23 '15 at 07:54
  • Now, if your workplace sees themselves as the Gestapo, then obviously, you have to abide by their wishes. Quite possibly, while looking for a different job. – Mike Nakis Sep 23 '15 at 07:55
  • Liked your last comment. :-) – dsharew Sep 23 '15 at 07:56
0

if the amount of code with in each condition becomes larger, i'll do :

void doSomething()
{
    if (condtion)
    {
        function1();
    }
    else if (condition2)
    {
        function2();
    }
    else
    {
        function3();
    }
}

like this, i think it's a bit easier to read.

Ozone33
  • 48
  • 8