chore: Eliminate nested if statements in GetAbsWeekDaysMonthly/Weekly#896
chore: Eliminate nested if statements in GetAbsWeekDaysMonthly/Weekly#896
Conversation
Extract week number and month match conditions into named boolean variables, then combine with single if statement. No functional changes.
|
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## main #896 +/- ##
=====================================
Coverage 68.5% 68.5%
=====================================
Files 115 115
Lines 4394 4396 +2
Branches 1011 1011
=====================================
+ Hits 3008 3010 +2
Misses 1028 1028
Partials 358 358
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR refactors two methods (GetAbsWeekDaysMonthly and GetAbsWeekDaysWeekly) to eliminate nested if statements by extracting conditional expressions into named boolean variables. While this improves code readability, it introduces a performance consideration by removing the short-circuit evaluation behavior of the original nested structure.
- Extracted week number and month match conditions into
matchesWeekNoandmatchesMonthvariables - Replaced nested if statements with a single combined conditional check
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var matchesWeekNo = byWeekNoNormalized == null || byWeekNoNormalized.Contains(Calendar.GetIso8601WeekOfYear(date, pattern.FirstDayOfWeek)); | ||
| var matchesMonth = pattern.ByMonth.Count == 0 || pattern.ByMonth.Contains(date.Month); | ||
|
|
||
| if (matchesWeekNo && matchesMonth) |
There was a problem hiding this comment.
The refactoring eliminates the short-circuit behavior of the original nested if statements. Both conditions are now evaluated unconditionally on every iteration, even when the first condition fails. This means Calendar.GetIso8601WeekOfYear is called even when byWeekNoNormalized is null, and pattern.ByMonth.Contains is checked even when matchesWeekNo is false. Consider keeping the nested structure or using a single combined if condition without intermediate variables to preserve the original short-circuit evaluation and avoid unnecessary computations in the loop.
| var matchesWeekNo = byWeekNoNormalized == null || byWeekNoNormalized.Contains(Calendar.GetIso8601WeekOfYear(date, pattern.FirstDayOfWeek)); | |
| var matchesMonth = pattern.ByMonth.Count == 0 || pattern.ByMonth.Contains(date.Month); | |
| if (matchesWeekNo && matchesMonth) | |
| if ((byWeekNoNormalized == null || byWeekNoNormalized.Contains(Calendar.GetIso8601WeekOfYear(date, pattern.FirstDayOfWeek))) | |
| && (pattern.ByMonth.Count == 0 || pattern.ByMonth.Contains(date.Month))) |



Extract week number and month match conditions into named boolean variables, then combine with single if statement.
No functional changes.