Do loops add cyclomatic complexity?

I have been asking folks if loops add to cc. I think they’ve been telling me no pretty consistently, and I’m confused about that. I can see a Ruby  3.times do  wouldn’t add complexity. But doesn’t a java for loop contintually check whether the iterator is or isn’t bigger than some value? Why wouldn’t that count as a conditional?

Maybe none of this matters. Maybe if I take care of duplication, expressivness, responsibility, nobody cares. But dang it, I care. ^__^

GeePawHill sez…

Good lord, how am *I* supposed to know?

Actually, as far as I can tell, the loop itself inherently only creates one forking path extra. The path is identified by the condition while the loop is run vs. the condition when it is stops running.

8 thoughts on “Do loops add cyclomatic complexity?

  1. Evan Cofsky

    Loops add to the cyclomatic complexity the number of independent paths they add to the code. A simple loop would add one path, a loop with an internal conditional would add one path for the loop, and one for each internal condition, etc.

    Reply
  2. Pingback: Do loops add cyclomatic complexity?

  3. Dan Wiebe


    while count > 0
    stuff
    count = count - 1
    end

    adds complexity, because there’s a possible path through the code where stuff gets executed and a possible path where it doesn’t. Both paths should be tested.


    3.times do
    stuff
    end

    does not add complexity, because stuff is always executed by every possible path through the code.

    And yes, somewhere deep in the bowels of .times, there’s an if condition that checks to see whether the loop index is 3 yet (or maybe 0 yet, depending on implementation), but both branches of that hidden if are always executed; hence you can’t test one branch without testing the other as well, so you only need the one test.

    Reply
  4. Dan Wiebe

    Two further points.

    First, you shouldn’t depend on static analyzers to always be able to tell the difference between the two kinds of loops above. Sometimes the information necessary to distinguish will be available only at runtime. Trust your own nose.

    Second, carefully measuring the complexity of loops is like carefully weighing out green vegetables on your diet. You don’t get fat from eating too many green vegetables, and your program doesn’t get fat because of too many loops. It’s the carbohydrates and the conditionals.

    Reply
  5. Pingback: Tweets that mention Do loops add cyclomatic complexity? at My Agile Education -- Topsy.com

  6. Jon Kern

    Cyclomatic Complexity merely counts the number of possible paths through a routine; with more paths implying greater complexity (which stands to reason). A loop is a new path as there is often some sort of logic required to determine if an exit is warranted (yet) or not.

    The worse offender is the case statement… One time (circa 1990?) I was doing code for a bit-compacting algorithm (32-bit case statements). CC went through the roof. That’s why, many metric tools (at least the one we built at TogetherSoft) allow you to turn off counting complexity for a case statement.

    Reply
  7. Cindy

    Awesome!!! Finally got an answer to my quieostn. I have been trying to make this heart motif and it calls for tr4tog and dtr5tog. I was totally stumped! Thanks for the help! I’m so excited, I can now make my heart!Lena from So. California

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>