Anyone can write code with a few months of programming experience.But some developers know what is the coding standard and naming convention but everyone not follow the coding standard.                 

1. Introduction

In this article we will learn what is the coding standard and naming conventions in C#.Firstly arrise a question in developers mind that, 
What is the definition of good code ?
Ans: According to me, When code is more understandable , readable, maintainable , review and reusing code this is the main characteristics of good code.
 
Review Code: This is the major point of code review. Inside it, review the code make sure that the code follow the coding standard or not.
 
Naming Conventions:
 
1. Pascal Case
2. Camel Case
 
Pascal Case: First character of all words are Upper Case and other characters are lower case.
Example: CaseHistory
 
Camel Case: First letter is in lowercase and first letter of every subsequent concatenated word is in caps.
OR
First character of all words, except the first word are Upper Case and other characters are lower case. 

Example: businessCase

2. Naming Conventions and Standards
 
2.1 Use Pascal casing for Class names. 
 
Example
  1. public class BusinessCase  
  2. {  
  3.    //.........  
  4. }  
 
2.2  Use Pascal casing for Method names.
 
Example
  1. void AutoComplete(string name)  
  2. {  
  3.    // ......  
  4. }  
 
2.3 Use Camel casing for variables and method parameters.
Example
  1. int totalCount = 0;  
  2. void AutoComplete(string name)  
  3. {  
  4.     string fullMessage = "Hello " + name;  
  5. }  
 
2.4 Use Meaningful, descriptive words to name variables. Do not use abbreviations.
 
Example
  1. Good:
  2. string address;
  3. int salary;
  4. Bad:
  5. string nam;
  6. string addr;
  7. int sal;
2.4 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.
 
Example
  1. for (int i = 0; i < count; i++)
  2. {
  3. //...........
  4. }
2.6 Do not use underscores (_) for local variable names.
 
2.7 Prefix boolean variables, properties and methods with "is" or similar prefixes.
  1. Ex: private bool _isFinished

2.8 Use appropriate prefix for the UI elements so that you can identify them from the rest of the variables.

Use appropriate prefix for each of the ui element. A brief list is given below.
 
Example
 

Control

Prefix

Label

lbl

TextBox

txt

DataGrid

dtg

Button

btn

ImageButton

imb

 
2.9 Use Pascal Case for file names.

3. Indentation and Spacing:

3.1 Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.
 
3.2 Comments should be in the same level as the code (use the same level of indentation).
 
Example

Good
  1. // Format a message and display
  2. string fullMessage = "Hello my name is " + name;
  3. DateTime currentTime = DateTime.Now;
  4. string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
  5. MessageBox.Show( message );
Bad
  1.               // Format a message and display
  2. string fullMessage = " Hello my name is " + name;
  3. DateTime currentTime = DateTime.Now;
  4. string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
  5. MessageBox.Show ( message );
3.3 Curly braces ( {} ) should be in the same level as the code outside the braces.
 
3.4 Use one blank line to separate logical groups of code.
Example 
Good
  1. bool ShowMessage ( string name )
  2. {
  3. string fullName = "Hello " + name;
  4. DateTime currentTime = DateTime.Now;
  5. string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
  6. MessageBox.Show ( message );
  7. if ( ... )
  8. {
  9. // Write your code here
  10. // ...
  11. return false;
  12. }
  13. return true;
  14. }
Bad
  1. bool ShowMessage (string name)
  2. {
  3. string fullName = "Hello " + name;
  4. DateTime currentTime = DateTime.Now;
  5. string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
  6. MessageBox.Show ( message );
  7. if ( ... )
  8. {
  9. // Write your code here
  10. // ...
  11. return false;
  12. }
  13. return true;
3.5 There should be one and only one single blank line between each method inside the class.
 
3.6 The curly braces should be on a separate line and not in the same line as if, for etc.
Example
 
Good
  1. if ( ... )
  2. {
  3. // write your code here
  4. }
Bad
  1. if ( ... ) {
  2. // Write your code here }
3.7 Use a single space before and after each operator and brackets.
Example 
Good
  1. if(showResult)
  2. {
  3. for ( int i = 0; i < 10; i++ )
  4. {
  5. //
  6. }
  7. }
Bad
  1. if(showResult==true)
  2. {
  3. for(int i= 0;i<10;i++)
  4. {
  5. //
  6. }
  7. }
4. Superb Programming Practices
 
4.1 Method name should meaning full means what they do. Do not use mis-leading names. If the method name meaning full then, there is no need of documentation.
Example
 
Good
  1. void SaveStudentDetails ( string studentDetails )
  2. {
  3. // Save the student details.
  4. }
Bad
  1. // This method will save the student details.
  2. void SaveDetails ( string student )
  3. {
  4. // Save the student details.
  5. }
4.2 A method should only one job at a time. Do not use for more than one job.
Example
Good 
  1. //Save student details
  2. SaveStudentDetails();
  3. //Send email to user that student details is added successfully.
  4. SendEmail();
Bad
  1. //Save student details
  2. Public void SaveStudentDetails()
  3. {
  4. // First task
  5. //Save student details
  6. //Second task
  7. // Send emails
  8. }
4.3 Make sure for string comparison convert string to uppercase or lower case for compare.
Example 
Good

  1. If(UserName.ToLower()=="Tom")
  2. {
  3. // write yor logic here
  4. }
Bad
  1. If(UseName=="TOm")
  2. {
  3. //
  4. }
5. Comments
 
5.1 Do not write comments for every line of code and every variable declared.
 
5.2 Use // or /// for comments. Avoid using /* … */
 
5.3 Write comments where is it required. But good readable and very less comments. If all variables and method names are perfect meaningful, then there is no need many comments.
 
5.4 If you initialize a numeric variable to a special number other than 0, -1 etc, document the reason for choosing that value.

5.5 Make sure perform spelling check on comments and make sure proper grammar and punctuation is used.
 
6.Enum

 Always use singular noun for define enum. 

Example 
  1. Enum Mailtype
  2. {
  3. Subject,
  4. Body,
  5. PlaneText
  6. }
7.Interface 
Always use letter "I" as prefix with name of interface. After letter I, use PascalCase.
 
Example
  1. public interface IMultiply    
  2. {       
  3.    int Multiplication();    
  4. }  
  
Conclusion:
 
In this article, I have shown you how to use C# coding standard and naming conventions . Hope you understood and liked it. 
 
 
 
  Modified On Nov-29-2017 12:32:13 PM

Leave Comment