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.
  1. var item new Item();  

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

  1. var items = new List<Item>();   
  2. //or  
  3. var itemList = new List<Item>();   

To declare a private variable, we use "_".

  1. 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 which only returns a view in MVC, we should use the expression body. 
  1. //Avoid  
  2. public ActionResult Dashboard()  
  3. {  
  4.     return View();  
  5. }  
  6.   
  7. //Do  
  8. public ActionResult Dashboard() => View();  
To check null or empty condition. 
  1. //Avoid  
  2. var varName = "faisal";  
  3. if (varName != null && varName != "")  
  4. {  
  5.    //code  
  6. }  
  7.   
  8. //Do  
  9. var varName = "faisal";  
  10. if (!string.IsNullOrEmpty(varName))  
  11. {  
  12.     //code  
  13. }  
Use null coalescing expression,
  1. Test test = new Test();  
  2.   
  3. //Avoid  
  4. var varName = test.Name != null ? test.Name : "";  
  5.   
  6. //Do  
  7. var varName = test.Name ?? "";  
Use object initializer,
  1. //Avoid  
  2. Test test = new Test();  
  3. test.Id = 1;  
  4. test.Name = "faisal";  
  5.   
  6. //Do  
  7. var test = new Test  
  8. {  
  9.    Id = 1,  
  10.    Name = "faisal"  
  11. };  
Use ?. operator,
  1. //Avoid  
  2. var empName = "";  
  3. Session["Name"] = "Faisal Pathan";  
  4. if (Session["Name"] != null)  
  5. {  
  6.    empName = Session["Name"].ToString();  
  7. }  
  8. else  
  9. {  
  10.      empName = "";  
  11. }  
  12.   
  13. //Do  
  14. var empName = "";  
  15. Session["Name"] = "Faisal Pathan";  
  16. empName = Session["Name"]?.ToString() ?? "";  
Avoid extra braces,
   
Note - only work with single line statements.
  1.  var count = 10;  
  2.   
  3. //Avoid  
  4.  if (count > 0)  
  5. {  
  6.    //code  
  7.    count++;  
  8. }  
  9.   
  10.   
  11. //Do  
  12.  if (count > 0) count++; //code  
  13.   
  14.   
  15. //Avoid  
  16. for (int i = 0; i < count; i++)  
  17. {  
  18.    //code  
  19.    count += 10;  
  20. }  
  21.   
  22. //Do  
  23. for (int i = 0; i < count; i++) count += 10;  
  24.   
  25.   
  26. var testList = new List<Test>();  
  27. var names = new ArrayList();  
  28.   
  29. //Avoid  
  30. foreach (var item in testList)  
  31. {  
  32.    names.Add(item.Name);  
  33. }  
  34.   
  35. //Do  
  36. foreach (var item in testList) names.Add(item.Name);  
Use string interpolation. 
  1.  Test test = new Test();  
  2.   
  3. //Avoid  
  4.  var details = string.Format("{0}, you are welcome, Your Id is {1}", test.Name , test.Id + "_emp");  
  5.   
  6. //Do  
  7. var details = $"{test.Name}, you are welcome, Your Id is {test.Id}_emp";  
New lightweight switchcase with c# 8, 
  1. int itemSwitch = 1;  
  2.   
  3. //Good  
  4. switch (itemSwitch)  
  5. {  
  6.  case 1:  
  7.  Console.WriteLine("Item 1");  
  8.  break;  
  9.  case 2:  
  10.  Console.WriteLine("Item 2");  
  11.  break;  
  12.  default:  
  13.  Console.WriteLine("Item case");  
  14.  break;  
  15. }  
  16.   
  17. //better  
  18.  var message = itemSwitch switch   
  19.             {  
  20.                 1 =>  Console.WriteLine("Item 1"),  
  21.                 2 =>  Console.WriteLine("Item 2"),  
  22.                 2 =>  Console.WriteLine("Item 3")  
  23.             };  

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.