Monday, July 13, 2009

Naming Conventions C#

Variable name :

  • Variable type should have prefix. ( Example b = boolean , str = String , i = integer , o = class object.
  • First letter of the word should be capitalized
  • Variable should have //comments like below
Example :
int iCounterForArray; // counter for array
string strNameOfEmployee; // string identifies name of the employee. Scope is limited locally
bool bAlive; // boolean tells whether process is still alive. scope is global
MyDrawingClass oDrawingClass; // local object for drawing class


Function name :

  • First Letter should be small
  • First letter of the following words should be capitalized
  • Should have meaningful name
  • Do not use the short names, instead use fullwords and long names
  • Should have comments before the function signature explains what the function does and what the argumens should have.
  • Comments should describe return values too.
Example :

/*
* This function compares the process names.
* aStrProcessName : string value of process name to be compared.
* return : true if same else false
*/
bool isProcessAlive( string aStrProcessName )
{
bool bAlive = false;
/* do string comparison */
if( aStrProcessName == strProcessName)
{
bAlive = true;;
}
return bAlive;
}


Class Name :

  • First letter Should be small case
  • All the first letter of the following words should be capitalized
  • All the member variables and functions should have comments.
  • Must have destructor and constructor
Example :

/*
* Main drawing class inherited from base drawing class
* This will draw customized drawing functionalities for
* circle and rectanlges.
*/
class MyDrawingClass : public MyDrawingBaseClass
{
/* Boolean identifies the current state */
private bool bState;
/* Getter function for state */
public bool getState();
}