Clean Code Using Null Conditional Operator and Coalescing Operator
Tags: beginners, dotnet, csharp, codenewbie
Published: 2020-08-06 19:22:25

### **Introduction**
In this article i am going to share one of the useful tips in writing a clean code which i have learnt in C#.

### **Example**
For a demo purpose lets take employees in an organisation as an example and lets create the Employees object which has 3 properties namely EmployeeID, Name and Salary.
```
class Employees
{
    public int? EmployeeID { get; set; }
    public string Name { get; set; }
    public long? Salary { get; set; }
}
```
Lets consider the use case that we are going to fetch an employee details from database and display or bind these data in front-end.

For demo purpose i am going to initialize our employees object with some sample employee details.
```
List<Employees> employee = new List<Employees>()
{
    new Employees() { EmployeeID = 1, Name = "John", Salary = 10000 },
    new Employees() { EmployeeID = 2, Name = "Steve", Salary = 20000 },
    new Employees() { EmployeeID = 3, Name = "Robert", Salary = 30000 }
};
```
As you can see i have created list of employees object with three employees details.

Okay , now we have our data ready. We will consider looping through each employee and display the data in the console.
```
foreach (var item in employees)
{
    if (item != null)
    {

        if (item.EmployeeID != null)
        {
            Console.WriteLine(item.EmployeeID);
        }
        else
        {
            Console.WriteLine("No ID Exists");
        }

        if (string.IsNullOrEmpty(item.Name))
        {
            Console.WriteLine(item.Name);
        }
        else
        {
            Console.WriteLine("No Name Exists");
        }

        if (item.Salary != null)
        {
            Console.WriteLine(item.Salary);
        }
        else
        {
            Console.WriteLine("No Salary Exists");
        }
    }

}
```
here i have used traditional way of implementation. As you can easily guess it what i basically did was just looped through each employee and checked for any null reference for employee object itself and for its properties.

However if this object has many properties and if you need to have these type 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 picture. Let me simplify the same logic which was written above using these operators
```
foreach (var item in employees)
{
    Console.WriteLine(item?.Name ?? "No Name Exists");

    Console.WriteLine(item?.EmployeeID.ToString() ?? "No ID Exists");

    Console.WriteLine(item?.Salary.ToString() ?? "No Salary Exists");
}
```
Isn't this so clear, concise and yet so easy to implement. Now Let me explain what are these operators and how does it 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 items properties.

The `??` operator is the null coalescing operator and this was designed to be used easy 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 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.😊 Drop in your suggestions and feedbacks.