- You need to use guard clauses in your strategies to stop runtime exceptions by dealing with null reference exceptions.
- You need to use guard clauses to validate enter knowledge and implement knowledge integrity by solely processing legitimate inputs.
- You need to use guard clauses to outline preconditions to your strategies and properties, enhancing the readability and maintainability of your code.
We’ll study every of those features of guard clauses utilizing code examples within the sections that comply with.
Use a guard clause to keep away from null reference exceptions in C#
Null reference exceptions are sometimes encountered in functions while you use an object reference that has not been initialized. On this part, we are going to study how we will use a guard clause to stop such exceptions from being thrown at runtime. The next code snippet exhibits how one can examine if the parameter of a technique is null and throw an ArgumentNullException, stopping a null reference exception from being thrown at runtime and permitting the tactic to finish gracefully.
public void CheckIfNull(object obj)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj), "Methodology parameter can't be null.");
}
//Different code
}
Use a guard clause to implement enter validation guidelines in C#
Enter validation allows you to preserve knowledge integrity by imposing validation guidelines and constraints in your software. You possibly can implement guard clauses in your software’s supply code to permit solely legitimate knowledge to be processed by your software.
The next code instance exhibits how you need to use a guard clause in C# to stop invalid enter. Be aware how an exception is thrown if the enter string is null or empty.
public void CheckIfNullOrEmpty(string str)
{
if(!string.IsNullOrEmpty(str))
{
throw new ArgumentException("Invalid knowledge: The string handed within the methodology argument can't be empty or null");
}
//Different code
}
Use a guard clause to boost code readability and maintainability in C#
Guard clauses enable you write maintainable and readable code by centralizing your software’s validation guidelines. They supply a chic technique to forestall sudden conduct in your software’s code, making it constant, organized, and simple to keep up.
Allow us to perceive this with a code instance. Within the console software venture we created earlier, create a brand new class named Creator and enter the next code.
class Creator
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Tackle { get; set; }
public string E mail { get; set; }
}
The next code snippet illustrates how one can make the most of the constructor of the Creator class to validate enter.
public Creator(string firstName, string lastName, string deal with, string electronic mail)
{
if (string.IsNullOrEmpty(firstName))
{
throw new ArgumentException("First title can't be null or empty", nameof(firstName));
}
if (string.IsNullOrEmpty(lastName))
{
throw new ArgumentException("Final title can't be null or empty", nameof(lastName));
}
if (string.IsNullOrEmpty(deal with))
{
throw new ArgumentException("Tackle can't be null or empty", nameof(deal with));
}
if (string.IsNullOrEmpty(electronic mail))
{
throw new ArgumentException("E mail can't be null or empty", nameof(electronic mail));
}
}
As you possibly can see, the previous code snippet is kind of verbose. We will make it far more concise and readable through the use of guard clauses. Be aware that the next code can change the entire if statements used within the constructor of the Creator class above.