Coding Standard in C#
Anyone can write a code. With a few months of programming
experience, you can write 'working applications'. Making it work is easy, but
doing it the right way requires more work, than just making it work.
Everybody say some different definition for
good code, in my definition is following point for good code.
·
Reliable
·
Maintainable
·
Reusable
Most of the developers writing code for higher
performance, compromising reliability and maintainability. Efficiency and
performance comes below reliability and maintainability. If your code is not
reliable and maintainable, you can spend lot of time to finding the issues and you
can’t give a proper fix.
Any other developer to work your code or fix
any issues he can understand the code what did, use proper commands and signatures,
when a good developer is doing like this comes below of reusability of standards.
Note :
The terms Pascal Casing
and Camel Casing are used throughout this document.
Pascal Casing - First character of all
words are Upper Case and other characters are lower case.
Example: BackColor
Camel Casing - First character of all words,
except the first word are Upper Case and other characters are lower
case.
Example: backColor
|
1.
Use
Pascal casing for Method names
Private void SayHello(string name)
{
...
}
2.
Use
Camel casing for variables and method parameters
int totalCount = 0;
Private void SayHello(string name)
{
string fullMessage
= "Hello " + name;
...
}
3.
Use
the prefix “I” with Camel Casing for interfaces ( Example: IEntity )
4.
Do
not use Hungarian notation to name variables.
In earlier days most of the programmers liked it - having
the data type as a prefix for the variable name and using m_ as prefix for
member variables. Eg:
string m_sName;
int nAge;
However, in .NET coding standards, this is not recommended.
Usage of data type and m_ to represent member variables should not be used. All
variables should use camel casing.
5.
Use
Meaningful, descriptive words to name variables. Do not use abbreviations.
Good:
string address
int salary
Not Good:
string nam
string addr
int sal
6.
Do
not use single character variable names like i, n, s etc. Use names like index, temp
One exception in this case would be variables used for
iterations in loops:
for ( int i = 0; i < count; i++ )
{
...
}
If the variable is used only as a counter for iteration
and is not used anywhere else in the loop, many people still like to use a
single char variable (i) instead of inventing a different suitable name.
7.
Do
not use underscores (_) for local variable names.
8.
All
member variables must be prefixed with underscore (_) so that they can be
identified from other local variables.
9.
Do
not use variable names that resemble keywords.
10.
Comments
should be in the same level as the code (use the same level of indentation).
Good:
// Format a message and
display
11.
The
curly braces should be on a separate line and not in the same line as if, for etc.
Good:
if ( ... )
{
// Do something
}
Not Good:
if ( ... ) {
// Do something
}
12. Use #region
to group related pieces of code together. If you use proper grouping using #region,
the page should like this when all definitions are collapsed
No comments:
Post a Comment