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

Exceptions for invalid use - review and extensions #106

Conversation

feO2x
Copy link
Contributor

@feO2x feO2x commented May 9, 2024

Hey Amichai,

I reviewed your exception PR and would suggest the following changes:

  • IsError is now a computed property. This removes 8 bits for the boolean value from the struct which leads to less execution time when copying by value. The sole indicator is now _errors being null.
  • I introduced a cached empty list for ErrorsOrEmptyList. This prevents allocating a new list on every property call where no errors are present.
  • The Value property will now throw an InvalidOperationException when errors are present. This behavior is now in line with the behavior of the Errors or FirstError property.
  • I changed the InvalidOperationException thrown in the implicit conversion methods to an ArgumentException - this is the usual exception that callers would expect when invalid parameters are passed in.
  • I added a ToErrorOr extension method for error arrays - we did not have this one beforehand, only for lists.
  • I removed the unnecessary null-forgiving operator uses in ErrorOr<TValue>. The C# compiler will comprehend that a field is not null according to the MemberNotNullWhenAttribute instances applied to IsError.
  • I added additional XML exception comments for improved dev experience.

Feel free to cherry-pick these features as you see fit.

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>
@feO2x feO2x force-pushed the exceptions-for-invalid-use branch from 9da4708 to 61d1540 Compare May 9, 2024 18:39
feO2x added 7 commits May 9, 2024 20:39
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>
@amantinband
Copy link
Owner

I'm lucky to get your help on this. Thanks!

@amantinband amantinband merged commit 7f4bbe3 into amantinband:exceptions-for-invalid-use May 12, 2024
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.

None yet

2 participants