‹ BackHN Continuity

Thread

Cyclomatic Complexity in C#

87 points · 29 comments · gone35

  1. bunderbunder · · focus · HN ↗
    Overall cyclomatic complexity is a useful metric, but it does have one shortcoming when used with modern languages: it was invented before polymorphism really became a thing.

    That means that it really only counts explicit branching. So, for example, in an OO language like C#, calling a virtual method doesn’t increment cyclomatic complexity even though the method invocation could go down many code paths. Potentially thousands if you’re dealing with a common interface like IEnumerable. If you’re working on a library then the number of potential code paths in this kind of situation is unbounded.

    As an aside, it’s interesting to think how it might apply to a language like Smalltalk that doesn’t even have if or switch statements.

    OO isn’t the only monkey wrench, either. Higher-order functions also introduce forms of branching that cyclomatic complexity doesn’t measure.

    Again that doesn’t make it a useless metric. Just don’t think that a cyclomatic complexity limit in your codebase is some sort of maintainability panacea. Some of the least comprehensible functions I’ve deciphered had quite low cyclomatic complexities.

    1. pipes · · focus · HN ↗
      I'm unsure if I have chain together something like

      theThing() .map(someLamda) .filter(someLamda) .reduce(someLamda)....

      Have I actually reduced complexity? I certainly prefer this over loops and if statements but does the pipeline and lamdas count as reducing cyclomatic complexity?

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.