Metrics
A metric is a number assay computes from source for one function, class or file. assay has seven. Six have a limit rule: a value over its limit is a violation of that rule, which check and gate report. assay metrics lists every value, judges nothing, and exits with 0 whenever it could run.
assay metrics # the whole project, on the consoleassay metrics lib/src # the files at and below a pathassay metrics --reporter json:build/metrics.jsonassay metrics --reporter html:build/metrics.htmlassay metrics --coverage coverage/lcov.info # with the line coverage a test run wroteThe report also gives every file its code lines and, where the cross-file check duplicated_code is on (it is under every preset), the duplicated code of the files under lib/. For every option, output form and exit code, see assay metrics.
The seven metrics
Section titled “The seven metrics”| Metric | Measured for | Limit rule |
|---|---|---|
| Cyclomatic complexity | function | max_cyclomatic_complexity |
| Cognitive complexity | function | max_cognitive_complexity, in strict only |
| Function length | function | max_function_length |
| Nesting depth | function | max_nesting_depth |
| Parameter count | function | max_parameters |
| Methods per class | class, mixin, extension, enum, extension type | none: reported only |
| File length | file | max_file_length |
The page of each metric says how it is counted, works out an example, and gives its limit under each preset.
A function is a top-level function, a method, a getter, a setter, an operator, a constructor with a body, a local function or a closure. A local function or closure is measured on its own and adds nothing to the function around it, except for function length.
The limit rules compute their values with the same code, and each rule page gives examples. max_widget_nesting has a count of its own and is not one of the seven metrics.
Files and limits
Section titled “Files and limits”- The files measured are the files
checkjudges: every Dart file thatassay.excludeandanalyzer.excludedo not cover, without the tool directories and the directories directly under the root whose name starts with a dot (see Which files are judged). - A file is not measured, and is listed with its compile errors, when it does not compile, cannot be read, or a rule that is on throws on it (
rule_failed):checkdoes not judge such a file either. - The limit of a metric is the
maxof its limit rule as the configuration sets it (Configuring metrics). There is none for methods per class, for a limit rule that is switched off or that the rule map does not switch on (cognitive complexity underrecommendedandcore), and for a limit rule with no number. - Where a limit rule is silent, the metric has no limit. The presets give
max_function_lengthandmax_file_lengththe excludetest/**,integration_test/**,test_driver/**, so a test file’s longmainis listed with its value and markedno limit here.
Over the limit, suppressed, taken over
Section titled “Over the limit, suppressed, taken over”A value over its limit stays over it whatever check makes of it. The report says which of three it is:
- reported:
checkreports it as a violation of the limit rule; - suppressed: a suppression hides that violation (
// ignore: max_parameters, with or without the prefixassay/, or a blanket form); - taken over: the violation of another rule at the same function takes it over, so that the function carries one violation. Under assay’s rules,
max_cognitive_complexitytakes overmax_cyclomatic_complexityandmax_nesting_depth: where a function’s cognitive complexity is over its limit, its cyclomatic complexity and its nesting depth, where they are over theirs, are taken over by it.
The takeover comes before any suppression. A value taken over is never suppressed, and a suppression of the cognitive violation leaves what it took over taken over. Where the cognitive limit is not in effect in the file (the rule is off, its exclude covers the file, or it has no number), nothing is taken over.
Under strict, a function over the cognitive and the nesting limits reads, in the box of nesting depth:
╭─ Nesting depth ──────────────────────────── limit 3 · 2 over · 16 functions ─╮│ Value File Line Name ││ ───────────────────────────────────────────────────────────── ││ ! 4 lib/over_limits.dart 13:5 deeplyNested ││ ! 4 lib/over_limits.dart 99:5 nestedCount taken over ││ 2 lib/measured.dart 11:7 Cart.total ││ 1 lib/over_limits.dart 27:8 nameOf ││ 0 lib/measured.dart 3:3 Cart ││ 0 lib/measured.dart 9:11 Cart.count ││ 0 lib/measured.dart 21:15 Cart.empty ││ 0 lib/measured.dart 26:11 Priced.price ││ 0 lib/measured.dart 28:8 Priced.isFree ││ 0 lib/measured.dart 32:5 largest ││ 6 more values, listed by `assay metrics --reporter json` │╰──────────────────────────────────────────────────────────────────────────────╯The report runs the same pass of the rules as check. It does not read the baseline: a violation the baseline knows is over its limit like any other. So on the same project, the values over their limit that are neither suppressed nor taken over are exactly the violations check reports for the limit rules; the suppressed ones are those it hides; and the values taken over are those it does not find at all.
The metric report
Section titled “The metric report”╭─ assay metrics ──────────────────────────────────────────────────────────────╮│ Preset recommended ││ Files 2 │╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Metrics ────────────────────────────────────────────────────────────────────╮│ Metric Limit Measured Highest Over limit ││ ─────────────────────────────────────────────────────────── ││ cyclomatic complexity 15 5 2 0 ││ cognitive complexity none 5 1 0 ││ function length 60 5 7 0 ││ nesting depth 4 5 1 0 ││ parameter count 6 5 2 0 ││ methods per class none 0 - - ││ file length 600 2 17 0 │╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Cyclomatic complexity ──────────────────── limit 15 · 0 over · 5 functions ─╮│ Value File Line Name ││ ────────────────────────────────────────────────── ││ 2 lib/prices.dart 2:5 total ││ 2 lib/prices.dart 11:49 closure in cheapest ││ 1 lib/prices.dart 11:5 cheapest ││ 1 lib/prices.dart 14:8 name ││ 1 lib/prices.dart 17:8 label │╰──────────────────────────────────────────────────────────────────────────────╯
…
╭─ Function length ────────────────────────── limit 60 · 0 over · 5 functions ─╮│ No limit in test/**, integration_test/**, test_driver/** ││ ││ Value File Line Name ││ ────────────────────────────────────────────────── ││ 7 lib/prices.dart 2:5 total ││ 1 lib/prices.dart 11:5 cheapest ││ 1 lib/prices.dart 11:49 closure in cheapest ││ 1 lib/prices.dart 14:8 name ││ 1 lib/prices.dart 17:8 label │╰──────────────────────────────────────────────────────────────────────────────╯
…The console form gives a box Metrics with a row per metric, then one box per metric with its highest values: every value over its limit (marked !) and as many more as make ten. The JSON form (--reporter json) lists every value with its limit, with a summary per metric. The HTML form (--reporter html:<file>) is one self-contained page with sortable tables of functions, classes and files. See assay metrics for each form.
Line coverage
Section titled “Line coverage”assay metrics --coverage <lcov file> adds the line coverage of the measured files under lib/, from a coverage report the project’s own tests wrote (flutter test --coverage writes coverage/lcov.info). It judges nothing, and without --coverage every form of the report is exactly what it is without coverage.
What is counted, and how, is in Coverage. The console, JSON and HTML forms show the same numbers; see Line coverage for each.
Code lines and duplicated code
Section titled “Code lines and duplicated code”Every measured file has its code lines: the lines a token of its code lies on. A blank line and a line that holds only a comment, a doc comment among them, are not code lines. Each line of a multi-line string is one, and so are the directives. No rule limits it; it is the base of the share of duplicated code. It is always in the report: codeLines of each file in the JSON form, the Code lines column of the Files table in the HTML form.
Where the check duplicated_code is on, the report adds the duplicated code of the measured files the check reads: their duplicated lines, the code lines inside a duplicated region, and the share they make of the code lines. It judges nothing. What is counted is in Duplicated code, and each form of the report in Duplicated code.