-
-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
Status
Proposed
Context
Code scanning tools (CodeQL, Copilot) frequently suggest converting foreach loops with if conditions to LINQ .Where() chains. This creates PR review noise and doesn't improve correctness.
Decision
Prefer explicit foreach loops over LINQ .Where()/.Select() chains for filtering.
Rationale
- Readability: Explicit loops are clearer, especially for complex filtering logic
- Debuggability: Easier to step through and inspect variables
- Maintainability: Easier to add logging, error handling, or additional logic
- Team preference: Confirmed by codebase owner
Exceptions
LINQ is acceptable for:
- Simple one-liners:
items.Where(x => x.IsActive).ToList() - Projection:
items.Select(x => x.Name).ToList() - Method chains: When LINQ reads more naturally (OrderBy, GroupBy, etc.)
Consequences
- Code scanning tools may flag foreach+if patterns
- These warnings should be dismissed as style preference
- See chore: Tune code scanning tools to reduce noise #231 for code scanning tuning
Examples
// Preferred
foreach (var item in items)
{
if (item.IsActive)
{
ProcessItem(item);
}
}
// Avoid (unless simple one-liner)
foreach (var item in items.Where(x => x.IsActive))
{
ProcessItem(item);
}Generated with Claude Code
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Projects
Status
Done