From 5bc5d17032cc0f8d09284b2827bba93aff05e630 Mon Sep 17 00:00:00 2001 From: M57 <41645758+Hy4ri@users.noreply.github.com> Date: Thu, 9 Jul 2026 15:26:29 +0300 Subject: [PATCH] Refactor parseDaysOfWeek in CronExpressionFormatter (#510) Refactored the deeply nested `parseDaysOfWeek` function using guard clauses to flatten the logic structure. By returning early on invalid conditions (`dowPart == "*" || domPart != "*" || monthPart != "*"` and `hoursList.size != 1 || minutesList.size != 1`), the remaining function body's indentation is reduced by two levels. This improves overall code readability and maintainability without altering existing functionality. The change was verified against existing unit tests, which all pass. --- .../util/CronExpressionFormatter.kt | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/m57/hermescontrol/util/CronExpressionFormatter.kt b/app/src/main/java/com/m57/hermescontrol/util/CronExpressionFormatter.kt index 35916de6..aea00697 100644 --- a/app/src/main/java/com/m57/hermescontrol/util/CronExpressionFormatter.kt +++ b/app/src/main/java/com/m57/hermescontrol/util/CronExpressionFormatter.kt @@ -115,28 +115,30 @@ object CronExpressionFormatter { hoursList: List, minutesList: List, ): String? { - if (dowPart != "*" && domPart == "*" && monthPart == "*") { - if (hoursList.size == 1 && minutesList.size == 1) { - val timeStr = formatTime(hoursList[0], minutesList[0]) - if (dowPart.contains("-")) { - val bounds = dowPart.split("-") - if (bounds.size == 2) { - val start = bounds[0].toIntOrNull() ?: -1 - val end = bounds[1].toIntOrNull() ?: -1 - if (start in 0..7 && end in 0..7) { - val startName = dowNames[start] - val endName = dowNames[end] - return "At $timeStr $startName through $endName" - } - } - } - val dows = parseField(dowPart, 0, 7) - if (dows.size == 1) { - val dowName = dowNames[dows[0]] - return "Every $dowName at $timeStr" + if (dowPart == "*" || domPart != "*" || monthPart != "*") return null + if (hoursList.size != 1 || minutesList.size != 1) return null + + val timeStr = formatTime(hoursList[0], minutesList[0]) + + if (dowPart.contains("-")) { + val bounds = dowPart.split("-") + if (bounds.size == 2) { + val start = bounds[0].toIntOrNull() ?: -1 + val end = bounds[1].toIntOrNull() ?: -1 + if (start in 0..7 && end in 0..7) { + val startName = dowNames[start] + val endName = dowNames[end] + return "At $timeStr $startName through $endName" } } } + + val dows = parseField(dowPart, 0, 7) + if (dows.size == 1) { + val dowName = dowNames[dows[0]] + return "Every $dowName at $timeStr" + } + return null }