Cleaner Code Using Null Conditional Operator And Coalescing Operator

Introduction

 
In this article, I am going to share a useful tip on writing clean code that I have learned in C#.
 
Example
 
For demo purposes, let's take employees in an organization as an example and create the Employees object which has 3 properties. namely EmployeeID, Name, and Salary.
  1. class Employees  
  2. {  
  3.     public int? EmployeeID { getset; }  
  4.     public string Name { getset; }  
  5.     public long? Salary { getset; }  
  6. }  
Let's consider the use case that we are going to fetch employee details from the database and display or bind these data in front-end.
 
For demo purposes, I am going to initialize our employee's object with some sample employee details.
  1. List<Employees> employee = new List<Employees>()  
  2. {  
  3.     new Employees() { EmployeeID = 1, Name = "John", Salary = 10000 },  
  4.     new Employees() { EmployeeID = 2, Name = "Steve", Salary = 20000 },  
  5.     new Employees() { EmployeeID = 3, Name = "Robert", Salary = 30000 }  
  6. };  
As you can see, I have created a list of employee objects with three employee's details.
 
Okay, now we have our data ready. We will consider looping through each employee and display the data in the console.
  1. foreach (var item in employees)  
  2. {  
  3.     if (item != null)  
  4.     {  
  5.   
  6.         if (item.EmployeeID != null)  
  7.         {  
  8.             Console.WriteLine(item.EmployeeID);  
  9.         }  
  10.         else  
  11.         {  
  12.             Console.WriteLine("No ID Exists");  
  13.         }  
  14.   
  15.         if (string.IsNullOrEmpty(item.Name))  
  16.         {  
  17.             Console.WriteLine(item.Name);  
  18.         }  
  19.         else  
  20.         {  
  21.             Console.WriteLine("No Name Exists");  
  22.         }  
  23.   
  24.         if (item.Salary != null)  
  25.         {  
  26.             Console.WriteLine(item.Salary);  
  27.         }  
  28.         else  
  29.         {  
  30.             Console.WriteLine("No Salary Exists");  
  31.         }  
  32.     }  
  33. }  
Here, I have used a traditional way of implementation. As you can easily guess, what I basically did was just loop through each employee and checked for any null reference for the employee object itself and for its properties.
 
However, if this object has many properties and if you need to have these types of checks for every property then this code becomes more lengthy. Hence this is when the Null Conditional Operator and Null Coalescing Operator comes into the picture. Let me simplify the same logic which was written above using these operators.
  1. foreach (var item in employees)  
  2. {  
  3.     Console.WriteLine(item?.Name ?? "No Name Exists");  
  4.   
  5.     Console.WriteLine(item?.EmployeeID.ToString() ?? "No ID Exists");  
  6.   
  7.     Console.WriteLine(item?.Salary.ToString() ?? "No Salary Exists");  
  8. }  
Isn't this so clear, concise, and yet so easy to implement? Now let me explain what are these operators and how they work.
 
The ? operator is the null conditional operator and this operator lets you access members and elements only when the receiver is not-null, returning null result otherwise. In our case, if the item object is null it returns null if not it will print the properties of the item.
 
The ?? operator is the null coalescing operator and this was designed to be used easily with null-conditional operators. It provides default value when the null conditional operator returns null. Hence in our example, if the item properties are null then the right side of the code going to be printed.
 
That's it. This is how you can easily use these two operators in combination to make your code look simple and clean.
 
If you find this post useful, please help others spreading the word. Please leave your suggestions and feedback.