Remove null-forgiving operators and introduce TryCreate/Create pattern for Version class#7
Conversation
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Malcolmnixon
left a comment
There was a problem hiding this comment.
It seems many of the null checks are around Version.Create. Possibly introducing a Version.TryCreate which can return the nullabke, and have Version.Create return a non-nullable and throw on error.
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Implemented in commit 03a0283. Added |
Pull Request
Description
Removed all null-forgiving operators (
!) from the codebase and introduced a better API design for theVersionclass following standard .NET conventions.API Changes:
Version.TryCreate(string tag)that returnsVersion?(nullable) for cases where null is acceptableVersion.Create(string tag)to returnVersion(non-nullable) and throwArgumentExceptionfor invalid tagsint.TryParsevsint.Parse)Source Code Changes:
GitHubRepoConnector.GetPullRequestsBetweenTagsAsync: Refactored conditional branches to explicitly check null state in each branchGitHubRepoConnector.GetTagHistoryAsync: Updated to useVersion.TryCreatefor filtering non-version tagsMockRepoConnector.GetTagHistoryAsync: Updated to useVersion.TryCreatefor filtering non-version tagsif (from == null || to == null)check in final else branch to satisfy compiler's flow analysisTest Code Changes:
Assert.IsNotNull()checks (15 occurrences removed)Version.Create()directly for known-valid version stringsVersion.Create()directly in collection initializersVersion_TryCreate_ReturnsNullForInvalidTagto verify TryCreate returns null for invalid inputVersion_Create_ThrowsForInvalidTagto verify Create throws ArgumentException for invalid inputBenefits:
TryCreatewhen filtering,Createwhen expecting valid inputType of Change
Related Issues
N/A
Pre-Submission Checklist
Before submitting this pull request, ensure you have completed the following:
Build and Test
dotnet build --configuration Releasedotnet test --configuration ReleaseCode Quality
dotnet format --verify-no-changesQuality Checks
Please run the following checks before submitting:
cspell "**/*.{md,cs}"markdownlint "**/*.md"yamllint '**/*.{yml,yaml}'Testing
Documentation
Additional Notes
CodeQL security scan completed with 0 alerts. All 63 tests passing (increased from 61 with 2 new tests for TryCreate/Create behavior).
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.