Skip to main content

Practice

Class Composition Sequence

Each section below must follow this sequence in terms of access modifiers.

# Access Modifiers
1 public
2 protected
3 internal
4 private

Every class should follow this sequence of declaration and/or implementation.

# Type Sample
1 Class Attributes [RequireComponent(typeof(MainCharacter)]
2 Class Declartion public class MainCharacterInputHandler : MonoBehaviour { }
3 Constant Variables public const int MAX_POWER = 10;
4 Static Properties public static int CurrentPower { private set; get; } = 20;
5 Static Variables public static int CurrentPower { private set; get; } = 20;

Example

[RequireComponent(typeof(CarEngine)]
public class Car : MonoBehaviour {
  public const string ITEM_SKU = "car";

  public static string ItemLocalizedSku => ConvertToCurrentLanguage(ITEM_SKU);
  private static 
}

Unused Using Directives

Always delete any using directive that is not being utilized.

Example

using UnityEngine;
using UnityEngine.UI;    // Delete this line if you are not referring to any UI elements.

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
}