What is the proper format when you provide every possible result in an if-else chain?

I'm asking this from a Java perspective, but it might apply to other languages ​​that follow a similar compiler pattern.

This can apply to any type of method where you provide all possible results, but I'll use the method boolean

as an example. I have listed two ways to code this method:

Example 1:

public boolean example1(final int value1, final int value2) {
    if (value1 == value2) {
        //do something 1
        return true;
    } else if (value1 < value2) {
        //do something 2
        return false;
    } else if (value1 > value2) {
        //do something 3
        return false;
    } //could also be in an "else" block.
    return false; //impossible to reach, but necessary to compile.
}

      

Positive:

  • Allows future viewers of a method to immediately determine what exactly the method does.

Negatives

  • A bit "hacked" and almost unclean.

Example 2:

public boolean example2(final int value1, final int value2) {
    if (value1 == value2) {
        //do something 1
        return true;
    } else if (value1 < value2) {
        //do something 2
        return false;
    } else { //The same as (value1 > value2)
        //do something 3
        return false;
    }
}

      

Positive:

  • It is not clear what this method does. (Although this can be prevented with proper documentation)

Negatives

  • It is not clear what this method does.
  • The other, at least to me, might mean that the only important comparisons are the first 2. After all, otherwise it means "everything else", which can be misleading and / or confusing.

These methods do the same. However, I find Example 1 is easier to read than Example 2, simply because it explicitly specifies the last if statement in the if-else chain. At the same time example 1 feels extremely, for lack of a better word dirty for me.

Is there a correct way to do this, or is it just personal preference?

EDIT: I decided not to include the set of if statements (not in the if-else chaining) because I know this is not a good way to do it. If someone disagrees with this and has good reason to do so, write below and I will change my question.

EDIT 2: The reason for this opinion is that if "do something", for example, more than 100 lines of code, without having an if-else chain, would significantly reduce readability. In short examples where there is only 1 or a few lines of code to "do something", then yes, that might be viable. For this reason, I'll add this as a third example below (although I still don't think this is very readable, this is personal preference.)

Example 3:

public boolean example1(final int value1, final int value2) {
    if (value1 == value2) {
        //do something 1
        return true;
    }
    if (value1 < value2) {
        //do something 2
        return false;
    }
    //do something 3
    return false;
}

      

Positive:

  • It's easier to read (for some people) when there are only a few lines of code to "do something".
  • Removes technically redundant "else" statements.

Negatives

  • Much harder to read in situations where there are hundreds of lines of code to “do something”.
  • The presence of "do something 3" outside the if statement indicates at first glance that the line will always be executed until return statements have been seen in previous if statements.

Example 4 (and my personal favorite):

public boolean example1(final int value1, final int value2) {
    if (value1 == value2) {
        //do something 1
    } else if (value1 < value2) {
        //do something 2
    } else if (value1 > value2) {
        //do something 3
    }
    return value1 == value2;
}

      

This is great because it first takes care of "doing something" and then restricts the return statement to a separate, clean, easy-to-read statement with no "hacky" consequences.

+3


source to share


4 answers


Personally, I'm a fan of only one operator return

when it makes sense. For your code, I think it does. So, I would use ...

Option 3:

public boolean example1(final int value1, final int value2) {
    if (value1 == value2) {
        //do something 1
    } else if (value1 < value2) {
        //do something 2
    } else if (value1 > value2) {
        //do something 3
    }
    return value1 == value2;
}

      



This particular structure doesn't always make sense, but since you always return value1 == value2

, I think it cleans things up a bit to only do it once at the end.

It should be pretty clear to anyone who goes and looks at the code what's going on. The method always ends at the same place with the same value, and before that there is a simple bit of workflow logic.

+1


source


As you noted, this is a matter of preference. The third one if

in the first example is redundant and some IDEs even warn about it.
Personally, I would go with the second one as I despise code that is redundant, but it all comes down to project standards and personal preference.

EDIT:
Actually, if we are talking about personal preference, I would remove it else

altogether - if you enter a block if

, you are just return

from a function, therefore else if

they else

are redundant



public boolean example1(final int value1, final int value2) {
    if (value1 == value2) {
        //do something 1
        return true;
    } 

    if (value1 < value2) {
        //do something 2
        return false;
    } 

    //do something 3
    return false;
}

      

+3


source


Example 2 is much better. In example 1, the system must pass a different set of "Validations", while in example 2, after 2 validations, it simply continues with the default operation.

So at least 1 less operation in example 2.

+1


source


Option 3 -

public boolean example2(final int value1, final int value2) {
    if (value1 == value2) {
        //do something 1
        return true;
    }

    if (value1 < value2) {
        //do something 2
        return false;
    }

    //The same as (value1 > value2)
    //do something 3
    return false;
}

      

In other words, you don't need else

if there is a statement at the end of each block return

, and if it clears its execution.

This is what I prefer when the code blocks are very short. If they are long, it is difficult to see the instructions return

.

+1


source







All Articles