Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

A set of simple guard clauses for method parameters

License

Notifications You must be signed in to change notification settings

steveniles/Jargon.Guard

Repository files navigation

Jargon Guard

nugetreleaselicense

Instread of writing

if(parameter==null){thrownewArgumentNullException();}

you can simply write

Guard.CannotBeNull(parameter);

or using fluent syntax

parameter.CannotBeNull();

Optional arguments

You can optionally provide the parameter name or a custom error message (or both)

parameter.CannotBeNull("parameter","Custom Error Message");

is the same as

if(parameter==null){thrownewArgumentNullException(paramName:"parameter",message:"Custom Error Message");}

Clause expressions

All clauses return the input parameter, so you can guard and consume it on the same line

this.SomeProperty=parameter.CannotBeNull();

Nullable structs return non-nullable version

intnumber=input.MaybeNullNumber.CannotBeNull();

Other clauses

Clauses are granular to provide maximum control of possible values

Collections

Ensure arrays and collections are not empty

items.CannotBeEmpty();//allows null but not emptyitems.CannotBeEmpty().CannotBeNull()//prevents both

Strings

Ensure strings are not empty ("") or blank (non-zero whitespace e.g. " ")

name.CannotBeEmpty().CannotBeBlank();

Guids

Ensure guids are not empty (00000000-0000-0000-0000-000000000000)

id.CannotBeEmpty();

About

A set of simple guard clauses for method parameters

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages