And the double negative can make the code harder to interpret down the line.
Or even the first time.
Rereading the original posted code:
Code:
bool nothingElseOn, itsARerun;
nothingElseOn = true;
itsARerun = true;
if ( nothingElseOn || (! itsARerun) )
printf( "Let's watch Family Guy!\n" );
else
printf( "Something else is on or I've seen this one.\n" );
If the interpretation of itsARerun is intended to correspond to "I've seen this one", then I think this code is wrong, in the sense of not matching the logic expressed in the message texts. The ! should be removed, and I also suspect the || is wrong.
If the entire test is reversed, using the Boolean algebra for negation, I'm pretty sure we get this:
Code:
if ( (! nothingElseOn) && itsARerun )
printf( "Something else is on or I've seen this one.\n" );
else
printf( "Let's watch Family Guy!\n" );
Here, "Something else is on" logically corresponds to
(! nothingElseOn), and "I've seen this one" also corresponds to
itsARerun, however, the
&& doesn't match the "or" in the string. (In boolean algebra, the logical NOT of AND is OR, and vice versa, i.e. NOT AND is OR, and NOT OR is AND.)
Naming the variable nothingElseOn was also a poor choice, akin to choosing itsNotARerun.
So let's revise it to improve the variable naming and the logic.
Code:
bool somethingElseIsOn, iveSeenIt;
somethingElseIsOn = true;
iveSeenIt = true;
if ( somethingElseIsOn || iveSeenIt )
printf( "Something else is on or I've seen this one.\n" );
else
printf( "Let's watch Family Guy!\n" );
Now, if we reverse the whole conditional:
Code:
if ( (! somethingElseIsOn) && (! iveSeenIt) )
printf( "Let's watch Family Guy!\n" );
else
printf( "Something else is on or I've seen this one.\n" );
You can read this as "If something else is not on, and I have not seen it, let's watch Family Guy". You can further simplify "something else is not on" to "nothing else is on".
Again notice the && here, not an ||. Which further reinforces my thinking that the code logic doesn't match the logic expressed in the text messages.
To the OP and any other interested reader:
Yes, this is how real-world code is designed. You first have to correctly understand the real-world expression and operation of something, also known as How It Works In Reality. Then you have to correctly translate that into code, or How The Software Works (or doesn't). Too many people skip the first part, and the resulting code is either wrong, or works for the wrong reason. If it works for the wrong reason, a subtle change in the real world can cause the code to stop working, even when it should still work.