Home > database >  Should i use if-else or switch
Should i use if-else or switch

Time:10-27

I have Abstract Class with two abstract methods. I also have 4 classes that extend that class and also override those methods.

The task is to "if possible optimize code" in those 4 classes.

There are two types of same override method

First

    protected boolean canApprove(int id, double cost, Type type) {
        boolean result = false;

        if (type == Type.CONSUMABLES && cost < 300) {
            result = true;
            return result;
        } else if (type == Type.CLERICAL && cost < 500) {
            result = false;
            return result;
        } else if (type == Type.GADGETS && cost < 1000) {
            result = true;
            return result;
        } else if (type == Type.GAMING && cost < 2000) {
            result = true;
            return result;
        } else if (type == Type.PC && cost < 5000) {
            result = true;
            return result;
        } else {
            result = false;
            return result;
        }
    }

Second

protected boolean canApprove(int id, double cost, Type type) {
        boolean result = false;

        switch (type) {
            case CONSUMABLES:
                if (cost < 300) {
                    result = true;
                    return result;
                } else {
                    break;
                }
            case CLERICAL:
                if (cost < 500) {
                    result = true;
                    return result;
                } else {
                    break;
                }
            case GADGETS:
                if (cost < 1000) {
                    result = true;
                    return result;
                } else {
                    break;
                }
            case GAMING:
                if (cost < 2000) {
                    result = true;
                    return result;
                } else {
                    break;
                }
            case PC:
                if (cost < 5000) {
                    result = true;
                    return result;
                } else {
                    break;
                }
            default:
                result = false;
                return result;
        }
        return result;
    }

My Question is which i should use or if there is a better way?

I dont know what method is the best to use?

CodePudding user response:

Neither.

return (type == Type.CONSUMABLES && cost < 300)
    || (type == Type.CLERICAL && cost < 500)
    || (type == Type.GADGETS && cost < 1000)
    || (type == Type.GAMING && cost < 2000)
    || (type == Type.PC && cost < 5000);

CodePudding user response:

Knowing that this is an opinion-based question and it is likely to be closed for that reason, I will still offer my opinion an an answer.

The general consensus, and general is being used here VERY loosely, is that switch statements are better than nested if/else structures because the evaluated variable is evaluated once in a switch rather than multiple times in an if/else. The question is, how much of a performance gain you get by using a switch. In my opinion, not much. This is why the other school of thought is that you should then err in the side of improved readability.

There are cases when the if/else structure is much easier to read than a switch. One example of this case is calculating a grade based on points:

if(points >= 90) {
    grade = 'A';
} else if (points >= 80) {
    grade = 'B';
} else if (points >= 70) {
    grade = 'C';
} else if (points >= 60) {
    grade = 'D';
} else {
    grade = 'F';
}

If you do this using a switch, the structure is crazy long and thus harder to read. And because this nested structure is so shallow, most likely you will gain no significant improvement by using a switch.

Conclusion: it is up to you, and your team if working in a project, to decide which one to use. AND, if I didn't make this point clear, this decision should be made in a case-by-case basis. Because of that, I must respectfully disagree with the community wiki post on this question. And for being a community wiki posting, it offers no explanation as to why the proposed solution is the better.

  • Related