Writing Classes - LopulBodon/CS_CodingStandards GitHub Wiki

Class Method and Member Order

Order in a class is from members to methods. Accessibility level of members and methods is from public to private. Members are located on the top of the class and the methods are located after the members. Constant members are at the very top and enums are right after the constant members.

Details of the ordering can be seen below:

class MyClass
{
     // public constants
     // protected constants
     // private constants
     // public enums
     // protected enums
     // private enums
     // public members
     // protected members
     // private members
     // setters and getters
     // abstract methods
     // constructors
     // public methods
     // protected methods
     // private methods
}

If it is a Unity project, then core Unity methods, such as Awake, Start, Update (or Löpül Bodon versions of these methods, such as LB_Awake), and so on, are added right after the constructors -if there are any- in Unity's call order. Then, the order would be:

class MyClass
{
     // public constants
     // protected constants
     // private constants
     // public enums
     // protected enums
     // private enums
     // public members
     // protected members
     // private members
     // setters and getters
     // abstract methods
     // constructors
     // Awake method
     // Start method
     // Fixed update method
     // Update method
     // Late update method
     // OnDestroy method
     // public methods
     // protected methods
     // private methods
}

One exception for this ordering rule is that the developer can group public, protected, and private methods that are working together, such as a public method that calls some private and/or protected methods to perform its task.

class MyClass
{
     public void MyPublicMethod()
     {
          MyPrivateMethod();
          MyProtectedMethod();
     }

     private void MyPrivateMethod() { }
     protected void MyProtectedMethod() { }
     
     public void SomeOtherMethod() { }
}

The last rule in this topic is about ordering the members with the same accessibility level. These kinds of members should be ordered from basic to complex one. Let us assume we have various of members from bool to DerivedWithSomeComposites that are all private, then the order should be as below.

class MyClass
{
     private bool
     private byte
     private short
     private unsigned int
     private int
     private unsigned long int
     private long int
     private float
     private double
     private string
     private Base
     private Derived
     private BaseWithSomeComposites
     private DerivedWithSomeComposites
}

Writing Styles

Accessibility levels (public, protected, private) should be always specified.

Constants are written in all capitals. If it is not a single word, it is divided with underbar (_).

Enums start with e prefix followed by the enum name, i.e., eCityCommons, eDays, eSpecialDuties.

All initializations are handled in the constructor (except constants obviously). If it is a Unity project and there is no constructor, then the parts that do not depend on other components are initialized in Awake (or LB_Awake) method, and the parts depending on other components are initialized in Start (or LB_Start) method.

Other writing styles are adopted from .NET framework design guidelines. Private and protected members are lowercase and camel case. Public members are uppercase for the first character and camel case. Methods and properties are uppercase for the first character and camel case regardless their accessibility level. Parameters also adopt .NET framework design guidelines.

class MyClass : SomeBase
{
     private int MY_CONST_INT = -1;
     public int MyWrongInt;
     protected float myHeritage;
     private bool myFlag;
     private OtherClass otherClass;

     public MyClass() 
          : base()
     {
          MyWrongInt = MY_CONST_INT;
          myHeritage = 0.0f;
          myFlag = false;
          otherClass = new OtherClass();
     }

     public bool MyMethod(int someInt)
     {
          if(MyWrongInt == someInt)
          {
               return true;
          }
     }
}