Skip to content

Commit b9fbdb9

Browse files
committed
refactor: Extract magic numbers and use switch statements
Code quality improvements based on code review: - Add SpeedLimit enum with named constants for intervals and CPS limits - minInterval (0.01s = 100 CPS) - maxInterval (300s = 1 click per 5 min) - safeGuardInterval (0.001s to prevent division by zero) - Add SpeedThreshold enum with named constants for speed tiers - CPS thresholds for formatting (high/medium/low) - CPS thresholds for descriptions (veryFast/fast/normal) - Interval thresholds for slow speeds (slow/verySlow) - Refactor formatClickSpeed() to use switch statement - Replace if/else chain with pattern matching - More idiomatic Swift code - Refactor describeClickSpeed() to use nested switch statements - Primary switch on CPS for fast speeds - Secondary switch on interval for slow speeds - Eliminates all magic numbers Benefits: - Self-documenting code with clear intent - Easier to maintain and adjust thresholds - More idiomatic Swift patterns - Better readability
1 parent a109e20 commit b9fbdb9

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

Sources/ClickIt/Lite/SimplifiedMainView.swift

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ import SwiftUI
99

1010
struct SimplifiedMainView: View {
1111

12+
// MARK: - Speed Configuration
13+
14+
/// Speed limits for click intervals
15+
private enum SpeedLimit {
16+
static let minInterval: Double = 0.01 // 100 CPS
17+
static let maxInterval: Double = 300.0 // 1 click per 5 min
18+
static var minCPS: Double { 1.0 / maxInterval }
19+
static var maxCPS: Double { 1.0 / minInterval }
20+
static let safeGuardInterval: Double = 0.001 // Prevents division by zero
21+
}
22+
23+
/// Thresholds for speed descriptions and formatting
24+
private enum SpeedThreshold {
25+
// CPS thresholds for formatting
26+
static let highCPS: Double = 10.0
27+
static let mediumCPS: Double = 1.0
28+
static let lowCPS: Double = 0.1
29+
30+
// CPS thresholds for descriptions
31+
static let veryFastCPS: Double = 50.0
32+
static let fastCPS: Double = 10.0
33+
static let normalCPS: Double = 1.0
34+
35+
// Interval thresholds for descriptions (seconds)
36+
static let slowInterval: Double = 60.0
37+
static let verySlowInterval: Double = 120.0
38+
}
39+
1240
// MARK: - Properties
1341

1442
@StateObject private var viewModel = SimpleViewModel()
@@ -178,18 +206,18 @@ struct SimplifiedMainView: View {
178206
value: Binding(
179207
get: {
180208
// Convert interval to log(CPS)
181-
let cps = 1.0 / max(viewModel.clickInterval, 0.001)
209+
let cps = 1.0 / max(viewModel.clickInterval, SpeedLimit.safeGuardInterval)
182210
return log10(cps)
183211
},
184212
set: { logCPS in
185213
// Convert log(CPS) back to interval
186214
let cps = pow(10.0, logCPS)
187215
let interval = 1.0 / cps
188-
// Clamp to valid range (0.01s to 300s)
189-
viewModel.clickInterval = min(max(interval, 0.01), 300.0)
216+
// Clamp to valid range
217+
viewModel.clickInterval = min(max(interval, SpeedLimit.minInterval), SpeedLimit.maxInterval)
190218
}
191219
),
192-
in: log10(1.0/300.0)...log10(100.0) // log(0.00333) to log(100)
220+
in: log10(SpeedLimit.minCPS)...log10(SpeedLimit.maxCPS)
193221
)
194222
.disabled(viewModel.isRunning)
195223

@@ -217,35 +245,49 @@ struct SimplifiedMainView: View {
217245
/// Format click speed for display
218246
private func formatClickSpeed(interval: Double) -> String {
219247
let cps = 1.0 / interval
220-
221-
if cps >= 10 {
222-
return String(format: "%.0f CPS", cps)
223-
} else if cps >= 1 {
224-
return String(format: "%.1f CPS", cps)
225-
} else if cps >= 0.1 {
226-
return String(format: "%.2f CPS", cps)
227-
} else {
228-
return String(format: "%.3f CPS", cps)
248+
let format: String
249+
250+
switch cps {
251+
case SpeedThreshold.highCPS...:
252+
format = "%.0f CPS"
253+
case SpeedThreshold.mediumCPS..<SpeedThreshold.highCPS:
254+
format = "%.1f CPS"
255+
case SpeedThreshold.lowCPS..<SpeedThreshold.mediumCPS:
256+
format = "%.2f CPS"
257+
default:
258+
format = "%.3f CPS"
229259
}
260+
261+
return String(format: format, cps)
230262
}
231263

232264
/// Describe click speed in human-readable terms
233265
private func describeClickSpeed(interval: Double) -> String {
234266
let cps = 1.0 / interval
235267

236-
if cps >= 50 {
268+
switch cps {
269+
case SpeedThreshold.veryFastCPS...:
237270
return "⚡ Very Fast - \(String(format: "%.1f", interval * 1000))ms per click"
238-
} else if cps >= 10 {
271+
272+
case SpeedThreshold.fastCPS..<SpeedThreshold.veryFastCPS:
239273
return "🚀 Fast - \(String(format: "%.2f", interval))s per click"
240-
} else if cps >= 1 {
274+
275+
case SpeedThreshold.normalCPS..<SpeedThreshold.fastCPS:
241276
return "⏱️ Normal - 1 click every \(String(format: "%.1f", interval))s"
242-
} else if interval < 60 {
243-
return "🐌 Slow - 1 click every \(String(format: "%.1f", interval))s"
244-
} else if interval < 120 {
245-
return "🐢 Very Slow - 1 click every \(Int(interval))s (~\(Int(interval/60)) min)"
246-
} else {
247-
let minutes = Int(interval / 60)
248-
return "🦥 Ultra Slow - 1 click every \(minutes) minutes"
277+
278+
default:
279+
// For CPS < 1.0, describe by interval instead
280+
switch interval {
281+
case ..<SpeedThreshold.slowInterval:
282+
return "🐌 Slow - 1 click every \(String(format: "%.1f", interval))s"
283+
284+
case SpeedThreshold.slowInterval..<SpeedThreshold.verySlowInterval:
285+
return "🐢 Very Slow - 1 click every \(Int(interval))s (~\(Int(interval/60)) min)"
286+
287+
default:
288+
let minutes = Int(interval / 60)
289+
return "🦥 Ultra Slow - 1 click every \(minutes) minutes"
290+
}
249291
}
250292
}
251293

0 commit comments

Comments
 (0)