- #1
SlurrerOfSpeech
- 141
- 11
I often encounter situations in my professional development like
and I handle it in exactly that manner. (Note: this is not an if-elseif-else situation. condition1 and condition2 are not mutually exclusive.)
However, my programming intuition says there's something wrong when it sees that the truthiness of condition1 and condition2 are each checked twice. The alternative
is even more like spaghetti code. I have yet a third way that gets rid of the redundant conditions by doing
Yes, I've actually used the above thingy in real life. I feel bad for anyone who has to read my code and figure out what it does.
What is the proper way of handling this?
Code:
if ( condition1 )
{
// do this
}
if ( condition2 )
{
// do that
}
if ( !condition1 && !condition2 )
{
// do something else
}
and I handle it in exactly that manner. (Note: this is not an if-elseif-else situation. condition1 and condition2 are not mutually exclusive.)
However, my programming intuition says there's something wrong when it sees that the truthiness of condition1 and condition2 are each checked twice. The alternative
Code:
if ( !condition1 && !condition2 )
{
// do something else
}
else
{
if ( condition1 )
{
// do this
}
if ( condition2 )
{
// do that
}
}
is even more like spaghetti code. I have yet a third way that gets rid of the redundant conditions by doing
Code:
int condval = 1 * (condition1 ? 1 : 0 ) + 2 * (condition2 ? 1 : 0);
if ( condval & 1 )
{
// do this
}
if ( condval & 2 )
{
// do that
}
if ( !condval )
{
// do something else
}
Yes, I've actually used the above thingy in real life. I feel bad for anyone who has to read my code and figure out what it does.
What is the proper way of handling this?