Swiftlint: Cyclomatic Complexity Violation: Function should have complexity 10 or less: currently complexity equals 17 (cyclomatic_complexity)

Created on 13 Jun 2016  路  6Comments  路  Source: realm/SwiftLint

I have getting this issue while running app.
I have go through all the forums but i didn't get whats need to done,Please help me to resolve this issue.

question

Most helpful comment

The cyclomatic complexity rule is warning you that there are 17 paths through which your code could go through. There's nothing necessarily _wrong_ with that per se, but it is indicative of a potential code smell, as in "hey, it might be challenging for a human to understand this, and you might want to consider refactoring".

All 6 comments

example? repro case? is this a bug, or are you asking what the rule means? or how to refactor your code?

@jpsim
I need an example to refractor my current code to resolve this warning

it would help if you shared code

@jpsim

This is the function we are using and facing warning.how to refractor this code to remove this warning
_func getContentsList() -> [Sample]? {
let unSortedContentList = SampleList.instanceFromFile("Sample")! as? SampleList

    var contentList: [Sample]?
    if unSortedContentList != nil {
        contentList = [Sample]()
        if unSortedContentList!. Sample != nil {
            for (content) in unSortedContentList!. Sample! {
                contentList?.append(content)
            }
        }
        if unSortedContentList!. Sample != nil {
            for (content) in unSortedContentList!. Sample! {
                contentList?.append(content)
            }
        }
        if unSortedContentList!. Sample != nil {
            for (content) in unSortedContentList!. Sample! {
                contentList?.append(content)
            }
        }
        if unSortedContentList!. Sample != nil {
            for (content) in unSortedContentList!. Sample! {
                contentList?.append(content)
            }
        }
        if unSortedContentList!.test != nil {
            contentList?.append(unSortedContentList!. test!)
        }
        if unSortedContentList!. test != nil {
            contentList?.append(unSortedContentList!. test!)
        }
        if unSortedContentList!. test != nil {
            for (content) in unSortedContentList!. test! {
                contentList?.append(content)
            }
        }
    }

    return contentList
}_

The cyclomatic complexity rule is warning you that there are 17 paths through which your code could go through. There's nothing necessarily _wrong_ with that per se, but it is indicative of a potential code smell, as in "hey, it might be challenging for a human to understand this, and you might want to consider refactoring".

Closing this due to lack of activity. Feel free to reopen if you have any other questions!

Was this page helpful?
0 / 5 - 0 ratings