Which is better apply two conditions in nested If or using single with And?
Solution 1
Are you going to do something different in the 'nested if' example if, in fact, txtPackage.Text isn't empty but contains something other than "abc"?
If you aren't, I'd ask why are you checking for string.empty at all?
You could just write:
if (txtPackage.Text == "abc")
{
//
}
and be done with it.
Totally depends upon what you want to do in the end.
Solution 2
I wasn't going to chime in, but seeing that some answers here seem to be about "I like my code to look like this"... I feel that I should say something :)
"Better" means the code will execute faster, or it's more readable / extendable. You would want to nest your if's in the case that you would possibly have multiple checks that all have a common requirement.
Example:
if (myThingy != null)
{
if (myThingy.Text = "Hello") ...
if (myThingy.SomethingElse = 123) ...
}
EDIT: It also needs to be said that nesting your IF's requires more CPU cycles (and is therefore "slower") than a single IF. On top of that, the order of your conditions can greatly increase performance.
Exapmle again:
if (somethingQuick() && somethingThatTakesASecondToCalculate()) ...
is a LOT faster (of course) than
if (somethingThatTakesASecondToCalculate() && somethingQuick()) ...
Because if the first part of the IF fails, the second part won't even be executed, thus saving time.
Solution 3
no one has mentioned maintaining that code. Which is easier to debug?
if this fails:
if (thisIsTrue && thisIsTrueToo)
doStuff();
you know one of these two statements failed, but not which one.
if this fails:
if (thisIsTrue) {
if (thisIsTrueToo)
doStuff();
}
your exception tells you the line number. I say go for easy maintenance, as you are likely not going to be on this code forever.
Solution 4
You really need to define what you mean by "better".
My style is to use one if and an AND if, like in your example, I'm testing the same thing for two different values.
If the two tests are conceptually different, I'll probably nest them
if (!user_option.work_offline) {
if (no_current_connection) {
start_connection()
}
}
Solution 5
It depends on what exactly you want to achieve. It's a logical question rather than a programming query. If you have a dependent condition i.e. If the first is TRUE and then test the second condition; if second TRUE then do something , if FALSE do something, in this case you need to use a nested if. But you need the state of both the conditions to do something then you can go with the operators.
Related videos on Youtube
Nakul Chaudhary
Updated on April 18, 2022Comments
-
Nakul Chaudhary over 1 year
Nested If or single if with And operator, which is better approach?
Single If with Andif (txtPackage.Text != string.Empty && txtPackage.Text == "abc") { // }
Nested If
if (txtPackage.Text != string.Empty) { if (txtPackage.Text == "abc") { // } }
-
Timothy Khouri almost 15 yearstypo: "I'd asK why"... but other than that... very well said :)
-
Raghib Ahsan almost 15 yearsIn .NET, it doesn't matter if two IF's are nested or not, they are converted to the exact same MSIL code. Therefore, nesting won't require more CPU cycles. But of course, this is only when the end result is the same.
-
Garry Shutler almost 15 yearsIn any case: HOLY PREMATURE OPTIMISATION BATMAN! I'd rather be concerned about my code being readable to begin with rather than stressing over individual CPU cycles.
-
Timothy Khouri almost 15 yearsI don't worry about CPU cycles as such... I was simply pointing out that "how my code looks" shouldn't be the first consideration. If one of my senior developers messed up the second example though... there would have to be some e'splainin to do :)
-
configurator almost 15 years"How your code looks", or more exactly, "readability" is most of the time the MOST important thing in code.