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
- public class BusinessCase
- {
- //.........
- }
Example
- void AutoComplete(string name)
- {
- // ......
- }
2.3 Use Camel casing for variables and method parameters.
Example
- int totalCount = 0;
- void AutoComplete(string name)
- {
- string fullMessage = "Hello " + name;
- }
2.4 Use Meaningful, descriptive words to name variables. Do not use abbreviations.
Example
- Good:
- string address;
- int salary;
- Bad:
- string nam;
- string addr;
- 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
- for (int i = 0; i < count; i++)
- {
- //...........
- }
2.7 Prefix boolean variables, properties and methods with "is" or similar prefixes.
- 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
- // Format a message and display
- string fullMessage = "Hello my name is " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show( message );
Bad
- // Format a message and display
- string fullMessage = " Hello my name is " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show ( message );
3.4 Use one blank line to separate logical groups of code.
Example
Good
- bool ShowMessage ( string name )
- {
- string fullName = "Hello " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show ( message );
- if ( ... )
- {
- // Write your code here
- // ...
- return false;
- }
- return true;
- }
Bad
- bool ShowMessage (string name)
- {
- string fullName = "Hello " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullName + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show ( message );
- if ( ... )
- {
- // Write your code here
- // ...
- return false;
- }
- return true;
3.6 The curly braces should be on a separate line and not in the same line as if, for etc.
Example
Good
- if ( ... )
- {
- // write your code here
- }
- if ( ... ) {
- // Write your code here }
3.7 Use a single space before and after each operator and brackets.
Example
Good
- if(showResult)
- {
- for ( int i = 0; i < 10; i++ )
- {
- //
- }
- }
Bad
- if(showResult==true)
- {
- for(int i= 0;i<10;i++)
- {
- //
- }
- }
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
- void SaveStudentDetails ( string studentDetails )
- {
- // Save the student details.
- }
Bad
- // This method will save the student details.
- void SaveDetails ( string student )
- {
- // Save the student details.
- }
4.2 A method should only one job at a time. Do not use for more than one job.
Example
Good
- //Save student details
- SaveStudentDetails();
- //Send email to user that student details is added successfully.
- SendEmail();
Bad
- //Save student details
- Public void SaveStudentDetails()
- {
- // First task
- //Save student details
- //Second task
- // Send emails
- }
4.3 Make sure for string comparison convert string to uppercase or lower case for compare.
Example
Good
- If(UserName.ToLower()=="Tom")
- {
- // write yor logic here
- }
Bad
- If(UseName=="TOm")
- {
- //
- }
5. Comments
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
- Enum Mailtype
- {
- Subject,
- Body,
- PlaneText
- }
7.Interface
Always use letter "I" as prefix with name of interface. After letter I, use PascalCase.
Example
- public interface IMultiply
- {
- int Multiplication();
- }
Conclusion:
In this article, I have shown you how to use C# coding standard and naming conventions . Hope you understood and liked it.
Leave Comment