Skip to content

Standardize argument-name validation in OutputBuilderFactory#714

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-modernize-throwifnull
Jul 13, 2026
Merged

Standardize argument-name validation in OutputBuilderFactory#714
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-modernize-throwifnull

Conversation

@tannergooding

Copy link
Copy Markdown
Member

The five public methods on OutputBuilderFactory each duplicated the same string name guard, split across two styles: an if (string.IsNullOrWhiteSpace(name)) throw block in Create/CreateTests, and a ternary-throw form in GetOutputBuilder/GetTestOutputBuilder/TryGetOutputBuilder. This factors the check into a single private ValidateName helper called from every method.

ValidateName is annotated [NotNull] on its name parameter so the analyzer treats name as non-null once it returns, matching how ArgumentNullException.ThrowIfNull is annotated.

Exception semantics are preserved exactly. The existing guard throws ArgumentNullException(nameof(name)) for null, empty, and whitespace names, so ValidateName keeps the string.IsNullOrWhiteSpace check rather than switching to ArgumentNullException.ThrowIfNull (which would not throw for a non-null empty/whitespace name) or ArgumentException.ThrowIfNullOrWhiteSpace (which would throw ArgumentException instead of ArgumentNullException). The distinct ArgumentException for a non-test builder in GetTestOutputBuilder is left unchanged.

Build (Release): 0 warnings, 0 errors. Golden PInvokeGenerator.UnitTests: 3708 passed, 0 failed.

The five public methods each duplicated the same `string name` guard,
split across two styles: an `if (string.IsNullOrWhiteSpace(name)) throw`
block in `Create`/`CreateTests` and a ternary-throw form in
`GetOutputBuilder`/`GetTestOutputBuilder`/`TryGetOutputBuilder`. Factor
the check into a single private `ValidateName` helper and call it from
every method so the guard lives in one place.

Preserve exception semantics exactly. The existing guard throws
`ArgumentNullException(nameof(name))` for null, empty, and whitespace
names, so `ValidateName` retains the `string.IsNullOrWhiteSpace` check
rather than switching to `ArgumentNullException.ThrowIfNull` (which
would not throw for non-null empty/whitespace) or
`ArgumentException.ThrowIfNullOrWhiteSpace` (which would throw
`ArgumentException` instead). The distinct `ArgumentException` for a
non-test builder in `GetTestOutputBuilder` is left unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit a3daabb into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding
tannergooding deleted the tannergooding-modernize-throwifnull branch July 13, 2026 02:36
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.

1 participant