2

For example, let's assume we have a class that is imported three times from other classes. This would lead to a reusability of three. However, as there might be functions that are called only once, it would make more sense to count the called functions.

One general suggestion from measuring code reusability is to answer the following question: "How many functions are called from more than one place?"

What would be the best way to realise the solution, e.g. counting the functions that are called in other parts of the code with Python?

6
  • What you are describing sounds like measuring of coupling to me. The higher your "reusability" count, the more tightly coupled the code is. That fits neatly into the idea that "DRY is the enemy of loose coupling". Not sure if that suggests that you aren't seeking to measure re-use very well, or if the idea of re-use is fundamentally flawed as it inevitably leads to tight coupling.
    – David Arno
    Jan 11 '18 at 10:01
  • Some questions: (i) why you wanna do that? (ii) can't you use some profiling tool in order to achieve that? (iii) do you need this for your entire codebase or for some specific class only? Jan 11 '18 at 10:12
  • 1
    @EmersonCardoso The advice to use the profiling tool solved the issue. I found the following two profiler that print out all the function called, with the time spend in each and the number of times they have been called. python -m cProfile -s cumtime file.py or python -m trace --count -C --file --summary dir/*.py. Both can be used for a specific class/file and for the entire code base. I want to meassure how often a package/code is used by different/decoupled project
    – Rene B.
    Jan 11 '18 at 11:02
  • Nice to hear it solved then :); I'll put the comment as an answer so you can accept it. Jan 11 '18 at 11:57
  • Looks like you want to use a code metrics tool that checks the class coupling.
    – Carra
    Jan 11 '18 at 12:36
2

Use some profiling tool/framework in order to see what functions / modules have been reached from your code.

Examples for Python are:

  • python -m cProfile -s cumtime file.py or
  • python -m trace --count -C --file --summary dir/*.py

The alternative would be to use your own logging in order to determine places reached during your code's execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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