• This repository has been archived on 02/Mar/2021
  • Stars
    star
    258
  • Rank 152,886 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🔒 The most simple, unambiguous, and lightweight .NET guard clause library.

LiteGuard

This project is no longer maintained.

With recent C# language features, there's no need for a guard clause library.

arg1?.Foo();
myField = arg2 ?? throw new ArgumentNullException(nameof(arg2));
arg3 = arg3 ?? throw new ArgumentNullException(nameof(arg3));
// etc.

LiteGuard NuGet version LiteGuard.Source NuGet version

Build status CodeQL analysis Lint Spell check

Why, it's lighter than air!

The guard clause library which stands out from the crowd like the faintest passing whisper.

You'll hardly know it's there!

LiteGuard is a .NET package available as source or binary for writing guard clauses. If your project is an application or library which is not packaged and/or exported for use in other solutions then the binary package is usually the best choice. If you are writing a library which is packaged and/or exported for use in other solutions then the source code package is usually the best choice.

Platform support: .NET Standard 1.0 and upwards.

We all have to do our bit in working toward the ultimate number of published guard clause libraries which, at current estimates, is somewhere in the region of 6.02214129(27) × 10^23.

Quick start

public void Foo(Bar bar)
{
    Guard.AgainstNullArgument(nameof(bar), bar);
    Guard.AgainstNullArgumentProperty(nameof(bar), nameof(bar.Baz), bar.Baz);
    Guard.AgainstNullArgumentProperty(nameof(bar), "Baz.Bazz", bar.Baz.Bazz);

    // the rest of your method, nice and safe, wrapped in the protecting arms of LiteGuard
}

public void Foo<T>(T bar) where T : class
{
    Guard.AgainstNullArgument(nameof(bar), bar);
    ...
}

public void Foo<T>(T bar)
{
    Guard.AgainstNullArgumentIfNullable(nameof(bar), bar);
    ...
}

Why did I create it?

The aim of LiteGuard is to be the most simple, unambiguous and lightweight guard clause library available.

A very explicit DSL

The names of LiteGuard clauses are unambiguous to ensure correct usage. Misuse should be obvious.

public void Foo(Bar bar)
{
    var baz = GetBaz();
    Guard.AgainstNullArgument("baz", baz); // clearly incorrect - baz is not an argument
}

No fluent API

Some guard clause libraries provide a fluent API.

A fluent API requires the creation of objects which serve no purpose other than to provide an access point for the next method in the DSL. The creation of these objects decreases performance, increases memory usage and adds pressure to the garbage collector. It is my belief that a guard clause library has no business in doing this. It should use as few resources as possible and be eligible for use in as wide a set of applications as possible. I love a good fluent API but it has its places and a guard clause library is not one of them.

No business rule clauses

Many guard clause libraries provide a huge range of methods for determining whether arguments satisfy all manner of business rules.

In my opinion, it is not the job of a guard clause library to validate arguments against business rules. I believe the role of a guard clause library is to prevent method calls with null arguments or null argument values. Ideally, I'd like such things to be built into .NET languages. If that ever happens I will happily allow LiteGuard to retire gracefully to a small but comfortable home near the seaside with a carriage clock and a little Havanese.


LiteGuard logo designed by Vanja Pakaski.

More Repositories

1

bullseye

🎯 A .NET library for running a target dependency graph.
C#
810
star
2

minver

🏷 Minimalistic versioning using Git tags.
C#
790
star
3

simple-exec

🏃 A .NET library that runs external commands.
C#
638
star
4

xbehave.net

✖ An xUnit.net extension for describing each step in a test with natural language.
C#
382
star
5

bau

The C# task runner
C#
151
star
6

scriptcs-nancy

Nancy Script Pack for scriptcs
C#
35
star
7

dude

The portable C# script runner!
C#
20
star
8

simple-targets-csx

⊙ A minimalist target runner for C# scripts.
C#
17
star
9

netstandard-deck

A slide deck for "What is .NET Standard?".
JavaScript
7
star
10

bau-msbuild

A Bau plugin for running MSBuild
C#
7
star
11

dotnet-scripting-dialect

Draft proposal for .NET scripting "dialects"
6
star
12

stylecop-msbuild

Adds official StyleCop MSBuild targets to your project for running StyleCop analysis with every build.
5
star
13

blunt

Smoke testing for multi-targetting NuGet packages.
C#
5
star
14

adamralph.github.com

My website
CSS
3
star
15

h5h

An HTML5 highlighter
CSS
3
star
16

code-the-future-now

Code samples for https://sessionize.com/s/adamralph/code_the_future_now/21105
C#
2
star
17

bau-nuget

A Bau plugin for running NuGet commands
C#
2
star
18

cola

The portable LINQPad runner!
C#
2
star
19

domaindrivendesign

A .NET framework for building applications using Domain Driven Design.
C#
2
star
20

racer83

A recreation of one of the masterpieces of my youth
Visual Basic
2
star
21

netstandard-magic

C#
1
star
22

.github

Standard files for my repos
1
star
23

xbehave.github.io

HTML
1
star
24

nancy-deck

Slides for my talk 'Nancy, the super duper happy OSS .NET project'
JavaScript
1
star