net
This document was downloaded from http://www.dotnetdaily.net/ You are permitted to use and
distribute this document for any noncommercial purpose as long as you retain this license & copyrights
information. This document is provided on As-Is basis. The author of this document will not be
responsible for any kind of loss for you due to any inaccurate information provided in this document.
Contents
1.
2.
3.
General Rules........................................................................................................................... 2
1.2
General Rules........................................................................................................................... 5
2.2
Grouping Code......................................................................................................................... 6
4.
5.
General Rules........................................................................................................................... 7
General Rules........................................................................................................................... 9
General Rules......................................................................................................................... 10
http://dotnetdaily.net
http://dotnetdaily.net
http://dotnetdaily.net
http://dotnetdaily.net
Use TAB for indentation. Do not use spaces. Recommended TAB size is 4;
Never declare more than one namespace per file;
Avoid putting multiple classes in a single file;
Comments should be in the same level as the code:
Ex:
2.1.5 Always use curly braces ({ and }) in a new line and in conditional
statements:
Ex:
protected void Page_Load(object sender, EventArgs e)
{
bool IsTrue;
if (IsTrue == true)
{
// Do something;
//....
return false;
}
}
http://dotnetdaily.net
MessageBox.Show(message);
}
http://dotnetdaily.net
2.2.4 Do not hardcode numbers and strings. Use constant variables or resource
files;
2.2.5 Declare readonly or static readonly variables instead of constants for
complex types;
2.2.6 Avoid having very large files. If a file has more than 1000 lines of code
then its a good candidate for refactoring. Split it logically in two or more
classes;
3. Language Usage
In .NET, if you dont respect some basic rules about language usage you can end up by
spending a lot of time on debugging code and trace bugs caused by inadvertently written
code.
Bad:
void SaveCustomerName(string customerName)
{
//...
}
http://dotnetdaily.net
{
// do something...
}
Bad:
if (name == "")
{
// do something...
}
3.1.5 Try to use int for any non-fractional numeric values that will fit into int
data type, even for negative numeric values;
3.1.6 Only use long for variables potentially containing values too large for
int data type;
3.1.7 Use double for fractional numbers to insure decimal precisions in
calcultations;
3.1.8 Use float for fractional number that will not fit in decimal or double.
Still avoid using float unless you fully understand the implications
upon any calculations;
3.1.9 Use String.Builder instead of string when you have to manipulate
string objects in a loop;
3.1.10 Try to use @ prefix for string literals instead of escaped strings;
3.1.11 Avoid using ternary conditional operator. For complex operations can
be difficult to understand:
Ex:
int result = isValid ? 9 : 4;
http://dotnetdaily.net
Bad:
if (isValid == false)
{
//do something
}
4. Code Commenting
Adding comments to your work is without doubt one of the most important factor in
team working. More than that, it also represents the best technique for keeping the
code maintainable in time. Have a look at rules below:
10
Ex:
/// <summary>
/// Method used for saving customer's name
/// </summary>
/// <param name="name"></param>
public void SaveCustomerName(string name)
5. Exception Handling
As programmers we always want to write code of the highest quality. Unfortunate we are
not perfect so exceptions appear as a side effect of our code. Knowing that, we must not
punish the user of our product to see our mistakes. Please take a look at the following basic
rules about exception handling.
Bad:
if (status.IsCompleted == true)
{
//some code here
}
http://dotnetdaily.net