prefer-correct-identifier-length

Same as prefer-correct-identifier-length in Dart Code Metrics

Group
Style
Path
lint/style/prefer-correct-identifier-length
Default
Off, opt in
Origin
Dart Code Metrics
Rule
339 of 512

Flags declared identifiers that are shorter or longer than the allowed range.

Very short names rarely convey meaning and very long ones hurt readability, so this rule keeps declared names within a configurable length band. Its scope is deliberately limited to variable and field declarations, getter and setter names, and enum constants; parameters, catch-clause variables, for-each loop variables, and plain function and method names are never checked, because short names are often idiomatic there. A single leading underscore is stripped before both the length and exception checks, so _id is judged as id. Declarations in initializer closures, arrow bodies, operators, and enum members follow the same policy as declarations in ordinary function bodies.

Options

min-length (int, default: 3) — flag identifiers shorter than this. max-length (int, default: 300) — flag identifiers longer than this. exceptions (list of strings, default: []) — names always allowed regardless of length.

Invalid

example.dartdart
// Bad: too-short variable declarations, accessor names and enum constants.

void example() {
  var a = compute();
  final xy = a;
  print(xy);
}

class Processor {
  int m = 0;

  String get n => '';

  set v(String value) {}
}

// A C-style for-loop counter is a variable declaration, so it is checked.
void loop(List<int> items) {
  for (var i = 0; i < items.length; i++) {
    print(items[i]);
  }
}

enum Size { s, m, l }

// A Dart 3 destructuring declaration declares variables, so every name the
// pattern binds is length-checked — records, lists, and nested patterns alike.
void patternDeclarations(List<int> items) {
  final (a, b) = (1, 2);
  final [c, d] = items;
  final (e, (f, _)) = (1, (2, 3));
  print([a, b, c, d, e, f]);
}

// A declaration inside a labeled block or a closure body is reached too.
void nestedDeclarations() {
  lbl: {
    final gg = 1;
    print(gg);
  }
  final callback = () {
    final hh = 2;
    return hh;
  };
  print(callback);
}

final topCallback = () {
  final aa = 1;
  return aa;
};

class ClosureDeclarations {
  final callback = () {
    final bb = 2;
    return bb;
  };

  Object arrow() => () {
    final cc = 3;
    return cc;
  };

  int operator +(int value) {
    final dd = value;
    return dd;
  }
}

enum MemberDeclarations {
  value;

  int read() {
    final ee = 4;
    return ee;
  }
}

class InitializerDeclarations {
  final Object callback;
  InitializerDeclarations() : callback = (() {
    final ff = 5;
    return ff;
  });
}

List<int> collectionLoop(int count) => [
  for (var w = 0; w < count; w++) w,
];
The `a` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
3void example() {
4 var a = compute();
The `xy` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
4 var a = compute();
5 final xy = a;
The `m` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
9class Processor {
10 int m = 0;
The `n` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
11
12 String get n => '';
The `v` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
13
14 set v(String value) {}
The `i` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
18void loop(List<int> items) {
19 for (var i = 0; i < items.length; i++) {
The `s` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
23
24enum Size { s, m, l }
The `m` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
23
24enum Size { s, m, l }
The `l` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
23
24enum Size { s, m, l }
The `a` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
28void patternDeclarations(List<int> items) {
29 final (a, b) = (1, 2);
The `b` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
28void patternDeclarations(List<int> items) {
29 final (a, b) = (1, 2);
The `c` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
29 final (a, b) = (1, 2);
30 final [c, d] = items;
The `d` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
29 final (a, b) = (1, 2);
30 final [c, d] = items;
The `e` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
30 final [c, d] = items;
31 final (e, (f, _)) = (1, (2, 3));
The `f` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
30 final [c, d] = items;
31 final (e, (f, _)) = (1, (2, 3));
The `gg` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
37 lbl: {
38 final gg = 1;
The `hh` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
41 final callback = () {
42 final hh = 2;
The `aa` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
48final topCallback = () {
49 final aa = 1;
The `bb` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
54 final callback = () {
55 final bb = 2;
The `cc` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
59 Object arrow() => () {
60 final cc = 3;
The `dd` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
64 int operator +(int value) {
65 final dd = value;
The `ee` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
73 int read() {
74 final ee = 4;
The `ff` identifier is 2 characters long. It's recommended to increase it up to 3 chars long.
81 InitializerDeclarations() : callback = (() {
82 final ff = 5;
The `w` identifier is 1 characters long. It's recommended to increase it up to 3 chars long.
87List<int> collectionLoop(int count) => [
88 for (var w = 0; w < count; w++) w,

Valid

example.dartdart
// Good: identifiers meet the minimum length (3), plus dcl's scope exemptions.
// dcl only checks variable declarations, getter/setter names and enum
// constants — never parameters, catch clauses, for-each variables, or plain
// method/function names.

void example() {
  var count = compute();
  final result = count + 1;
  print(result);
}

class Processor {
  String path = '';
  int id = 0; // `id` is in the exceptions list
  int _id = 0; // leading underscore stripped -> `id` -> exempt

  // Method name (`at`) and parameters (`i`, `j`) are out of scope.
  int at(int i, int j) => i + j;

  // Getter name is >= 3 characters.
  String get label => path;
}

// Parameters, catch clauses and for-each variables are never checked, so short
// names here are fine.
void shortLived(int a, int b) {
  try {
    print(a + b);
  } catch (e) {
    print(e);
  }
  for (final x in [1, 2, 3]) {
    print(x);
  }
}

enum Color { red, green, blue }

Configure prefer-correct-identifier-length

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-correct-identifier-length": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/prefer-correct-identifier-length: <reason>