C# Coding Standards

In this article, we’ll learn how we can optimize lines of code, which we should follow to write better, more clear code

Introduction

In this article, we’ll learn how we can optimize lines of codes, which we should follow to write better, more clear code.

As per my thinking, there are some golden rules to achieve good coding standards.

  1. Naming conventions
  2. Optimizing syntax
Now, we are going to understand each above point in detail.

 

Naming Conventions

How we should declare variables.

Note –  We should always use camel case while declaring variables, like var itemList = new List<T>();

To declare a variable which returns a single entity/object.

var item = new Item();

To declare a variable that returns multiple entity/objects means to add “s” or “List” suffix, so we can easily identify that it will return a list of classes/objects.

var items = new List<Item>();   
//or  
var itemList = new List<Item>();

To declare a private variable, we use “_”.

private int _value = 10;

Naming conventions table

 Name Case
Variables camelCase
Class PascalCase
Constructor PascalCase
Properties PascalCase
Delegate PascalCase
Enum PascalCase
Arguments in methods camelCase
Method PascalCase
Constants PascalCase
Field camelCase

Optimize syntax

To declare an empty method that only returns a view in MVC, we should use the expression body. 
//Avoid  
public ActionResult Dashboard()  
{  
    return View();  
}  
  
//Do  
public ActionResult Dashboard() => View();

To check null or empty condition. 

//Avoid  
var varName = "faisal";  
if (varName != null && varName != "")  
{  
   //code  
}  
  
//Do  
var varName = "faisal";  
if (!string.IsNullOrEmpty(varName))  
{  
    //code  
}

Use null coalescing expression,

Test test = new Test();  
  
//Avoid  
var varName = test.Name != null ? test.Name : "";  
  
//Do  
var varName = test.Name ?? "";

Use object initializer,

//Avoid  
Test test = new Test();  
test.Id = 1;  
test.Name = "faisal";  
  
//Do  
var test = new Test  
{  
   Id = 1,  
   Name = "faisal"  
};

Use ?. operator,

//Avoid  
var empName = "";  
Session["Name"] = "Faisal Pathan";  
if (Session["Name"] != null)  
{  
   empName = Session["Name"].ToString();  
}  
else  
{  
     empName = "";  
}  
  
//Do  
var empName = "";  
Session["Name"] = "Faisal Pathan";  
empName = Session["Name"]?.ToString() ?? "";
Avoid extra braces,
Note – only work with single line statements.
var count = 10;  
  
//Avoid  
 if (count > 0)  
{  
   //code  
   count++;  
}  
  
  
//Do  
 if (count > 0) count++; //code  
  
  
//Avoid  
for (int i = 0; i < count; i++)  
{  
   //code  
   count += 10;  
}  
  
//Do  
for (int i = 0; i < count; i++) count += 10;  
  
  
var testList = new List<Test>();  
var names = new ArrayList();  
  
//Avoid  
foreach (var item in testList)  
{  
   names.Add(item.Name);  
}  
  
//Do  
foreach (var item in testList) names.Add(item.Name);

Use string interpolation. 

 Test test = new Test();  
  
//Avoid  
 var details = string.Format("{0}, you are welcome, Your Id is {1}", test.Name , test.Id + "_emp");  
  
//Do  
var details = $"{test.Name}, you are welcome, Your Id is {test.Id}_emp";

New lightweight switch-case with c# 8, 

int itemSwitch = 1;  
  
//Good  
switch (itemSwitch)  
{  
 case 1:  
 Console.WriteLine("Item 1");  
 break;  
 case 2:  
 Console.WriteLine("Item 2");  
 break;
 case 3: 
 Console.WriteLine("Item 3"); 
 break;
 default:  
 Console.WriteLine("Default item case");  
 break;  
}  
  
//better  
 var message = itemSwitch switch   
            {  
                1 =>  Console.WriteLine("Item 1"),  
                2 =>  Console.WriteLine("Item 2"),  
                3 =>  Console.WriteLine("Item 3") ,
                _ => "Default item case" 
            }; 
 Console.WriteLine(message);

Please give your valuable feedback/comments/questions about this article below. Please let me know how you like and understand this article and how I could improve it.

1 Comment

  1. Nishant Kakadiya

    Really helpful for me,
    it’s really helpful to improve my coding structure.
    can you please send more posts about c# coding standards.

    Thanks.

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories