Saravanan
1. Introduction
Programming style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors
Everyone may have different definitions for the term ‘good code’. In the following are the characteristics of good code.
• Reliable
• Maintainable
• Consistency
• Efficient
If your code is not reliable and maintainable, you will be spending lot of time to identify issue, trying to understand code etc throughout the life of your application.
2. Naming conventions
• Always use Camel Case or Pascal Case names.
Camel Case
A word with the first letter lowercase, and the first letter of each subsequent word-part capitalized.
Example: customerName
1. Use Camel casing for method
int totalSum = 0;
void SayHeading()
{
…
}
2. Camel Case for Variable (inline)
Avoid using single characters like “x” or “y” except in FOR loops.
Avoid enumerating variable names like text1, text2, text3 etc.
Parameter
3 Camel Case for Parameter
Example:
public void Execute(string commandText, int iterations)
4. Field (Private) Camel Case and prefix with a single underscore (_) character.
Example:
private string _name;
Pascal Case
A word with the first letter capitalized, and the first letter of each subsequent word-part capitalized.
Example: CustomerName
1. Use Pascal casing for Project File
Myproject.Web
2. Use Pascal casing for Class names
public class HelloWorld
{
…
}
3. Use Pascal casing for Method names
void SayHello(string name)
{
…
}
4. Use Pascal Case for Property
Property name should represent the entity it returns. Never prefix property names with
“Get” or “Set”.
Example:
public string Name
{
get{…}
set{…}
}
5. Use Pascal Case for Field (Public, Protected,or Internal)
6. Use Pascal Case for Enum (both the Type and the Options).
7. Use Pascal Case for File names
• Avoid ALL CAPS and all lowercase names. Single lowercase words or letters are acceptable.
• Do not create declarations of the same type (namespace, class, method, property, field, or parameter) and access modifier (protected, public, private, internal) that vary only by capitalization.
• Do not use names that begin with a numeric character.
• Do add numeric suffixes to identifier names.
• Always choose meaningful and specific names.
• Always err on the side of verbosity not terseness.
• Variables and Properties should describe an entity not the type or size.
• Do not use Hungarian Notation!
Example: strCompanyId or iIndex
• Avoid using abbreviations unless the full name is excessive.
• Avoid abbreviations longer than 5 characters.
• Any Abbreviations must be widely known and accepted.
• Use uppercase for two-letter abbreviations, and Pascal Case for longer abbreviations.
• Do not use reserved words as names.
• 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 index = 0; index < count; index++ )
{
...
}
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 (index) instead of inventing a different suitable name.
• Do not use underscores (_) for local variable names.
• All member variables must be prefixed with underscore (_) so that they can be identified from other local variables.
• Prefix boolean variables, properties and methods with “is” or similar prefixes.
• File name should match with class name.
3. Indentation and Spacing
For example, for the class HelloWorld, the file name should be helloworld.cs (or, helloworld.vb)
There should be one and only one single blank line between each method inside the class.
The curly braces should be on a separate line and not in the same line as if, for etc.
Good:
if ( … )
{
// Code
}
Bad:
if ( … ) {
// Code
}
Use a single space before and after each operator and brackets.
Good:
if ( i>0 )
{
for ( int index = 0; index < 4; index++ )
{
//Code
}
}
Bad:
if(i>0)
{
for(int index = 0; index <10; index ++)
{
//Code
}
}
4. Formatting
• Group internal class implementation by type in the following order:
a. Member variables.
b. Constructors & Finalizes.
c. Nested Enums, Structs, and Classes.
d. Properties
e. Methods
• Sequence declarations within type groups based upon access modifier and visibility:
a. Public
b. Protected
c. Internal
d. Private
• Never declare more than 1 namespace per file.
• Avoid putting multiple classes in a single file.
• Always use a Tab & Indention size of 4. Do not use SPACES
• Declare each variable independently – not in the same statement.
• Comments should be in the same level as the code (use the same level of indentation).
5. Exceptions
• Do not use try/catch blocks for flow-control.
• Only catch exceptions that you can handle.
• Never declare an empty catch block.
• Always use validation to avoid exceptions.
Example:
// Bad!
try
{
conn.Close();
}
Catch(Exception ex)
{
// handle exception if already closed!
}
// Good!
if(conn.State != ConnectionState.Closed)
{
conn.Close();
}
• When you re throw an exception, use the throw statement without specifying the original exception. This way, the original call stack is preserved.
Good:
catch
{
// do whatever you want to handle the exception
throw;
}
Bad :
catch (Exception ex)
{
// do whatever you want to handle the exception
throw ex;
}
• Do not write very large try-catch blocks. If required, write separate try-catch for each task you perform and enclose only the specific piece of code inside the try-catch. This will help you find which piece of code generated the exception and you can give specific error message to the user.
6. Architecture
• Always Use multi-tier architecture (often referred to as N-tier architecture)
• N-tier application architecture provides a model for developers to create a flexible and reusable application. By breaking up an application into tiers, developers only have to modify or add a specific layer, rather than have to rewrite the entire application over.
• There should be a presentation tier, a business or data access tier, and a data tier.
Dream Dare Win
www.jeywin.com
******