Jump to content
  • Advertisement
Sign in to follow this  
utilae

which is better?

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Catafriggm
2 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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!