Sunday, May 11, 2008

C# Coding Practices


This document explains the coding styles, naming conventions and programming practices for the .NET C# language. Consistent naming conventions and practices greatly increase predictability and discoverability. This document is based on Microsoft's document on coding standards. This is the coding standards and practices that I use on a day to day basis.

Capitalization Styles Defined


Pascal case

The first letter of an identifier and the first letter of each subsequent concatenated word is capitalized.

Example: ForeColor, BackColor

Camel case

The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.

Example: strValue, intNumberOfDays

Uppercase

All letters in the identifier are capitalized

Example: ID, PI

Hungarian Type Notation


Hungarian notation is a naming convention in which the name of a variable indicates its type and intended use. It is in fact, a commenting technique. Note that using Hungarian notation for the sake of using it will result in obfuscating your code.

Example: strFirstName, bIsMarried

The Big Picture is to be able to tell a story using your class without relying on heavy commenting. For instance, lets focus our attention to the Circle class depicted below.


namespace MyCompany.Shapes
{
public class Circle
{
private int _iRadius;
public const double PI = 3.14159;

public Circle()
{
this._iRadius = -1;
}

public int Radius
{
get
{
return this._iRadius;
}
set
{
this._iRadius = value;
}
}

public Double GetArea()
{
if(this._iRadius < 0)
{
return 0;
}
return PI * Math.Pow(this._iRadius, 2);
}
}
}


Just by looking at the namespace, we are able to determine that the Circle object is classified as a Shape. Notice the access modifer of the class. What does this say about how the developer intended for his/her class to be used? Moving our attention to the constructor, we can see that the Circle object is dependent on a radius of integer type. And we know this, because the developer used Hungarian notation. This means that we will be sure to set this property before calling "GetArea." Spend a few minutes and see what else you can find out about this Object by simply focusing on the naming conventions and styles.


Above demonstrates why using naming conventions in your code is extremely important. It saves time, mandates consistency, allows for quick interfacing, and can even save a corporation millions of dollars.

Primitive Type Notation



sbyte sy
short s
int i
long l
byte y
ushort us
uint ui
ulong ul
float f
double d
decimal dec
bool b
char c


Visual Control Type Notation


Notation was developed by the Microsoft special interest group


Assembly asm
Boolean bln
Button btn
Char ch
CheckBox cbx
ComboBox cmb
Container ctr
DataColumn dcol
DataGrid dgrid
DataGridDateTimePickerColumn dgdtpc
DataGridTableStyle dgts
DataGridTextBoxColumn dgtbc
DataReader dreader
DataRow drow
DataSet dset
DataTable dtable
DateTime date
Dialog dialog
DialogResult dr
Double dbl
Exception ex
GroupBox gbx
HashTable htbl
ImageList iml
Integer int
Label lbl
ListBox lbx
ListView lv
MarshallByRefObject rmt
Mainmenu mm
MenuItem mi
MDI-Frame frame
MDI-Sheet sheet
NumericUpDown nud
Panel pnl
PictureBox pbx
RadioButton rbtn
SDI-Form form
SqlCommand sqlcom
SqlCommandBuilder sqlcomb
SqlConnection sqlcon
SqlDataAdapter sqlda
StatusBar stb
String str
StringBuilder strb
TabControl tabctrl
TabPage tabpage
TextBox tbx
ToolBar tbr
ToolBarButton tbb
Timer tmr
UserControl usr
WindowsPrincipal wpl



C# Coding Practices Continued (2 of 3) >>

0 comments:

 

blogger templates | Make Money Online