Skip to content

prefer_correct_identifier_length

v1.0.0WarningConfigurableClass Naming

Flags a variable, field or parameter whose name is shorter or longer than the configured bounds — 2 to 40 characters by default.

A one-letter name outside a tiny scope forces the reader to hold a mapping the code never states, and a forty-character one is usually a sentence that belongs in a doc comment.

This rule is in the pedantic preset, and works with its defaults — no configuration needed.

class Order {
final int q = 0; // LINT: one character
// LINT: 47 characters
final int theCompletelyUnnecessarilyVerboseLineItemTotal = 0;
}
class Order {
final int quantity = 0;
final int lineItemTotal = 0;
}

Locals, fields, top-level variables and parameters all go through the same bounds:

// Don't
int discount(int p, int r) => p - r; // LINT on `p` and on `r`
// Do
int discount(int price, int rebate) => price - rebate;

Loop counters, coordinates and the e of a catch clause pass out of the box — the built-in set is i j k n x y z e a b id db ui os _:

// All accepted
void render(List<String> rows) {
for (var i = 0; i < rows.length; i++) {
try {
print(rows[i]);
} catch (e) {
print(e);
}
}
}

A private name is measured without its underscore

Section titled “A private name is measured without its underscore”

The leading _ is a modifier, not part of the name, so _id counts as two characters:

class User {
// Accepted — `id` is two characters, and is in the built-in allow set anyway
final int _id = 0;
}

additional_allow_names extends the built-in set. Use it for the abbreviations your codebase keeps on purpose:

analysis_options.yaml
many_lints:
rules:
prefer_correct_identifier_length:
additional_allow_names: [dx, dy, sql]
// Accepted with the config above
void translate(double dx, double dy) {}

Writing allow_names: instead replaces the built-in set outright, so i and e start being reported unless you list them again. Reach for additional_allow_names unless that is what you mean.

With min_length: 3:

// Don't — two characters is no longer enough
final ok = true; // LINT
// Do
final isReady = true;
Option Type Default Description
min_length int 2 Shortest allowed name, excluding a leading underscore
max_length int 40 Longest allowed name, excluding a leading underscore
allow_names list (built-in set) Names exempt from both bounds. Replaces the built-in set
additional_allow_names list [] Names to add without replacing the built-in set

Only names you declare as storage. Variables, fields, top-level variables and parameters are checked. A method, class, getter or enum constant is not — for type names reach for prefer_correct_type_name.

No scope awareness. A two-character local in a three-line function is judged the same as a two-character field on a public API. Widen additional_allow_names rather than lowering min_length when only a few names are the exception.

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
prefer_correct_identifier_length:
enabled: true

To disable this rule:

many_lints.yaml
rules:
prefer_correct_identifier_length: false

To keep the rule on but skip certain paths, use per-rule exclude.