Skip to content

no_magic_number

Reports a number written into the code without a name. Give it one with a const declaration.

Tier
advisory
Scope
Dart
Fix
none

An integer or double literal in any notation: 18, 3.14159, 0xFF, 1_000_000, 1e-9. Each of these is reported:

bool isAdult(int age) => age >= 18;
double area(double radius) => 3.14159 * radius * radius;
final timeout = Duration(seconds: 30);
  • A minus sign directly before the literal belongs to the number: -5 is one violation that covers -5.
  • Every number in an expression is a violation of its own: hours * 60 * 60 has two.
  • Only a const declaration gives a number a name. The initializer of a final or var variable is reported: final timeout = 30;, an instance field int size = 20;. Where the variable can be const or static const, that is the fix.
  • A constant expression that is not a declaration is reported: const EdgeInsets.all(16) as an argument, const [8, 16] as a return value. The number has a name once the expression initializes a constant.
  • A number in a method, getter or constructor of an enum is reported, and so is one in a constructor’s initializer list or body, of a const constructor too, in a case, and as an index, names[3].
  • The minus sign belongs to the number only directly before the literal: in -(5) the number is judged without it.

It does not report:

  • the numbers in the rule parameter allowed, by default -1, 0, 1 and 2;
  • a number in a constant’s declaration, at any depth of its initializer: const maxAttempts = 5;, static const size = 20;, const timeout = Duration(seconds: 30);, const sizes = [8, 16, 24];;
  • a number in a parameter’s default value: {this.attempts = 3}, [int width = 80];
  • a number in an annotation: @Timeout(Duration(seconds: 45));
  • a number an enum value is declared with: missing(404);
  • digits in a string, a name or a comment.

A bare number says how much, not what. age >= 18 leaves you to guess that 18 is the age of majority; timeout * 1000 leaves you to guess a unit conversion. When the same number stands in three places, nothing says whether they are one decision or three, and changing one means searching for the others.

Code written by an AI coding agent is full of such numbers: the agent knows what it meant while it wrote the line, and nothing makes it write that down. A named constant keeps the meaning beside the value, and gives the value one place.

The rule is advisory because many numbers are plain where they stand: the 8 of a padding in a widget tree, the 3 of a test’s expected length. The presets switch the rule off in test directories, and allowed takes the numbers you never want named.

Violating
class Uploader {
Future<void> send(List<int> bytes) async {
if (bytes.length > 5242880) {
throw ArgumentError('The file is too large.');
}
for (var attempt = 0; attempt < 3; attempt += 1) {
if (await _trySend(bytes)) return;
await Future<void>.delayed(Duration(milliseconds: 250 * (attempt + 1)));
}
}
}
Corrected
class Uploader {
static const maxBytes = 5 * 1024 * 1024;
static const maxAttempts = 3;
static const retryStep = Duration(milliseconds: 250);
Future<void> send(List<int> bytes) async {
if (bytes.length > maxBytes) {
throw ArgumentError('The file is too large.');
}
for (var attempt = 0; attempt < maxAttempts; attempt += 1) {
if (await _trySend(bytes)) return;
await Future<void>.delayed(retryStep * (attempt + 1));
}
}
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
allowed [-1, 0, 1, 2] The numbers that are never reported. Your list replaces the default.
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.
assay:
rules:
no_magic_number:
allowed: [-1, 0, 1, 2, 100, 0.5]
  • A number is compared by its value, not its notation. With 1 allowed, 1.0 and 0x1 are allowed too; with 0.5 allowed, .5 and 5e-1 are.
  • A negative number is compared with its sign: -1 is allowed by default and -2 is not, though 2 is. To allow both, list both.
  • Your list replaces the default: allowed: [100] allows 100 and reports 0 and 1. With an empty list, every number is reported.

recommended and strict set this rule’s exclude to the test directories, test/**, integration_test/** and test_driver/**, read from the project root: there a number is most often the value a test expects. An exclude of your own replaces that list; to judge your tests too, write exclude: [].

Write why on the line directly above the suppression:

// The polynomial is the one of the published CRC-32 specification.
// ignore: assay/no_magic_number
crc = table[(crc ^ byte) & 0xFF] ^ (crc >> 8);

A suppression covers its whole line, so one suppression silences every number on it.

// ignore_for_file: assay/no_magic_number silences it in a whole file, and // ignore: no_magic_number without the prefix works too. See Suppressions.