https://github.com/kmcginnes/spicytaco.maybe
NullReferenceExceptions are the root of all evil. Slay them for good with SpicyTaco.Maybe.
https://github.com/kmcginnes/spicytaco.maybe
c-sharp maybe monad null optionals
Last synced: 6 months ago
JSON representation
NullReferenceExceptions are the root of all evil. Slay them for good with SpicyTaco.Maybe.
- Host: GitHub
- URL: https://github.com/kmcginnes/spicytaco.maybe
- Owner: kmcginnes
- License: mit
- Created: 2015-03-08T00:47:52.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2019-06-02T21:39:41.000Z (almost 7 years ago)
- Last Synced: 2025-02-03T10:46:33.611Z (about 1 year ago)
- Topics: c-sharp, maybe, monad, null, optionals
- Language: C#
- Homepage:
- Size: 101 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- License: License.md
Awesome Lists containing this project
README
SpicyTaco.Maybe [](http://teamcity.krismcginnes.com:8084/viewType.html?buildTypeId=SpicyTacoMaybe_Build&guest=1) [](https://www.nuget.org/packages/SpicyTaco.Maybe/) [](License.md)
===============
## 🚨 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:
```c#
public Maybe 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:
```c#
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.