Loading, please wait...

A to Z Full Forms and Acronyms

C# Coding Standard and Naming Convention

Feb 18, 2018 CSharp, 9166 Views
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 does not follow the coding standard.

Introduction

In this article, we will learn what is the coding standard and naming conventions in C#.Firstly arise a question in developers mind that, What is the definition of good code?

Ans: According to me, When the 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 follows the coding standard or not.

Naming Conventions:

  1. Pascal Case
  2. CamelCase

Pascal Case: First character of all words are Upper Case and other characters are lower case.

Example: CaseHistory

CamelCase: 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: business case

  1. Naming Conventions and Standards

2.1 Use Pascal casing for Class names.

Example:

public class BusinessCase
{

   //.........

}

2.2 Use Pascal casing for Method names.

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;

string name;

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.6 Do not use underscores (_) for local variable names.

2.7 Prefix boolean variables, properties, and methods with "is" or similar prefixes.

Example:

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

Button

btn

GridView

grd

Image

img

ImageButton

imb

2.9 Use Pascal Case for file names.

  1. Indentation and Spacing:

3.1 Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.

3.2 Comments should be at 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.3 Curly braces ( {} ) should be at the same level as the code outside the braces.

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.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:

if ( ... )
{

// write your code here

}

Bad:

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++ )

  {

    // write your logic here

  }

}

Bad:

4.Superb Programming Practices

4.1 Method name should be meaning full means what they do. Do not use misleading names. If the method name meaning full then, there is no need for 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

 SendEmail();

}

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")
{

        //write your logic here

}
  1. 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 fewer comments. If all variables and method names are perfectly 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.

  1. Enum

Always use a singular noun for define enum.

Example

Enum Mailtype
{

        Subject,

        Body,

        PlaneText
}
  1. 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 standard and naming conventions. Hope you understood and liked it.

A to Z Full Forms and Acronyms