Swiftlint: cyclomatic_complexity fails on simple code.

Created on 1 Feb 2016  路  10Comments  路  Source: realm/SwiftLint

I haven't checked how this rule judges complexity, but it fails on non-complex code even functions with just linear statements.

question

Most helpful comment

@JohnEstropia alternatively, you can disable this particular rule for referenced method only (put this comment above method):
// swiftlint:disable:next cyclomatic_complexity

In general, switch statements are "complex" in terms of CC (taking into account issue @jpsim pointed out)

All 10 comments

Example?

It seems to be sensitive to switch-cases and optional bindings.

class func colorFromCode(code: String?) -> UIColor {
        switch code! {
        case "000": return Cache.couplesColor000
        case "001": return // ...
        case "002": return // ...
        case "003": return // ...
        case "004": return // ...
        case "005": return // ...
        case "006": return // ...
        case "007": return // ...
        case "008": return // ...
        case "009": return // ...
        default: return UIColor.clearColor()
        }
    }

Above code gets complexity 13.

It would help if a summary of what this rule checks for is written somewhere. (each rule's description seem to have disappeared from swiftlint rules)

We have disabled this rule for now as it doesn't seem to be practical.

The complexity computed above doesn't look correct to me: I count 11 paths rather than 13. But that would still exceed the limit of 10 enforced by the rule, which seems to be what you're disputing.

So I opened #461 to track the actual issue with the incorrect measurement.

But if you'd rather not enable cyclomatic complexity rule, you can disable it as you have, or increase its configuration setting in your configuration file:

cyclomatic_complexity: 15

@JohnEstropia alternatively, you can disable this particular rule for referenced method only (put this comment above method):
// swiftlint:disable:next cyclomatic_complexity

In general, switch statements are "complex" in terms of CC (taking into account issue @jpsim pointed out)

The CC measurement issues reported here were fixed in #463, thanks for reporting @JohnEstropia!

disabling doesn't work for me - am I missing something?

image

Works for me:

$ cat cc.swift 
// swiftlint:disable:next cyclomatic_complexity
func colorFromCode(code: String?) -> UIColor {
    switch code! {
    case "000": return Cache.couplesColor000
    case "001": return // ...
    case "002": return // ...
    case "003": return // ...
    case "004": return // ...
    case "005": return // ...
    case "006": return // ...
    case "007": return // ...
    case "008": return // ...
    case "009": return // ...
    default: return UIColor.clearColor()
    }
}
$ swiftlint lint --path cc.swift
Linting Swift files at path cc.swift
Linting 'cc.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.
$ tail -n +2 cc.swift > cc2.swift # remove first line
$ swiftlint lint --path cc2.swift
Linting Swift files at path cc2.swift
Linting 'cc2.swift' (1/1)
cc2.swift:1:1: warning: Cyclomatic Complexity Violation: Function should have complexity 10 or less: currently complexity equals 11 (cyclomatic_complexity)
Done linting! Found 1 violation, 0 serious in 1 file.

@haemi please make sure you're on the latest version of SwiftLint and open a new issue with steps to reproduce if you're still having problems.

for me it doesn't work when running from within Xcode as a build phase; from Terminal, everything's fine...

That's weird because SwiftLint doesn't execute different executables (or generally even code paths) based on whether or not you're running from Terminal or a build phase.

But nonetheless, I'd appreciate if you could open a new issue with steps to reproduce if you're still having problems.

In terminal

cd `ROOT_PROJECT`
open .swiftlint.yml

Change value of cyclomatic_complexity to 20
-> warning dissapear.

Was this page helpful?
0 / 5 - 0 ratings