Skip to main content

Configuring Metrics

Metrics are one of the core building blocks of DCM. Metrics help you understand the complexity of your code and identify areas that may be difficult to maintain or test.

All metrics are configurable.

Enabling a Metric

To enable a metric add its id and the threshold to the metrics entry:

analysis_options.yaml
dcm:
metrics:
cyclomatic-complexity:
threshold: 20
info

To simply collect metric values without getting metric violations (for example, for DCM Dashboards), you can set the threshold to unset.

analysis_options.yaml
dcm:
metrics:
cyclomatic-complexity:
threshold: unset

Note that by default, metric violation below the threshold are not reported. To view all metric values in your console, use the --report-all flag.

Setting Multiple Thresholds

To set multiple thresholds for a particular metrics, set entries:

analysis_options.yaml
dcm:
metrics:
cyclomatic-complexity:
threshold: 20
entries:
- threshold: 10
paths:
- '.*some_folder.*'
- '.*some_file.dart'
- threshold: unset
paths:
- '.*another_folder'

Each entry expects a threshold and a list of paths (which are regular expressions).

info

Configuring the main threshold is required as that threshold will be used for file that are not covered by the entries config (if no threshold is set, the default threshold will be used instead).

Understanding Metric Thresholds

The threshold is a value after which a calculated metric value is marked as a violation.

Some metric thresholds can be any positive number (for example, 20 or 200), while others can only be a number between 0 and 1.

Additionally, some metric threshold are inverted (meaning, the lower the calculated value, the higher the severity level of the violation). For example, weight-of-class takes a threshold value from 0 and 1, and with the configured threshold of 0.33, a calculated value of 0.1 will be marked as a Very High level of violation.

Each calculated metric value falls into one of four violation levels (based on the configured threshold) - Very High, High, Near, and Below.

Violation levelRegular thresholdInverted threshold
Very High> 200%< 50%
High100% - 200%50% - 100%
Near80% - 100%100% - 125%
Below< 80%> 125%
note

All % values are relative to the configured threshold.

For example, if the cyclomatic complexity of a method is 30 (and the threshold is 20), the value falls into the High level. And all values above 40 will be marked as Very High.

Disabling Metric Ignores

To disable ignore comments for a particular metric, set ignorable: false:

analysis_options.yaml
dcm:
metrics:
cyclomatic-complexity:
threshold: 20
ignorable: false

Customizing Estimated Effort

To change the estimated effort for a particular metric, set effort (in minutes):

analysis_options.yaml
dcm:
metrics:
cyclomatic-complexity:
threshold: 20
effort: 10
info

If you are setting custom estimated effort, don't forget to set threshold.

Excluding Files for All Metrics

To exclude files from a metrics report, provide a list of glob patterns for ignored files:

analysis_options.yaml
dcm:
exclude:
metrics:
- test/**
- lib/src/some_file.dart