rubyrubocopcyclomatic-complexity

Rubocop cyclomatic complexity of top-level code


Is there a reason why rubocop excludes top-level code when evaluating cyclomatic complexity? It only returns complexity for code independent paths through a method. Is there a way to include top-level code in this analysis?


Solution

  • Most complexity measures use def as a scope.

    It is assumed that code at top-level is meant to run only when the app/library is loaded and that all the actual code is within method definitions.

    A file that does 30 require_relative would bust most metrics, but it is not considered an issue.

    Consider moving the actual code you are running in a method and calling it from the top level directly as first improvement.