C# Coding standards

C# Coding standards and Best programming practices

Any programmer who starts programming knows how to write code, but some of them know coding standards and best programming practices. Not everyone of them follows c# coding standards. Today we will try to bring all the coding standards in this article, which are followed by some of known organizations.

Purpose of coding standards

  1. To follow the consistent coding pattern in your project.
  2. Helps the developer to easily read and understand code to resolve issue as soon as based on assumptions and previous experience of developer.
  3. For large projects, it helps to maintain the project for future developments changes.
  4. To have it in readable format with proper spacing in code blocks.
  5. To follow variable and method naming convention in accordance for what they do in logic.
  6. To follow commenting with each logical code blocks.
  7. Helps in uniform problem solving.
  8. Helps in quick code integration and new developer in team.
  9. Saves money due to less man hours.

Benefits of coding standards

Ultimate benefit of coding standard is that it must be readable, understandable, reviewed, maintainable and reusable. These 5 are main characteristics of good coding practice.

C# Coding Standards

1. C# Naming Conventions
  • Must use Pascal case for Class name. Below is the example.
public class ClassName
{
     //Your code here
}
  • Must use Pascal case for method name, below is the example.
void MethodName(string name)
{
     //Your code here
}
  • Must us Camel case for variables and method parameters.
int totalCount = 0;
void MethodName(string parameterName)
{
   string fullMessage = "Hello " + name;
}
  • Use Pascal case for Constructor, Delegates, Enum and Constants.
  • Use Camel case for file names.
  • Must use meaningful and descriptive name for variable declaration, also it should not be abbreviation.
string name, address,  contact;
decimal salary;
int age;
  • Avoid use of single character named variables, considering exception of initialiser variables used in for, while, do while loops.
for (int i = 0; i < count; i++)
{
     //Your code here
}
  • Avoid use underscores symbol for local variable names.
  • Use prefix 'is' for variables, properties and methods with return type boolean.
private bool isValid, isCompleted;
  • Use appropriate prefix for front end/UI elements.You can choose prefix of your choice, below are the examples are just for reference.
Labellbl
Textboxtxt
Buttonbtn
DropdownListddl
Checkboxchk

2. C# Layout Conventions

  • Use TAB for indentation in code, avoid spaces. Also configure tab level of your choice.
  • Add at least on line space between properties and methods declaration.
  • Write only one logical statement per line to avoid the complexity of code.
  • Use parentheses in equation / expression to group them logically.

3. C# Commenting Conventions
Below are the some code commenting guidelines in c#.

  • Must write comments for complex logical code blocks, which helps the other developers.
  • Place comment in separate line, avoid commenting after the code on same line.
  • Begin comment with uppercase, avoid one space after comment delimiter.
  • Single line commenting in C# Code behind, Select the line or text you want to comment and press shortcut Ctrl+K+C. To undo the comment press Ctrl+K+U.
// Single line comment
  • Multi line commenting in C#, Used to comment whole block of code.
/*
Line One
Line Two
*/
  • For XML commenting, follow below mentioned example for reference.
/// <contact>
///      <name></name>
///      <company></company>
///      <phone></phone>
/// </contact>
4. C# Code Improvement Tips

  • Check Null and Empty condition.
Do:
string varName = null;
if (!string.IsNullOrEmpty(varName))
{
     //Your code here
}
Avoid:
string varName = null;
if (varName != null && varName != "")
{
     //Your code here
}

  • Use ternary operator instead of if-else.
Do:
int marks = 70;
string message = ( marks > 35 ? "Pass" : "Fail" );
Avoid:
int marks = 70;
string message = "";
if( marks > 35)
{
     message = "Pass";
}
else
{
     message = "Fail";
}

  • Use null coalescing operator.
Do:
Students obj = new Students();
string varName = obj.Name ?? String.Empty;
Avoid:
Students obj = new Students();
string varName = obj.Name != null ? obj.Name : String.Empty;

  • Use object initialiser.
Do:
Students obj = new Students {
     ID = 1;
     Name = "Test";
}
Avoid:
Students obj = new Students();
obj.ID = 1;
obj.Name = "Test";
  • Use StringBuilder object to append strings, specially when string data is larger.
  • Avoid use of type "var" variables, Create variables with known datatype if you know the datatype of right side of assignment.
  • Use expression method in MVC for empty methods which returns only view.
Do:
public ActionResult StudentList() => View();
Avoid:
public ActionResult StudentList()
{
     return View();
}

Post a Comment

0 Comments