Thread overview
cogito, cognitive complexity for D
May 17, 2022
Eugen Wissner
May 24, 2022
Christian Köstlin
May 25, 2022
Bastiaan Veelo
May 25, 2022
Christian Köstlin
May 27, 2022
Eugen Wissner
May 17, 2022
Glad to announce that Funkwerk releases cogito [1], a tool that measures cognitive complexity of D code.

>From the Sonar’s paper on cognitive complexity [2]:

“[…], Cognitive Complexity has been formulated to address modern language structures, and to produce values that are meaningful at the class and application levels. More importantly, it departs from the practice of evaluating code based on mathematical models so that it can yield assessments of control flow that correspond to programmers’ intuitions about the mental, or cognitive effort required to understand those flows.”

cogito takes advantage of the DMD frontend (2.098.1 currently) to analyze the D code. It was our first experience using the DMD frontend for an internal tool.

Build and usage instructions are in the README. A pre-built version for Linux x86-64 can be found in release attachments. You can build it yourself with `make install && make build/release/bin/cogito`. There is also a Dockerfile that can be used to run cogito, alternatively the binary can be copied from the resulting Docker container and run on the host system.

cogito reports the cognitive complexity of functions, aggregates and modules. There are options to set a limit, so cogito fails if some score is too high. The options are --threshold, --aggregate-threshold and --module-threshold, respectively. On failure only the problematic units are printed.

[1] https://github.com/funkwerk/cogito
[2] https://www.sonarsource.com/docs/CognitiveComplexity.pdf
May 24, 2022

On 2022-05-17 12:50, Eugen Wissner wrote:

>

Glad to announce that Funkwerk releases cogito [1], a tool that measures
cognitive complexity of D code.

Thanks for sharing this tool.
I tried it on OSX by building with this script instead of your Makefile as I am managing my dlang version with https://dlang.org/install.html:

#!/bin/bash -xe

# select dmd sources (please run ~/dlang/install.sh dmd-2.098.1 manually)
DMD_SOURCES=~/dlang/dmd-2.098.1/src

# prepare filesystem
rm -f tools/dmd2/src
mkdir -p tools/dmd2
ln -s $DMD_SOURCES tools/dmd2/src

mkdir -p build

# activate compiler (please run ~/dlang/install.sh ldc-1.29.0 manually)
source ~/dlang/ldc-1.29.0/activate


# build dmd-lib
ldmd2 -debug -lib -version=MARS -version=NoMain -J=./include -J=$DMD_SOURCES/dmd/dmd/res -I$DMD_SOURCES/dmd -od=build -of=dmd.a $DMD_SOURCES/dmd/dmd/*.d $DMD_SOURCES/dmd/dmd/*/*.d

# and cognito
dub build --build=release --config=executable

and I am wondering what real world "numbers" are. Is e.g. 20 a "good" or "bad" number?

Another question that comes to mind is, if the dmd frontend could be made available as a dub package, which would simplify the build process?

Kind regards,
Christian

May 25, 2022

On Tuesday, 24 May 2022 at 21:25:38 UTC, Christian Köstlin wrote:

>

if the dmd frontend could be made available as a dub package,

It already is, as sub package of https://code.dlang.org/packages/dmd

— Bastiaan.

May 25, 2022

On 2022-05-25 16:43, Bastiaan Veelo wrote:

>

On Tuesday, 24 May 2022 at 21:25:38 UTC, Christian Köstlin wrote:

>

if the dmd frontend could be made available as a dub package,

It already is, as sub package of https://code.dlang.org/packages/dmd

— Bastiaan.

Nice,

so if the code would be ported from 2.098.1 to 2.100.0 the dub dependency could be used perhaps ...

May 27, 2022
On Tue, May 24, 2022 at 11:25:38PM +0200, Christian Köstlin via Digitalmars-d-announce wrote:
> and I am wondering what real world "numbers" are. Is e.g. 20 a "good" or "bad" number?

Module thresholds aren't useful for everyone, D modules can be pretty large.

I see different values used for aggregate and method thresholds.

- PHP's simplify (set of tools) suggests 8 and 50
  (https://github.com/symplify/phpstan-rules/blob/de025c98988a85d0345b376408630e84b585cd20/packages/cognitive-complexity/config/cognitive-complexity-rules.neon)
- If I remeber correctly, Sonar Qube itself uses only the method
  cognitive complexity and the maximum is 15.
- “Genese Cpx” has only method complexity but has a warning and error
  thresholds of 5 and 10, respectivly
  (https://www.npmjs.com/package/@genese/complexity).

> Another question that comes to mind is, if the dmd frontend could be made available as a dub package, which would simplify the build process?

I'll try to build with dub's package. I've only seen a fork of the dmd frontend as dub package, but should have missed the dmd package itself.