Measuring complexity of Erlang code?

I’d like to programmatically report on the complexity (probably cyclomatic complexity would be the correct measure here) of our Erlang code. When I search the web for information, I get links to papers, but no actual tooling.

I know that elvis has a warning about the complexity of functions. Has anyone extracted this code, so it can be used to generate a report?

I’ve also seen GitHub - terryyin/lizard: A simple code complexity analyser without caring about the C/C++ header files or Java imports, supports most of the popular languages., but I’m not convinced it’s measuring the Erlang complexity correctly.

Is there anything in ELP (ELP - Erlang Language Platform | ELP) that I could use?

Is there anything I’ve missed?

6 Likes

Seems like something that could live in Erlang -- erl_syntax (used by Erlang -- erl_tidy).

The Elvis warning you refer to - is that elvis_core/doc_rules/elvis_style/dont_repeat_yourself.md at main · inaka/elvis_core · GitHub? (elvis_core/doc_rules/elvis_style/god_modules.md at main · inaka/elvis_core · GitHub and elvis_core/doc_rules/elvis_style/max_function_clause_length.md at main · inaka/elvis_core · GitHub are also potentially relevant)

Yes. The other two checks are also useful.

I was kinda hoping that someone else had turned one of the papers into tooling and made it available, though…

1 Like

Sounds like something @elbrujohalcon might knock up :wink:

You might try Next generation code analysis | CodeScene.
Which supports Erlang out of the box and is free for open source projects (assuming you are willing to give it enough privileges on github).

This is a nice video showing the tool. Using erlang/otp repo in the presentation.
Workshop from CodeScene: Prioritizing Technical Debt as if time and money matters

3 Likes

Thanks for the @mention, @phild .

The actual expert on that part of the code (i.e., the developer that did all the research and actually wrote the code for the rule) is Juan, but I don’t think he has a user on this forum.

Nevertheless, @rlipscombe … That part of elvis_core is not the simplest one to extract. IIRC, it depends quite heavily on elvis_code module, and the dependencies katana-code and zipper. I believe that it might be easier to just copy/re-implement the logic with fewer dependencies somewhere else. In fact, if you’re interested in creating a repository just for that… I would be super-happy to help.

So, I see a few choices for you to achieve your goal:

  1. Use the same dependencies and copy the logic from elvis_style.
  2. Rewrite that logic using fewer dependencies (e.g. working directly with erl_syntax instead of using elvis_code and friends).
  3. Rewrite that logic using more modern and versatile AST tools, like the ones @robertoaloi and his friends use on WhatsApp’s erlfmt, elp, etc…
  4. Find Juan on slack, github, etc… and ask him :slight_smile:

If you need to analyze code that’s already compiled (i.e., with no macros)… #2 is by far the best option.
If you need to analyze source code… then any of the others might work.

PS: Now I have 2 ideas for Spawnfest projects… and 0 Spawnfests this year.

1 Like