Skip to main content

prefer-correct-identifier-length

added in: 1.6.0
⚙️
Pro+

Warns when the identifier name in variables, parameters, or enum constants is too short or too long.

⚙️ Config

Set min-identifier-length (default is 3) to configure the minimum allowed identifier length.

Set max-identifier-length (default is 300) to configure the maximum allowed identifier length.

Set exceptions (default is empty) to ignore specific identifier names.

Set ignore-inline-functions (default is false) to ignore inline functions (example).

analysis_options.yaml
dart_code_metrics:
rules:
- prefer-correct-identifier-length:
exceptions: [ 'a' ]
max-identifier-length: 30
min-identifier-length: 3
ignore-inline-functions: false

Example

❌ Bad:

var x = 0; // LINT: The x identifier is 1 characters long. Try increasing its length to 3 characters.
var multiplatformConfigurationPoint = 0; // LINT: The multiplatformConfigurationPoint identifier is 31 characters long. Try reducing its length to 30 characters.

final declarations = <({int f, String s})>{}; // LINT: The f identifier is 1 characters long. Try increasing its length to 3 characters.

final value = switch (declarations) {
Set s => s.single, // LINT: The s identifier is 1 characters long. Try increasing its length to 3 characters.
_ => 0,
};

if (declarations case Set(length: final l)) { // LINT: The l identifier is 1 characters long. Try increasing its length to 3 characters.
print(l);
}

✅ Good:

var property = 0; // Correct, the length is 8
var multiplatformConfiguration = 0; // Correct, the length is 26

final declarations = <({int first, String second})>{};

final value = switch (declarations) {
Set someSet => someSet.single,
_ => 0,
};

if (declarations case Set(: final length)) {
print(length);
}

Example with "ignore-inline-functions"

Config
analysis_options.yaml
dart_code_metrics:
rules:
- prefer-correct-identifier-length:
ignore-inline-functions: true

✅ Good:

class TestClass {
void _someNamedCallback({required Function(String, int) callback}) {
callback();
}

void _anotherMethod() {
_someNamedCallback(callback: (a, b) { // Correct, inline functions are ignored
...
});
}
}