Introduction

Writing cleaner code is a constant cycle of improvement for developers.
New innovations and technologies get released that help us make more readable, versatile, and clean code.
Cleaner code can be beneficial for all parties. It can help developers understand code, speed up the workflow for faster delivery of solutions for clients and reduce the cost for organisations.
Ternary Conditional Operators
Ternary conditional operators are an alternative to if conditional statements.
They can be utilised to condense boolean conditional-based statements into one clean line of code.
For example, the following if statement
int membershipCost;
if(isStaff)
{
membershipCost = 10;
}
else {
membershipCost = 30;
}
Can be alternatively written using a ternary operator
int membershipCost = (isStaff) ? 0 : 30;
Whereby the value after the ? is returned if the boolean condition is true, otherwise the value after the : is returned.
This results in a cleaner format of code and avoids the needs to instantiate the variable or repeat the assignment.
Another benefit is that the DRY (don’t repeat yourself) principle is also followed as the variable name is only referenced once in the ternary statement, as opposed to three times in the standard if statement.
It must be noted that if the conditional statement is likely to be extended to include more than two branches, the ternary operator will need be refactored back to an alternative form such as an if statement.
Tuples

Tuples can be powerful in scenarios where a method needs to return multiple values but where a class creation is excessive.
However, they must be used cautiously because if the return data is going to be reused, it would make more sense to create a custom class to contain the return properties.
Public Book getNewBook(){
Book book = new Book(){
Name: Test
Year: 2017
Genre: Thriller
}
return book;
}
In the above example, the creation of this book class may be unnecessary and wasteful.
Instead, tuples can be used to return these values without cluttering the project with an extra class.
The tuple can used by firstly replacing the return type of book with a tuple.
Public (string Name, int Year, string Genre) getNewBook(){
return ('Test',2017,'Thriller');
}
It must be noted that prior to C# Version 7, tuples had to be created using the Tuple.Create() method.
Similar to returning a class type, the tuples elements can be accessed by treating it as an object as follows:
var newBook = getNewBook();
string newBookName = newBook.Name;
Booleans

Working with Booleans is an inevitable task for most developers.
But many do not understand that the way they use them for assignments and comparisons is not the cleanest and most easily understandable approach.
Common bad practice is when boolean conditions are expressed as being equal to true or false as follows
if(isLegal == true){ ... }
if(isLegal == false){ ... }
This is unnecessary and can be written without the equality operator
if(isLegal){ ... }
if(!isLegal){ ... }
It is also greatly beneficial to assign Boolean conditions to variables.
This makes it a lot easier to follow and understand.
In the following example, the variable ‘membershipPoints’ is queried and the ‘hasFreeCredit’ variable is set to either true or false.
bool hasFreeCredit;
if(membershipPoints > 50)
{
hasFreeCredit = true;
}
else
{
hasFreeCredit = false;
}
This is a common scenario which is often handled as outlined.
This approach is excessive, does not adhere to the DRY principle, and is harder to read than assigning the boolean to a variable.
It can be written in a cleaner fashion with the following code, whereby the value assigned to the variable is a condition which can either return true or false.
bool hasFreeCredit = membershipPoints > 50;
Null Coalescing Operator
Null coalescing operators can be used when checking for nulls.
Using the ?? operator, if the value on the left of the operator is null, the value on the right will be returned.
The following code can be simplified using this operator from
string returnDate;
if(date == null)
{
returnDate = "N/A";
}
else
{
returnDate = date;
}
to
string returnDate = date ?? "N/A";
Icons made by Icon home from www.flaticon.com