Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conditionally compiledGuard API #922

Open
AlgorithmsAreCool opened this issue Aug 28, 2024 · 0 comments
Open

conditionally compiledGuard API #922

AlgorithmsAreCool opened this issue Aug 28, 2024 · 0 comments
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@AlgorithmsAreCool
Copy link

Overview

I've been writing some code along a very hot path and I'm finding some tension between wanting the safety of using Guard clauses but also worrying about the runtime overhead.

Has there been any consideration of defining a sister API that used ConditionalAttribute to elide these guards/assertions in release builds?

This can be done external to this library via #if DEBUG but there is some precedent with the Debug.Assert family of methods.

API breakdown

public static class DebugGuard //Open to any other name...
{
    [ConditionalAttribute("Debug")] 
    public static void IsGreaterThan<T>(T value, T minimum, [CallerArgumentExpression(nameof(value))] string name = "");
    
    [ConditionalAttribute("Debug")] 
    public static void IsGreaterThanOrEqualTo<T>(T value, T minimum, [CallerArgumentExpression(nameof(value))] string name = "");
...
}

Usage example

public void MyHotPath(int index)
{
    DebugGuard.IsGreaterThanOfEqualTo(index, 0);
    DebugGuard.IsLessThanOf(index, SomeLimit);
}

Breaking change?

No

Alternatives

public void MyHotPath(int index)
{
#if DEBUG
    Guard.IsGreaterThanOfEqualTo(index, 0);
    Guard.IsGreaterThanOfEqualTo(index, 0);
#endif
}

Additional context

No response

Help us help you

Yes, but only if others can assist

@AlgorithmsAreCool AlgorithmsAreCool added the feature request 📬 A request for new changes to improve functionality label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality
Projects
None yet
Development

No branches or pull requests

1 participant