Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this questionApart from code readability, why is it bad to use lots of if statements?
Every if/else you write increases the number of code paths you have to test.
If you have a lot of if statements one after the other, then a switch case statement is more useful. At least it has a fixed response time for each possible inupt as it does not need to fall through all the if statements.
A lot of if (and else) statement usually indicates
a) Violation of Single responsibility Principle
b) Need to refactor out into Strategy Design Pattern
Well, the question I have to ask back is, "as opposed to what"?
If you mean as opposed to a switch statement, then as well as readability it can sometimes be more efficient. As a rule (certainly in C#, likely in the others) below a certain number of branches it will just be turned into a series of if statements when compiled but beyond that number it will be turned into a hash-based lookup and jump that is likely to be faster on the average case.
Alternatively, if you have good knowledge of the relative frequency of different cases, you can be more efficient with a set of if statements. E.g. if 95% of cases match the first branch, 4% match the second and 1% match all the rest, this can give better performance for the 95% of cases by doing a single comparison and perhaps not even branching in that case.
So. Maybe there's an efficiency gain in this case.
Still, readability counts for a hell of a lot. As an extreme example, there's a Turing-complete language that was designed just for the sake of seeing how small a compiler for a Turing-complete language could be. It's designer named the language "brainf**k". This in itself is a great comment on the importance of readable code in real programming.
Now, a bigger issue with a large number of if statements, is that it often indicates that you are modelling something through those if statements that could be modelled better with a class hierarchy. In a way this is just another take on readability (assuming the program works either way). However, it's about the readability - and understandability - of the program as a whole. It will massively impact upon the mental model you have of the program, extensibility and affect not just how well you can maintain it, but you will conceive of as possible with it. It can easily be the difference between a program that becomes a legacy time-sink until someone works out how to get rid of it, and one that grows to continue to give return on investment.
Sometimes because of processor architecture, it's more efficient to find a way to avoid branching at all; this could be considered a disadvantage of if
statements.
Chusdad is right. Switch structure is better than multiple if-else. The next approach is to use enum as a switch key. In this case you get warning if you forgot to mention one of enum elements in your switch.
But if you are using enum and switch, go forward for better design: remove switch at all!
Declare abstract business method in your enum. Implement it for each enum element. The call enumElement.theMethod() instead of implementing switch.
Here is an example.
enum Foo {
ONE {
public void foo() {
// do something
}
},
TWO {
public void foo() {
// do something
}
},
;
public abstract void foo();
}
Here is the code that uses this:
Foo.valueOf(System.getProperty("foo")).foo();
Compare this line with the following:
String foo = System.getProperty("foo");
if("ONE".equals(foo)) {
//.....
} else if("ONE".equals(foo)) {
//.....
} else {
//.....
}
I think that no comments are required.
if(A)
{
}else
if(B)
{
}else
if(C)
{
}else
if(D)
{
}else
{ }
All if
s will still have to get "calculated" making it something like "O(n)". It should be avoided when possible and use switch(Lalala)
Another possibility - it depends what the if statements are doing. In some cases, they can be making decisions that are better done by a properly designed class hierarchy.
Can you post some examples of if statements you have been told are bad?
If statement can produce a stall in the pipeline. You can read about pipeline Hazards here. So, for very performance critical applications it is not a good idea to have many if statements in a big loop. Check also this article about branch prediction in modern processors, and benchmarks of various if conditions.
Also, I see a lot of people talk about case statement. Check this SO thread.
And for the end, wiki article about pipeline that probably every programmer should have read.
Hope it helps.
If you find yourself writing if
statements for the same condition over and over, then it will be more maintainable to make that part of the type hierarchy.
The reason is that if the logic for this condition changes, you will need to hunt down every if
statement that uses it and modify it.
If that logic is encapsulated in a class hierarchy, you should only need to change it once.
精彩评论