ardalis / ardalis/GuardClauses

Correct Expression signature

Open
#366 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.3k
Forks
296
PR merge metrics
No merged PRs in 30d

Description

The Expression of T method seems illogical to me.

```
public static T Expression(this IGuardClause guardClause,
Func func,
T input,
string message,
[CallerArgumentExpression("input")] string? parameterName = null,
Func? exceptionCreator = null)
where T : struct
```

There are 2 problems in my opinion.
1. The message is mandatory, but not used if you use the exceptionCreator method
2. If you want to create your own exception, then you are forced to specify the parameterName

So I would propose to split this method in 2 overloads:
```
public static T Expression(this IGuardClause guardClause,
Func func,
T input,
string message,
[CallerArgumentExpression("input")] string? parameterName = null)
where T : struct
```
and
```
public static T Expression(this IGuardClause guardClause,
Func func,
T input,
Func exceptionCreator,
[CallerArgumentExpression("input")] string? parameterName = null)
where T : struct
```

(Same goes for EpressionAsync)

What do you think?

Contributor guide

Open the contributing guide

Research direction

Start by locating the existing Expression and ExpressionAsync entry points and inspect their current overloads and related tests. The change is done when message-based and exceptionCreator-based overloads no longer require unused or unnecessary arguments, with the synchronous and asynchronous APIs following the same design.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.