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

Draft: Migrate from Errors to Exceptions for invalid use of library #105

Merged
merged 13 commits into from
May 12, 2024

Conversation

amantinband
Copy link
Owner

No description provided.

Copy link

codecov bot commented May 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (dcf6122) to head (fcc4099).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #105   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines          423       435   +12     
  Branches        49        54    +5     
=========================================
+ Hits           423       435   +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@feO2x
Copy link
Contributor

feO2x commented May 9, 2024

Hey Amichai,

I've reviewed your code and created a new PR with suggested changes.

I still have one question: would we allow null to be a valid value for ErrorOr<TValue>? If not, we should change the implementation of

public static implicit operator ErrorOr<TValue>(TValue value)
{
    return new ErrorOr<TValue>(value);
}

to

public static implicit operator ErrorOr<TValue>(TValue value)
{
    if (value is null)
    {
        throw new ArgumentNullException(nameof(value));
    }

    return new ErrorOr<TValue>(value);
}

The reason behind this is the following: users of our library might have <Nullable> turned off. The C# compiler would then no longer create warnings when null is passed as the value for an ErrorOr<TValue> instance (where TValue is resolved to a reference type). This is why we as framework/library devs should check for null (even if we have <Nullable> enabled in our code base).

I'm interested in your opinion on this.

feO2x added 8 commits May 9, 2024 20:38
This change saves us 8 bits in the ErrorOr<TValue> struct which leads to less execution time when an instance is copied by value. As the struct is now properly encapsulated from a Design by Contract point of view, I simply choose _errors being null as the indicator for errors not being present.

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
Instead of instantiating a new list every time ErrorsOrEmptyList is called with no errors being present, I reused a cached empty list. I also removed the unnecessary null-forgiving operators (the compiler uses the MemberNotNullWhenAttribute on IsError to verify that we checked for null beforehand).

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
The Value property is now properly encapsulated. When errors are present, the property getter will throw, similar to other properties like Errors or FirstError.

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
When clients have <Nullable> turned off, the C# compiler won't hint that null shouldn't be passed as the errors array/list. By explicitly checking for null, we eliminate these error cases.

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
… passed

Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
Signed-off-by: Kenny Pflug <kenny.pflug@live.de>
@feO2x
Copy link
Contributor

feO2x commented May 9, 2024

We could also ask the community if they ever want null to be a valid value.

Exceptions for invalid use - review and extensions
@amantinband
Copy link
Owner Author

Hey Amichai,

I've reviewed your code and created a new PR with suggested changes.

I still have one question: would we allow null to be a valid value for ErrorOr<TValue>? If not, we should change the implementation of

public static implicit operator ErrorOr<TValue>(TValue value)
{
    return new ErrorOr<TValue>(value);
}

to

public static implicit operator ErrorOr<TValue>(TValue value)
{
    if (value is null)
    {
        throw new ArgumentNullException(nameof(value));
    }

    return new ErrorOr<TValue>(value);
}

The reason behind this is the following: users of our library might have <Nullable> turned off. The C# compiler would then no longer create warnings when null is passed as the value for an ErrorOr<TValue> instance (where TValue is resolved to a reference type). This is why we as framework/library devs should check for null (even if we have <Nullable> enabled in our code base).

I'm interested in your opinion on this.

Yeah let's make this more strict as well. In any case this will all be introduced in a new major version. I think your suggestion is a better design.

@feO2x
Copy link
Contributor

feO2x commented May 12, 2024

Yep, you are right. These are breaking changes, so we need to bump up to version 3.

@amantinband amantinband merged commit 1294ebb into main May 12, 2024
3 checks passed
@amantinband amantinband deleted the exceptions-for-invalid-use branch May 12, 2024 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants