There are metrics for class and module cohesion and coupling, but I didn't find anything yet for a single function. Some functions are obviously doing too much, like doThisAndThat (should be doThat(doThis())) or getDataForThis (should be convertForThis(getData())), and it would be very nice if there were an automatic way to detect such cases, to increase composability and testability of functions, which in turn will increase code reuse. Maybe this is the same as a metric for potential code reuse in the code-base?

  • 1
    What you're looking at is how well decomposed the algorithm is. doThisAndThat() is decomposed into doThat() doThis() which is good. This relates to "Stepwise Refinement" which was a big topic in the 70s (?). Here is a link web.stanford.edu/class/cs106j/lectures/03-Stepwise-Refinement/…. The metric for this can be calculated depending on what language you use, for example cflow can help see how functions are connected using a flow chart. gnu.org/software/cflow
    – r0k1m
    May 31, 2021 at 10:35

1 Answer 1


Cohesion inside a single function might simply be called code duplication.

There's also algorithms for automatic procedure extraction. Here's one thesis about it: https://research.cs.wisc.edu/areas/pl/theses/raghavan.thesis.pdf

One could say that a function has "cohesion" if the procedure extraction algorithm is unable to extract anything from it further (give and take the tuning of that algorithm).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.