Cyclomatic Complexity Violation: Function should have complexity 10 or less: currently complexity equals 13 (cyclomatic_complexity)
The method is too complex. But instead of rewriting the code, you could exclude switches
from the cyclomatic_complexity
calculation (since they are perfectly readable) like this:
cyclomatic_complexity:
ignores_case_statements: true
The warning occurs because your function is too complex as defined by the metric which essentially counts the number of decisions that need to be made.
A simple way to avoid it in this particular case would be with some simple math:
func selectedMenuInLoggedOutState(sender: UIButton) {
guard let menu = LeftGuestMenu(rawValue: sender.tag - 1) else { return }
self.changeGuestViewController(menu)
}
You can disable swiftlint warnings in code like this:
// swiftlint:disable cyclomatic_complexity
func selectedMenuInLoggedOutState(sender: UIButton) {
...
}
// swiftlint:enable cyclomatic_complexity
or, if you want to explicitly just disable the next swiftlint warning:
// swiftlint:disable:next cyclomatic_complexity
func selectedMenuInLoggedOutState(sender: UIButton) {