fzipp / gocyclo

Calculate cyclomatic complexities of functions in Go source code.
BSD 3-Clause "New" or "Revised" License
1.33k stars 81 forks source link

Change rules of cyclomatic complexity calculation #5

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hello, @fzipp.

I think highly nested construction should not give the same amount of points as the first-level one. Flat code is less complex than nested, isn't it? For illustration, here is a code fragment that has complexity of 11:

package zzz

func smth(obj interface{}) {
    switch obj.(type) {
    case int:
        ProcessInt(obj)
    case int8:
        // ..
    case int16:
    case int32:
    case int64:
    case uint:
    case uint8:
    case uint16:
    case uint32:
    case uint64:
    }
}

Pretty simple and straight-forward function, isn't it? Now here is another one that is "less complex" (level is 10):

package xxx

func smth(i int) {
    if i < 1 {
        if i < 2 {
            if i < 3 {
                if i < 4 {
                    if i < 5 {
                        if i < 6 {
                        } else {
                        }
                    } else {
                    }
                } else {
                    if i < 5 {
                    } else {
                    }
                }
            } else {
                if i < 4 {
                    if i < 5 {
                    } else {
                    }
                } else {
                }
            }
        } else {
        }
    } else {
    }
}

For me, personally, this doesn't look like a less complex function. What do you think?

My opinion is complexity should grow geometrically with every level of nesting. But requirements for flat code may be loosed up.

weberc2 commented 8 years ago

Cyclomatic complexity is an established concept; it is concerned with branches, and it doesn't know how they're represented textually in a language. https://en.wikipedia.org/wiki/Cyclomatic_complexity

ghost commented 8 years ago

@weberc2 OK, I see. Though, usefulness of this metrics looks questionable to me. Somebody will definitely have to implement a tool that just enforces flat code, nothing more and nothing less.

flyingmutant commented 8 years ago

It may be a good idea to reopen this issue. As it stands, the metric calculated by gocyclo is a) of highly questionable value and b) unrelated to cyclomatic complexity.

nodirt commented 8 years ago

Yep. The project claims to compute cyclomatic complexity, but in fact it does not