.NET C# clean code and best practice Part-1 (Conditional Statement)
Early return pattern
When you have complicated business rules inside a method, using early returns can make the code clearer and easier to manage.
This approach prevents the need for excessive levels of code nesting and helps make the order in which the code runs smoother. This leads to code that is easier to read and more compact.
Example:
if (condition)
{
//complex logic
//....
//....
//....
}
// early return using invert if condtion
if (invertCondition)
{
return;
}
//now add complex logic here
Don’t use many conditions inside If statement
if(condition1 && condition2 && condition3)
{
// do something
}
When dealing with multiple conditions within an if statement, you can opt for either of these approaches:
- Consolidate conditions into a local variable and treat them as a single condition.
bool isValidCondtion = condition1 && condition2 && condition3;
if(isValidCondtion)
{
// do something
}
2. Develop a helper validation method and invoke it within the main method.
if(Validate(DummyEnitity dummyEnitity))
{
// do something
}
Both approaches serve the purpose of simplifying complex conditions and enhancing code readability. Your choice between them should be based on the context and your preference for organizing the code.
Don’t use negation operator
Make your code more readable.
if(!isValid)
{
}
//Instead add condtion without negation operator
if(isValid)
{
}
Avoid using excessive conditional statements within a method as it can raise the cyclomatic complexity of the code. This can make the code harder to understand, test and maintain.
internal void TestMethod()
{
if (condition1)
{
// do something
//......
//......
//conditional statement
//......
//......
}
if (condition2)
{
// do something
//......
//......
//conditional statement
//......
//......
}
}
We can improve this method by creating smaller sub-methods and relocating the conditional logic into them. This approach will lower the cyclomatic complexity, resulting in code that is easier to understand, test, and maintain.
internal void TestMethod()
{
SubMethod1();
SubMethod2();
}
private void SubMethod1()
{
if (condition1)
{
// do something
//......
//......
//conditional statement
//......
//......
}
}
private void SubMethod2()
{
if (condition2)
{
// do something
//......
//......
//conditional statement
//......
//......
}
}
Replace the “==” operator with “is” and the “!=” operator with “is not” for better code clarity and adherence to best practices. This helps improve the readability and maintainability of the code.
if (result is not null)
{
// do something
}
Null-coalescing operator(??) and Null-conditional operators (
?.)
// If user Address or City is null then cityName becomes "Unknown"
string cityName = user?.Address?.City ?? "Unknown";
Both operators can significantly improve code readability and help handle null values more gracefully.
If statement always uses curly brackets { }
if (lstProduct.HasItems())
{
// do something
}
Quite simple scenario uses ternary operator (?:)
int number = 9;
string numberType = (number % 2 == 0) ? "Even" : "Odd";
Please read the following link: .NET C# clean code and best practice Part-2
https://mabhishekit.medium.com/net-c-clean-code-and-best-practice-part-2-c7f3e133ca69