How to Calculate Cyclomatic Complexity for R Functions

How do you calculate cyclomatic complexity for R functions?

Also, I just found a new package called cyclocomp (released 2016). Check it out!

Calculate Cyclomatic complex when it comes to a recursion function

See https://en.m.wikipedia.org/wiki/Cyclomatic_complexity. Recursion by definition does not impact cyclomatic complexity and the tools measuring it also do not actually 'run' the code and measure it. This is because the original definition of McCabe states that CC is the number of possible path that the code can take, and that's about it. If you are still not convinced , just give it a different name, say recursive cylcomatic complexity, and measure it, by counting the actual number of invocations at runtime. Refer: https://www.guru99.com/cyclomatic-complexity.html

How to calculate cyclomatic complexity of a Clojure function?

Macros are an abstraction and should not contributed to the CC calculation, any more than a function call would.

That said, I don't think that CC is particularly interesting for Clojure. I would be more interested in something that measured overuse of mutability.

What is the highest Cyclomatic Complexity of any function you maintain? And how would you go about refactoring it?

This is kid stuff compared to some 1970s vintage COBOL I worked on some years ago. We used the original McCabe tool to graphically display the CC for some of the code. The print out was pure black because the lines showing the functional paths were so densely packed and spaghetti-like. I don't have a figure but it had to be way higher than 171.

What to do

Per Code Complete (first edition):

If the score is:

  • 0-5 - the routine is probably fine
  • 6-10 - start to think about ways to simplify the routine
  • 10+ - break part of the routine into a second routine and call it from the first routine

Might be a good idea to write unit tests as you break up the original routine.



Related Topics



Leave a reply



Submit