# which is better?

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Which is better? This:
if(X=="A" && Y==1)
;//do stuff
else if(X=="A" && Y==2)
;//do stuff
else if(X=="B" && Y==1)
;//do stuff
else if(X=="B" && Y==2)
;//do stuff


Or this:
if(X=="A")
{
if(Y==1)
;//do stuff
else if(Y==2)
;//do stuff
}
else if(X=="B")
{
if(Y==1)
;//do stuff
else if(Y==2)
;//do stuff
}



##### Share on other sites
i like the second one better, but functionally they're the same.

##### Share on other sites
#2, less comparisons.

##### Share on other sites
2 is better, because it has less comparisons, but if the compiler's halfway intelligent, it'll generate the same code either way.

##### Share on other sites
Quote:
 Original post by Catafriggm2 is better, because it has less comparisons, but if the compiler's halfway intelligent, it'll generate the same code either way.

And, because of this, you should choose whichever better describes the intent. For instance, if X can be "A", "B", or "C", then the first is probably clearer. If Y can be 1, 2, or 3, then the first is probably clearer. (If it turns out that the compiler doesn't optimize it and profiling shows that this is a bottle neck, then switch it) If X and Y are limited to the values shown, then the second is probably clearer.

##### Share on other sites
#2, just looks cleaner.

##### Share on other sites
second one. but if it grows up i would use switch function for them, if, elif... doesnt looks good

##### Share on other sites
Quote:
 Original post by DerAnged#2, just looks cleaner.

Really? I like the looks of #1 better. Symmetry and all. However, #2 may be more descriptive of the logic.

By the way, if this is X is a char *, you don't want to be using == to compare strings, you want strcmp(). If X is a char, you want single quotes (') around the character. If X is a C++ stirng, never mind. But you all probably knew all that (and it's just an example).

##### Share on other sites
I say it depends on how the variables are related. Either case may present a more readable solution in different situations. If Y is like a sub-case of X, I'd go with style 2. If they are completely unrelated variables that just happen to control the same logic, I might go with style 1.

##### Share on other sites
Depending on how much there is in common between the cases, use either your option 2, or the equivalent refracted code below.
if(Y==1)   {   if(X=="A")         ;//do stuff   else if(X=="B")         ;//do stuff   }else if(Y==2)   {   if(X=="A")         ;//do stuff   else if(X=="B")         ;//do stuff   }
Also if X can only be "A" or "B" then
else if(X=="B")
could simply be an else. Ditto goes for Y. You'd of course perhaps use an assert, to ensure it was never any other value.
The prettiness of option 1 will wear off you expand it any furthur.

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631067
• Total Posts
2997734
×