Monday, September 10, 2012

Coding: Why I hate "single line else"

Have you ever seen code like this?
    public class B {
        boolean state;
        int value;
        String text;
    }
    public void method1(B b) {
        if (b.state) {
            //do something
            //do a lot of work 
        } else {
            log.info("Nooo! b.state illegal value... skip");
        }
    }
or like this?
public void method2(B b) {
        if (b.state) {
            //do something
            //do a lot of work 
        } else {
            throw new RuntimeException("Nooo! b.state illegal value");
        }
    }
or like this?
    public int method3(B b) {
        int result;
        if (b.state) {
            //do something
            //do a lot of work
            result = 1;
        } else {
            log.warning("Nooo! b.state illegal value");
            result = -1;
        }
        return result;
    }
This is what I named, often improperly, "single line else".
Maybe you are not interested in my opinion, but I hate this kind of coding style.
I don't understand why some people write code like that, maybe this people like tabbing code or like to make me forget what condition is related to the last statement.

So maybe now you are thinking that I have a short memory, and this is true, but what happens if code is not as short as "Method 1/2/3" and moreover contains more nested statement than above? Now I'll show you "Method X"!

    public void methodX(B b) {
        if (/* expected condition 1 */) {
            if (/* expected condition 2 */) {
                if (/* expected condition 3 */) {
                    //do work
                } else {
                    throw new RuntimeException("Unexpected condition 3");
                }
            } else {
                throw new RuntimeException("Unexpected condition 2");
            }
        } else {
            throw new RuntimeException("Unexpected condition 1");
        }
    } 
In my honest opinion logthrow and return based on a condition must be done in the if block instead of else, with only few exceptions. Method X could be written in the equivalent form:
    public void methodX(B b) {
        if (/* NOT expected condition 1 */)
            throw new RuntimeException("Unexpected condition 1");
        if (/* NOT expected condition 2 */)
            throw new RuntimeException("Unexpected condition 2");
        if (/* NOT expected condition 3 */)
            throw new RuntimeException("Unexpected condition 3");
        //do work
    }
When I see the longest Method X, often, the same person who wrote it, also codes like this:
    public int method4(B b) {
        int value = b.value;
        if (b.state) {
            return value * 2;
        } else {
            return value / 2;
        }
    }
Besides being able to write it as Method 4 - B
    public int method4(B b) {
        int value = b.value;
        if (b.state) {
            return value * 2;
        }
        return value / 2;
    }
This kind of code could seem same code as before but it isn't, it is worse than Method 4 - A. Writing double return in a logical if else is another thing I hate. If applications logic needs to switch between two conditions to do some work it must contain a single return producing code like this:
    public int method4(B b) {
        int result = b.value;
        if (b.state) {
            result *= 2;
        } else {
            result /= 2;
        }
        return result;
    }

Finally think to this code:
    public int method5(B b) {
        int value = b.value;
        if (b.state) {
            return value * 2;
        } else {
            return value;
        }
    }
in this case it's not a logical double return so i like to convert Method 5 - A into Method 5 - B:
    public int method5(B b) {
        int result = b.value;
        if (b.state) {
            result *= 2;
        }
        return result;
    }
Avoid the "single line else" is what I think and I say: "A person's mind changes now and then so let's everybody try to change our mind", this applies to me too. Keep this post easy!

Related posts:

No comments:

Post a Comment