Short #1 Don’t stick to ‘no negations in conditions’ rule

I mean, what’s the point? Let the code flow naturally after the ‘if’ and leave more unlikely situations for ‘else’ (displaying error for instance). In the ‘if’ block, you can put cases that are more natural for your game. A player wants to deal damage to living enemies. It is more common, as dead bodies are usually waiting for being cleaned up and are less attractive for the player. So:

If (!enemy.IsDead())
{
    enemy.TakeDamage();
}
else
{
    enemy.AddForce(FORWARD); // Profane the corpse
}

It increases readability and stresses the expected game flow. If an attacker tells Mr. Wayne “Give me that wallet, or else…,” Mr. Wayne should give the wallet back, move on and doesn’t care about “else.” It’s the standard course of the situation and that solution should be considered first.

Share these goodies:

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
157 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
BartoszYanick Bourbeau Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Yanick Bourbeau
Guest
Yanick Bourbeau

Common dude, just wrote a IsAlive() function 🙂 The problem with negation in the first IF is that coders often misses the “!”. If you really like to do still use this approach, at least wrote == false in the end. the false keyword get highlighted by most of the IDEs.