The "When" NuGet package provides a set of extension methods for the bool type, facilitating conditional execution of synchronous and asynchronous actions based on boolean evaluations
$ dotnet add package When.CoreWhen is a minimalist C# library designed for developers who prefer to avoid the verbosity of traditional if statements, especially when dealing with single-line conditions. By offering extension methods for the bool type, When enables the execution of synchronous and asynchronous actions based on boolean evaluations, resulting in cleaner and more readable code.
Synchronous Execution: Execute actions when a boolean value is true, false, or based on either condition.
Asynchronous Support: Handle asynchronous tasks with Task and ValueTask return types, facilitating seamless integration with modern async programming patterns.
If you hate how this codes looks
public async Task DoSomething(bool condition)
{
if (condition)
{
await Console.Out.WriteLineAsync("Condition is true.");
}
else
{
await Console.Out.WriteLineAsync("Condition is false.");
}
}
// Or even simpler synchronous code like
public void DoSomething(bool condition)
{
if (condition) Console.WriteLine("Condition is true.");
}
because you really want to write code like
using When.Core.Extensions;
public async Task DoSomething(bool condition)
=> await condition.WhenTrueElse(() => Console.Out.WriteLineAsync("Condition is true."), () => Console.Out.WriteLineAsync("Condition is false."))
public void DoSomething(bool condition)
=> condition.WhenTrue(() => Console.WriteLine("Condition is true."));
Then you might like When.
Its probably one of the most trivial nuget packages that you will ever stumble across, but it keeps me happy so I thought I would share it.