Function length
The length of a function is the number of lines of its body that carry code. A long function does several things, and you have to keep all of them in mind to change one; no part of it can be tested alone.
It is one of the five metrics of a function, and the one that a function declared inside another adds to: a closure’s lines count for the function around it as well as for the closure.
How it is counted
Section titled “How it is counted”The lines of the body that carry code, from the opening brace to the closing one (for an expression body, from => to the semicolon).
- Blank lines and lines with only a comment do not count. Every line of a multi-line string does.
- The documentation comment, the annotations, the signature and a constructor’s initializers are not part of the body.
- A function on one line has length 1, one with no body 0.
- A closure’s lines count for the function around it as well as for the closure.
The page of the limit rule has every case: How the length is counted.
Example
Section titled “Example”/// The sum of [prices].int total(List<int> prices) { // Start from nothing. var sum = 0;
for (final price in prices) { sum += price; } return sum;}Its length is 7:
| Lines | Count |
|---|---|
the line of the opening brace, int total(…) { |
1 |
var sum = 0; |
1 |
the three lines of the for loop |
3 |
return sum; |
1 |
| the line of the closing brace | 1 |
| the documentation comment, the comment line and the blank line | 0 |
The limit rule is max_function_length, advisory: a function whose value is over the limit max is a violation of it. A function exactly at max is not reported.
| Preset | Limit |
|---|---|
core |
none: the preset does not hold the rule |
recommended |
60 |
strict |
40 |
recommended and strict make the rule silent in the test directories, test/**, integration_test/** and test_driver/**: there the main function of a test file holds every test below it. A value in those directories has no limit, and the report marks it no limit here.
To change the limit, set the rule’s max; the rule’s exclude stays as the preset set it (Configuring metrics):
assay: rules: max_function_length: max: 80In the report
Section titled “In the report”assay metrics gives the metric a row in the box Metrics and a box of its own, with the limit and the counts on the right of its top line and the highest values first. Under strict, with a test file whose main has 46 lines:
╭─ Function length ───────────────────────── limit 40 · 1 over · 16 functions ─╮│ No limit in test/**, integration_test/**, test_driver/** ││ ││ Value File Line Name ││ ─────────────────────────────────────────────────────────────── ││ 46 test/long_test.dart 1:6 main no limit here ││ ! 45 lib/over_limits.dart 53:14 longList ││ 25 lib/over_limits.dart 27:8 nameOf ││ 15 lib/over_limits.dart 99:5 nestedCount ││ 13 lib/over_limits.dart 13:5 deeplyNested ││ 9 lib/measured.dart 11:7 Cart.total ││ 4 lib/measured.dart 32:5 largest ││ 3 lib/measured.dart 3:3 Cart ││ 1 lib/measured.dart 9:11 Cart.count ││ 1 lib/measured.dart 21:15 Cart.empty ││ 6 more values, listed by `assay metrics --reporter json` │╰──────────────────────────────────────────────────────────────────────────────╯- JSON: the key
functionLengthof each function, with itsvalue, itslimitand whether it is over it. The limit isnullwhere the rule’sexcludecovers the file. - HTML: the column Length of the table of functions.
- Trend: the series Function length over limit (Points and series).
Every form is on assay metrics.