ThrowGuard 1.0.6

dotnet add package ThrowGuard --version 1.0.6
NuGet\Install-Package ThrowGuard -Version 1.0.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ThrowGuard" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ThrowGuard --version 1.0.6
#r "nuget: ThrowGuard, 1.0.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ThrowGuard as a Cake Addin
#addin nuget:?package=ThrowGuard&version=1.0.6

// Install ThrowGuard as a Cake Tool
#tool nuget:?package=ThrowGuard&version=1.0.6

Throw Helpers & Guards

A small library of throw helpers and guard methods to optimize and simplify your code.

Throw helpers optimize your methods by moving the IL code required for throwing exceptions to a shared, common place. See below to learn more.

Guard methods simplify and streamline your code, making it cleaner and easier to understand your intent. Guard methods use throw helpers internally.

Getting Started

Add the Nuget package to your project.

dotnet add package ThrowGuard

Throw Helpers

using ThrowGuard;

// throw ApplicationException...
Throw.AppException();

// throw ArgumentException...
Throw.BadArg(myParam, msg: "Parameter has an invalid value.");

// throw DirectoryNotFOundException...
Throw.DirectoryNotFound("Directory doesn't exist: {0}".SF(folderName));

// throw CustomException...
Throw.This(new CustomException());

// use alternative style (as an extension method)...
new CustomException("Parameter contains bad content.").Throw();

Guard Methods

The following is a sampling of the available guard methods.

using ThrowGuard;

namespace GuardsSamples

public class MyClass
{
  private readonly IRepo _repo;

  public MyClass(IRepo repo)
  {
    _repo = Throw.IfNull(repo);
  }

  public MyMethod(Guid id, string? name, string? name2)
  {
    var theId = Throw.IfEmpty(id, "Cannot use Guid.Empty as an identifier.");
    var theName = Throw.IfNullOrWhitespace(name);
    var altName = Throw.IfNullOrWhitespaceWhen(
      name2, () => someCondition, 
      "{0} cannot be null/whitespace when {1} is true".SF(
        nameof(name2), nameof(someCondition)));

    Throw.BadArgWhen(
      () => theName.Equals("Beetlejuice"),
      "No. Just, no!");

    Throw.IfNull(
      _repo.NameSanitizer, argName: "Repo Name Sanitizer",
      ex: _ => new InvalidOperationException(
        "A valid NameSanitizer is required for this operation."));

    Throw.IfNullOrEmpty(
      _repo.NameSanitizer.SafeList, // a collection
      argName: "Repo Sanitizer SafeList"
      ex: _ => new InvalidOperationException(
        "Name sanitizer safe-list is empty."));

    Throw.InvalidOpWhen(
      () => !_repo.NameSanitizer.Sanitize(theName), 
      "Sanitizer: Hey, I'm not a magician!");

    Throw.InvalidOpWhen(
      () =>
      {
        var idExists = _repo.IdExists(theId);
        var nameExists = _repo.NameExists(theName);
        return nameExists && !idExists;
      },
      $"Name and Id combination already in use > {theName}: {theId}");

    Throw.IfNotBetween(
      theName.Length, 4, 40, 
      "Name must be between 4 and 40 characters long.",
      nameof(theName));
  }
}

<br>

Why use throw helpers?

I'm sure you've heard -- repeatedly by now -- that throwing exceptions is expensive. What you may not know, however, is what that means in practical terms. What you may not know is that, in order for your code to be able to throw an exception, specialized IL code must be included alongside the IL for the rest of your method implementation. That means that even if your code never actually throws an exception, that IL code must still be included. This is bloat, and it's replicated in every one of your methods that might throw an exception.

So why should you care if your code is bloated? You shouldn't, unless, of course, you care about performance & efficiency! That's because the size of the generated IL code directly affects how long it takes to load and execute a method, how much memory the code takes up; as well as the disk storage and network bandwidth used. This bloat is completely useless, totally wasteful, and entirely avoidable.

Throw helpers are a highly effective technique for eliminating this bloat. By centralizing the relevant IL code, throw helpers slim down the IL generated for your methods.

So, if you do any of these:

void MyMethod(MyObject myObject, string myString)
{
  // throw ArgumentNullException if myObject is null
  // otherwise assign it to variable 'obj'
  var obj = myObject ?? throw new ArgumentNullException(nameof(myObject));

  // make sure myString is not null or whitespace 
  // before assigning it to variable 'str'
  if (string.IsNullOrEmpty(myString))
  {
    throw new ArgumentException(
      "Value is null or whitespace.", nameof(myString));
  }
  var str = myString;

  // throw built-in and custom exceptions 
  // based on some condition
  if (IsItTuesday())
  {
    throw new ApplicationException("Yes, because it's Tuesday.");
  }
  if (IsItThursday())
  {
    throw new MyCustomException("Look, I don't make the rules!")
  }
  ...
}

...replace them with these:

void MyMethod(MyObject myObject, string myString)
{
  var obj = Throw.IfNull(myObject);
  var str = Throw.IfNullOrWhitespace(myString);

  Throw.AppExceptionWhen(
    () => IsItTuesday,
    "Yes, because it's Tuesday.");

  Throw.When(
    () => IsItThursday
    new MyCustomException(
      "Look, I don't make the rules!"));
  ...
}

<br/>

License

Apache 2.0

<br/>

If you like this project, or find it useful, please give it a star. Thank you.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on ThrowGuard:

Package Downloads
AspNetStatic

Transforms ASP.NET Core into a static site generator.

RazorViewComponent

Component-based UI elements for AspNet Razor.

AspNetStaticContrib

AspNetStatic community contributed extensions.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 241 12/16/2023
1.0.5 70 12/15/2023
1.0.4 201 12/5/2023
1.0.3 109 12/3/2023
1.0.2 91 12/1/2023
1.0.1 83 11/25/2023
1.0.0 94 11/24/2023