Anyone can write code with a few months of programming experience. But only a few developers know what coding standards and naming conventions are, but not everyone follows best practices.
Introduction
In this article, we will learn what the coding standards and naming conventions are in C#. Firstly, you might wonder:
What is the definition of good code?
Answer
According to me, when the code is more understandable, readable, maintainable, reviewed and reused are the main characteristics of good code.
Review Code
This is the major point of code review. Inside it, review the code to make sure that the code follows the coding standard.
Naming Conventions
Pascal Case
The first characters of all words are Upper Case and other characters are lower case.
Example
CaseHistory
Camel Case
The first letter is in lowercase and the first letter of every subsequent concatenated word is in caps.
OR
The first character of all words, except the first word, is Upper Case and other characters are lower case.
Example
businessCase
Naming Conventions and Standards
Use Pascal casing for Class names.
Example
- public class BusinessCase
- {
-
- }
Use Pascal casing for Method names.
Example
- void AutoComplete(string name)
- {
-
- }
Use Camel casing for variables and method parameters.
Example
- int totalCount = 0;
- void AutoComplete(string name)
- {
- string fullMessage = "Hello " + name;
- }
Use Meaningful, descriptive words to name variables. Do not use abbreviations.
Example
- Good:
- string address;
- int salary;
- Bad:
- string nam;
- string addr;
- int sal;
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++)
- {
-
- }
Do not use underscores (_) for local variable names.
Prefix boolean variables, properties, and methods with "is" or similar prefixes.
- Ex: private bool _isFinished
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 elements. A brief list is given below.
Example
Control |
Prefix |
Label |
lbl |
TextBox |
txt |
DataGrid |
dtg |
Button |
btn |
ImageButton |
imb |
Use Pascal Case for file names.
Indentation and Spacing
Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.
Comments should be in the same level as the code (use the same level of indentation).
Example
Good
-
- string fullMessage = "Hello my name is " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show( message );
Bad
-
- string fullMessage = " Hello my name is " + name;
- DateTime currentTime = DateTime.Now;
- string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();
- MessageBox.Show ( message );
Curly braces ( {} ) should be in the same level as the code outside the braces.
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 (...) {
-
-
- 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 (...) {
-
-
- return false;
- }
- return true;
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.
Example
Good
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++)
- {
-
- }
- }
Superb Programming Practices
Method name should clarify the meaning. Do not use misleading names. If the method name is clear then there is no need of documentation.
Example
Good
- void SaveStudentDetails (string studentDetails )
- {
-
- }
Bad
-
- void SaveDetails (string student )
- {
-
- }
A method should do only one job at a time. Do not use it for more than one job.
Example
Good
-
- SaveStudentDetails();
-
- SendEmail();
Bad
-
- Public void SaveStudentDetails()
- {
-
-
-
-
- }
Make sure string comparisons convert string to uppercase or lowercase for comparison.
Example
Good
- If(UserName.ToLower()=="tom")
- {
-
- }
Bad
Comments
- Do not write comments for every line of code and every variable declared.
- Use // or /// for comments. Avoid using /* … */
- Write comments where is it required. But they should be good readable and limited. If all variables and method names are perfectly meaningful, then there is no need of many comments.
- If you initialize a numeric variable to a special number other than 0, -1 etc, document the reason for choosing that value.
- Make sure to perform spelling check on comments and make sure proper grammar and punctuation is used.
Enum
Always use a singular noun for defining enum.
Example
- Enum Mailtype
- {
- Subject,
- Body,
- PlaneText
- }
Interface
Always use the letter "I" as a prefix with the name of an 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 standards and naming conventions. I hope you understood and liked it.