Skip to content

Use CallerArgumentExpressionAttribute in throw helpers#1788

Merged
dlemstra merged 11 commits into
dlemstra:mainfrom
IliaBrahinets:use-caller-argument-expression-for-null-checks
Feb 2, 2025
Merged

Use CallerArgumentExpressionAttribute in throw helpers#1788
dlemstra merged 11 commits into
dlemstra:mainfrom
IliaBrahinets:use-caller-argument-expression-for-null-checks

Conversation

@IliaBrahinets

@IliaBrahinets IliaBrahinets commented Jan 20, 2025

Copy link
Copy Markdown
Contributor

The following changes have been made in throw helpers:

  • Added CallerArgumentExpressionAttribute to simplify throw assertions (nameof(parameterName) can be omitted in most cases)
  • Added StringSyntaxAttribute for error message templates

@CLAassistant

CLAassistant commented Jan 20, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@dlemstra

Copy link
Copy Markdown
Owner

Thanks for your pull request it will probably take a while before I have time to look at this. Only quick thing that I saw was string? message = "Must not be null". I don't want to change the default message for a ArgumentNullException so please restore those two overloads.

@IliaBrahinets

Copy link
Copy Markdown
Contributor Author

Removed the default value for IfNull message. The are multiple options with overloads:

  1. IfNull([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? paramName = null, string? message = null)
  2. IfNull([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? paramName = null) and IfNull([NotNull] object? value, string message, [CallerArgumentExpression(nameof(value))] string? paramName = null)
  3. IfNull([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? paramName = null) and IfNull([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? paramName = null, string? message = null)

The third option causes ambiguity for Throw.IfNull(value) call, while the second isn't cosistent in terms of paramName positioning. I decided to implement the first approach for all throw helpers except overloads with message arguments. Awaiting for your feedback on this.

@dlemstra

dlemstra commented Jan 23, 2025

Copy link
Copy Markdown
Owner

It looks like you did not address that issue yet? Doing new ArgumentNullException(paramName, null) is not the same as new ArgumentNullException(paramName). I also don't mind fixing this myself.

@IliaBrahinets

Copy link
Copy Markdown
Contributor Author

You are right. Updated to handle null message cases.

@dlemstra dlemstra left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this change, looks a lot better. I left a couple comments.

Comment thread src/Magick.NET/ImageOptimizers/ImageOptimizer.cs Outdated
Comment thread src/Shared/Throw.cs Outdated
@IliaBrahinets

Copy link
Copy Markdown
Contributor Author

All comments have been resolved. Ready for review @dlemstra

@dlemstra

dlemstra commented Jan 29, 2025

Copy link
Copy Markdown
Owner

Will take a look at it this weekend. Thanks for all the fixes.

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.

3 participants