Skip to content

Repository files navigation

Guard Clauses

Designed to fail early when encountering arguments outside the correct specification.

The bugs I hate are the ones that show up only after hours of successful operation, under unusual circumstances, or whose stack traces lead to dead ends. -Martin Fowler.

Fail fast is a technique that exposes potential bugs early in development, making them immediately visible.

Assertions are the key to failing fast. An assertion is a tiny piece of code that checks a condition and then fails if the condition isn’t met. So, when something starts to go wrong, an assertion detects the problem and makes it visible. -Martin Fowler.

Usage

publicenumCategory{Client,Vip}publicclassPerson{intid;stringname;DateOnlybirthday;Categorycategory;doublestars;publicPerson(intid,stringname,DateOnlybirthday,Categorycategory,doublestars){this.id=Guard.Against.NegativeOrZero(id);this.name=Guard.Against.NullOrWhiteSpace(name).And(x =>x.Length<=50,nameof(name)).And(x =>x.Length>=3,nameof(name));this.birthday=Guard.Against.OutOfRange(birthday,minimunValue:newDateOnly(1900,1,1),maximunValue:newDateOnly(2022,1,1));this.category=Guard.Against.EnumOutOfRange(category);this.stars=Guard.Against.OutOfRange(stars,minimunValue:0,maximunValue:5);}}

Extensions methods

MethodException
Guard.Against.ZeroThrow a ArgumentException if the input number is zero.
Guard.Against.NegativeThrow a ArgumentException if the input number is negative.
Guard.Against.NegativeOrZeroThrow a ArgumentException if the input number is negative or zero.
Guard.Against.InvalidRegexFormatThrow a ArgumentException if the input number does not match with a Regex pattern.
Guard.Against.InvalidInputThrow a ArgumentException if the input does not pass a predicate function.
Guard.Against.OutOfRangeThrow a ArgumentOutOfRangeException if the input is out of range.
Guard.Against.EnumOutOfRangeThrow a InvalidEnumArgumentException if the input is a not defined enum.
Guard.Against.NullThrow a ArgumentNullException if the input is null.
Guard.Against.NullOrEmptyThrow a ArgumentNullException if the input is null or empty.
Guard.Against.NullOrWhiteSpaceThrow a ArgumentNullException if the input is null, empty or white spaces.
AndThrow a ArgumentException if the input does not satisfy a condition.

Custom extensions

You can create your own extension methods by simply extending the IGuardClause interface inside the GuardClauses.Extensions namespace.

namespaceGuardClauses.Extensions;publicstaticclassZipCodeExtensions{publicstaticZipCodeInvalidZipCode(thisIGuardClauseguardClause,ZipCodezipCode)=>ValidateZipCode(zipCode)?zipCode:thrownewArgumentException($"ZipCode ({zipCode}) is invalid.",nameof(zipCode));}

Usage:

publicclassAddress{stringLine1,Line2,City,State,ZipCode;publicAddress(stringline1,stringline2,stringcity,stringstate,ZipCodezipCode){Line1=line1;
...
...ZipCode=Guard.Against.InvalidZipCode(zipCode);}
...}

References

About

A collection of extension methods for validating method arguments in order to spot bugs as quickly as possible.

Topics

Resources

Code of conduct

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages