prefer_correct_identifier_length
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;}Examples
Section titled “Examples”Parameters are checked too
Section titled “Parameters are checked too”Locals, fields, top-level variables and parameters all go through the same bounds:
// Don'tint discount(int p, int r) => p - r; // LINT on `p` and on `r`
// Doint discount(int price, int rebate) => price - rebate;The conventional short names are exempt
Section titled “The conventional short names are exempt”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 acceptedvoid 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;}Adding your own short names
Section titled “Adding your own short names”additional_allow_names extends the built-in set. Use it for the abbreviations your codebase keeps on purpose:
many_lints: rules: prefer_correct_identifier_length: additional_allow_names: [dx, dy, sql]rules: prefer_correct_identifier_length: additional_allow_names: [dx, dy, sql]// Accepted with the config abovevoid 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.
Tightening the bounds
Section titled “Tightening the bounds”With min_length: 3:
// Don't — two characters is no longer enoughfinal ok = true; // LINT
// Dofinal isReady = true;Options
Section titled “Options”| 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 |
Known limitations
Section titled “Known limitations”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.
Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: prefer_correct_identifier_length: enabled: trueTurning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: prefer_correct_identifier_length: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”match_class_name_pattern— Match class names against a regular expression.prefer_correct_type_name— Keep type names within a sensible length and correctly capitalised.use_class_prefix— Require a name prefix for classes deriving from a configured type.use_class_suffix— Require a name suffix for classes deriving from a configured type.