This repository provides a comprehensive guide to implementing guard clauses in .NET. Guard clauses are a powerful tool in defensive programming, ensuring that your code fails fast when encountering invalid input or unexpected conditions.
In this project, you'll explore:
- Using built-in .NET features for guard clauses.
- Implementing custom guard clauses tailored to your needs.
- Practical examples with Order and Customer objects in a console application.
- Built-in Guard Clauses: Using .NET features like
ArgumentNullException
,ArgumentException
, andArgumentOutOfRangeException
for input validation. - Custom Guard Clauses: Creating reusable, maintainable, and expressive validation methods.
- Order Example: Validates order properties such as quantity and price.
- Customer Example: Ensures customer properties like name and age meet expected constraints.
📦 GuardClauses
┣ 📂 GuardClauses # Console application showcasing guard clauses in action
┣ 📂 Tests # Unit tests for built-in and custom guard clauses (coming soon)
Ensure you have the following installed:
- .NET Core SDK
- A modern C# IDE (e.g., Visual Studio or JetBrains Rider)
git clone https://github.com/MrEshboboyev/guard-clauses.git
cd GuardClauses
dotnet run --project GuardClauses
Dive into the Ensure
to see how guard clauses are implemented and used.
static void ProcessOrder(Order order)
{
Ensure.NotNull(order);
Ensure.NotNullOrEmpty(order.CustomerName);
Ensure.NotNullOrEmptyList(order.Products);
Console.WriteLine($"User {order.CustomerName} has ordered:");
foreach (var product in order.Products)
{
Console.WriteLine($"{product}");
}
}
public static class Ensure
{
public static void NotNull<T>(T? value, [CallerArgumentExpression("value")] string? paramName = null)
{
if (value is null) throw new ArgumentNullException(
"The value cannot be null",
paramName);
}
public static void NotNullOrEmpty(string? value, [CallerArgumentExpression("value")] string? paramName = null)
{
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(
"The string cannot be null nor empty",
paramName);
}
public static void NotNullOrEmptyList<T>(List<T>? list, [CallerArgumentExpression("list")] string? paramName = null)
{
if (list is null || list.Count == 0)
{
throw new ArgumentException("The list should contain at least 1 item.", paramName);
}
}
}
// Usage
static void ProcessOrder(Order order)
{
Ensure.NotNull(order);
Ensure.NotNullOrEmpty(order.CustomerName);
Ensure.NotNullOrEmptyList(order.Products);
Console.WriteLine($"User {order.CustomerName} has ordered:");
foreach (var product in order.Products)
{
Console.WriteLine($"{product}");
}
}
- Ensure order quantity and price are valid before processing.
- Validate customer data such as name and age during object instantiation.
The repository includes unit tests for both built-in and custom guard clauses.
- Fail Fast: Identify and handle invalid inputs immediately.
- Readable Code: Simplify method logic by removing nested validation checks.
- Reusable Logic: Custom guard clauses make validation consistent and maintainable.
This project was developed by MrEshboboyev, a software developer passionate about clean code, defensive programming, and scalable architectures.
This project is licensed under the MIT License. Feel free to use and adapt the code for your own projects.
C#, .NET, Guard Clauses, Defensive Programming, Input Validation, Software Architecture, Clean Code, Error Handling, Console Application, Custom Implementation
Feel free to suggest additional features or ask questions! 🚀