Skip to content

CodeQL 8: perf: replace ContainsKey+indexer with TryGetValue#196

Open
rlorenzo wants to merge 1 commit into
mainfrom
codeql/8-trygetvalue
Open

CodeQL 8: perf: replace ContainsKey+indexer with TryGetValue#196
rlorenzo wants to merge 1 commit into
mainfrom
codeql/8-trygetvalue

Conversation

@rlorenzo
Copy link
Copy Markdown
Contributor

@rlorenzo rlorenzo commented May 13, 2026

Summary

Closes ~14 of 18 cs/inefficient-containskey alerts by replacing ContainsKey + indexer (two dictionary lookups) with TryGetValue (one lookup) in C# and dropping the redundant ContainsKey guard in Razor views.

Changes

C# (3 files, 4 sites)

  • RotationsController.cs:498-511 - three personData.ContainsKey(i.MothraId) ? personData[i.MothraId].XXX : "Unknown" ternaries collapsed into one TryGetValue(out var p) plus three p?.XXX ?? "Unknown" expressions.
  • RAPSAuditService.cs:153 - ContainsKey(key) + indexerTryGetValue(key, out var moreRecentActions).
  • VMACSExport.cs:303, 345 - same pattern in two helpers.

Razor views (4 files, 10 sites)

Pattern ViewData.ContainsKey("X") && (bool)(ViewData["X"] ?? false) collapsed to (bool)(ViewData["X"] ?? false) - ViewData[] already returns null on miss, so the ContainsKey guard is redundant once the ?? false is present. Touches:

  • Areas/RAPS/Views/Members/List.cshtml (×4)
  • Areas/RAPS/Views/Members/Roles.cshtml (×1)
  • Areas/RAPS/Views/Roles/Members.cshtml (×1)
  • Areas/RAPS/Views/Roles/Templates.cshtml (×4)

Not addressed: 1 test/Students/EmergencyContactControllerTests.cs:319 site (test infrastructure) and 3 likely already covered or out-of-scope.

Context

Eighth in the CodeQL N: cleanup series.

Test plan

  • npm run test:backend - 1946 tests passing
  • Pre-commit lint+test+verify all passed
  • CodeQL workflow on this PR shows ~14 of the 18 ContainsKey alerts closed

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 40aa73d7-6ad2-4972-a438-765410517512

📥 Commits

Reviewing files that changed from the base of the PR and between 38de1ad and 3ec2656.

📒 Files selected for processing (7)
  • web/Areas/ClinicalScheduler/Controllers/RotationsController.cs
  • web/Areas/RAPS/Services/RAPSAuditService.cs
  • web/Areas/RAPS/Services/VMACSExport.cs
  • web/Areas/RAPS/Views/Members/List.cshtml
  • web/Areas/RAPS/Views/Members/Roles.cshtml
  • web/Areas/RAPS/Views/Roles/Members.cshtml
  • web/Areas/RAPS/Views/Roles/Templates.cshtml

📝 Walkthrough

Walkthrough

Six files refactored to replace ContainsKey + dictionary indexing with TryGetValue in C# services, and simplified ViewData conditionals in Razor views using null-coalescing operators. UTF-8 BOM markers removed from view file headers.

Changes

Dictionary and ViewData access pattern consolidation

Layer / File(s) Summary
Service-side dictionary pattern refactoring
web/Areas/ClinicalScheduler/Controllers/RotationsController.cs, web/Areas/RAPS/Services/RAPSAuditService.cs, web/Areas/RAPS/Services/VMACSExport.cs
RotationsController.BuildWeekScheduleItem uses TryGetValue for instructor person lookups. RAPSAuditService.GetMemberRolesAndPermissionHistory retrieves action history via TryGetValue. VMACSExport.GetExportUsers and GetServerUrl consolidate permission and server URL lookups into single TryGetValue calls. All changes eliminate redundant ContainsKey checks while preserving fallback defaults.
View-side ViewData conditional simplification
web/Areas/RAPS/Views/Members/List.cshtml, web/Areas/RAPS/Views/Members/Roles.cshtml, web/Areas/RAPS/Views/Roles/Members.cshtml, web/Areas/RAPS/Views/Roles/Templates.cshtml
Five Razor view files remove UTF-8 BOM markers from file headers. Conditional rendering logic for permission flags (CanRSOP, canEditRoleMembership, canEditPermissions, canEditRoleTemplates, canApplyTemplates, canViewHistory) simplified from ViewData.ContainsKey(...) && (bool)(ViewData[...] ?? false) to direct (bool)(ViewData["..."] ?? false) checks.

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately describes the main change: replacing inefficient ContainsKey+indexer patterns with TryGetValue across multiple files.
Description check ✅ Passed The description is directly related to the changeset, detailing specific files, line numbers, patterns changed, and test results for the ContainsKey+indexer refactoring.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codeql/8-trygetvalue

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link
Copy Markdown

Bundle Report

Bundle size has no change ✅

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 13, 2026

Codecov Report

❌ Patch coverage is 0% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.96%. Comparing base (38de1ad) to head (3ec2656).

Files with missing lines Patch % Lines
...inicalScheduler/Controllers/RotationsController.cs 0.00% 11 Missing ⚠️
web/Areas/RAPS/Views/Members/List.cshtml 0.00% 4 Missing ⚠️
web/Areas/RAPS/Views/Roles/Templates.cshtml 0.00% 4 Missing ⚠️
web/Areas/RAPS/Services/VMACSExport.cs 0.00% 3 Missing ⚠️
web/Areas/RAPS/Services/RAPSAuditService.cs 0.00% 1 Missing ⚠️
web/Areas/RAPS/Views/Members/Roles.cshtml 0.00% 1 Missing ⚠️
web/Areas/RAPS/Views/Roles/Members.cshtml 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #196      +/-   ##
==========================================
- Coverage   42.96%   42.96%   -0.01%     
==========================================
  Files         877      877              
  Lines       51468    51469       +1     
  Branches     4802     4802              
==========================================
  Hits        22113    22113              
- Misses      28831    28832       +1     
  Partials      524      524              
Flag Coverage Δ
backend 43.04% <0.00%> (-0.01%) ⬇️
frontend 41.34% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rlorenzo
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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.

2 participants