Skip to main content

Practice

Less Nested Scopes

Avoid deeply nested scopes by using return's, break's, etc.

Example

Instead of
private bool DeliverDamage() {
  if (_isAlive) {
    if (_power > 0) {
      // Do damage
      return true;
    }
    else {
      // Can't do damage
      return false;
    }
  }
  return false;
}
Do this
private bool DeliverDamage() {
  if (!_isAlive || _power <= 0) {
    return false;
  }
    
  // Do damage
  return true;
}

Positive Booleans

Use positive meaning for booleans to avoid double negative logics. It's easier to read a code that says "If something is valid" instead of "If something is NOT invalid.".

Example

Instead of
bool isNotValid = false;
if (!isNotValid) {
  // Do valid things
}
Do this
bool isValid = true;
if (isValid) {
  // Do valid things
}