Coding Standards - longle97/GameDesign GitHub Wiki

For the purpose of being consistent with the Microsoft's .Net Framework, having consistency in coding style and being easy to read, these coding standards have been gathered to be used for this project.

Naming

PascalCasing - Both words start with uppercase letters

camelCasing - The first word starts with a lower case and the second word has an uppercase

Use PascalCasing for the following objects:

  • Class name
  • Constructor name
  • Method name
  • Constants name
  • Properties name
  • Delegate name
  • Enum type name

Example: Both class name and method name using PascalCasing

public class PlayerStats
{
  public void CalculateStats()
  {
    //...
  }
}

Use camelCasing for the following objects:

  • Method arguments
  • Local variables
  • Field name

Example: Both method argument and local variable using camelCasing

public class BattleLog
{
  public void Add(LogEvent logEvent)
  {
    int hitCount = logEvent.Hits.Count;
    // ...
  }
}

Things to do

  • Use PascalCasing when abbreviating 3 characters or more (but avoid uncommon abbreviations)

Example:

UIControl uiControl;
CtrlPlayer ctrlPlayer;
  • Align curly brackets vertically for clear visibility

Example:

public class Player
{
  public Score()
  {
      // ...
  }
}
  • Use predefined type names and avoid system type names

Example:

// Correct
string firstName;
int lastIndex;
bool isSaved;
 
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;
  • Use var for local variable declaration except when using primitive types such as int, string, bool, double, etc.

Example:

var players = new Player();
 
// Exceptions
int index = 100;
string playerName;
bool playerDefeated;
  • Use nouns to name a class

Example:

public class Player
{
  //...
}
public class ShipLocation
{
  //...
}
  • At the top of a class, declared all member variables at the top, and have static variables come first

Example:

public class Player
{
  public static string PlayerName;
  public static decimal AccountAge;

  public string Number {get; set;}
  public DateTime DateOpened {get; set;}

  // Constructor
  public Player()
  {
      // ...
  }
}

Things not to do

  • Do not have type indicators in an identifier

Example:

// Do this
int counter;
string player;
 
// Don't do this
int iCounter;
string strPlayer;
  • Do not use all caps for constants

Example:

// Do this
public static const string ShipType = "AirShip";
 
// Don't do this
public static const string SHIPTYPE = "AirShip";
  • Try to avoid abbreviations, except for commonly used abbreviations such as Id, Html, Ftp, etc.

Example:

// Do this
ShipGroup shipGroup;
 
// Avoid doing this
ShipGroup shipGrp;

// Exceptions
PlayerId playerId;
  • Do not use underscores unless prefixing private static variables

Example:

// Do this
public PlayerScore playerScore;
 
// Don't do this
public PlayerScore player_Score;

// Exceptions
private DateTime _registrationDate;

References

  1. ktaranov C# Coding Standards and Naming Conventions
  2. DoFactory C# Coding Standards and Naming Conventions