which is better?

Started by
18 comments, last by GameDev.net 18 years, 10 months ago
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
}

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Advertisement
i like the second one better, but functionally they're the same.

Beginner in Game Development?  Read here. And read here.

 

#2, less comparisons.
-------------------------Rayoom Sledge Hammer Productions - Programmer
2 is better, because it has less comparisons, but if the compiler's halfway intelligent, it'll generate the same code either way.
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.
#2, just looks cleaner.
second one. but if it grows up i would use switch function for them, if, elif... doesnt looks good
+-+-+-+-+-STR
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).
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.
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement