Skip to content

NullReferenceExceptions are the root of all evil. Slay them for good with SpicyTaco.Maybe.

License

Notifications You must be signed in to change notification settings

kmcginnes/SpicyTaco.Maybe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpicyTaco.Maybe Build status NuGet Prerelease Version MIT License

🚨 No longer maintained. 🚨

Bad news. I have moved on to an entirely different platform (iOS/Swift) and will no longer be maintaining this library.

The good news is that Microsoft is introducing nullable reference types in C# 8. That should make libraries like this one less important. Although, I believe that they still have a place.

Introduction

No one likes NullReferenceExceptions. Let's change things. Let consumers of your code know when a return value might be empty. Force them to acknowledge a possible lack of value.

My goal is to make Maybe as easy to use as possible, but also force the consuming code down the path of success. I'm doing this by not including nice to have features such as implicit conversions, .HasValue or .Value, etc.

Usage

When you're returning a value that might be null, follow this style:

public Maybe<Person> GetPersonByName(string firstName, string lastName)
{
  var person = _database.People
    .Where(x => x.FirstName == firstName && x.LastName == lastName)
    .FirstOrDefault()
    .ToMaybe();
  return person;
}

You're consumer will only be able to execute code on the value through accessor methods:

GetPersonByName("John", "Doe")
  .Do(person => SomeUsefulAction(person))
  .DoWhenEmpty(() => this.Log().Warn("No user named 'John Doe' could be found."));

Install

You're welcome to install this library, but keep in mind it is a pre-release. I'm still plying with the API surface. Things might get renamed or dissapeared.

Install-Package SpicyTaco.Maybe -Pre

License

I've licensed this under MIT License. That means do what you want with this code.

About

NullReferenceExceptions are the root of all evil. Slay them for good with SpicyTaco.Maybe.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages