OOP Basic – Class design

(1) In class design, always declare member variables private unless required. You can provide access to it from the outside of the class using a public method.

public class Company
{
  // properties
  private string name;
  private int employeeNumber; // Should be privated
  // Method
  public AddNewEmployee()
  {
      employeeNumber++;
      // Do something
  }
}

The primary benefits are :
– It prevents changing the values of the variables from outside accidentally.
– It also helps maintain a program easily.
– It follow encapsulation of OOP design.

(2) In class design, consider chain of constructor

public class Company
{
  //Constructor
  public Company():this("DefaultName",0)
  {
  }
  public Company(string name, int employeeNumber)
  {
    // Init value
  }
  // properties
  private string name;
  private int employeeNumber; // Should be privated
  // Method
  public AddNewEmployee()
  {
      employeeNumber++;
      // Do something
  }
}

(3) In class design, method name, return type and method behavior should consistency.

public class Company
{
  //Constructor
  public Company():this("DefaultName",0)
  {
  }
  public Company(string name, int employeeNumber)
  {
    // Init value
  }
  // properties
  private string name;
  private int employeeNumber; // Should be privated
  // Method
  public AddNewEmployee()
  {
      employeeNumber++;
      // Do something
  }
  public bool GetCountAllEmployees(string name)  // Wrong ..... shit method
  {
     
  }
}

(4) In class design, use the following access modifiers to specify the accessibility of a type or member when you declare it (Detail here)

Share
%d bloggers like this: