Rules / Style / prefer-correct-identifier-length
prefer-correct-identifier-length
lint/style/prefer-correct-identifier-lengthrecommended
Same as prefer-correct-identifier-length · Dart Code MetricsFlags 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.
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 }
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 }
∙
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 }
How to configure
Set the severity of prefer-correct-identifier-length in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-correct-identifier-length": "error"
}
}
}
}