Skip to content

BSL029 — Magic numbers

Summary

Magic numbers

Identifiers

Field Value
Rule code BSL029
Compatible alias MagicNumber
Severity INFORMATION
Enabled by default Yes
Implemented Yes
Tags convention, readability

Behavior

  • The public identifier BSL029 and alias MagicNumber are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL029"]
ignore = ["MagicNumber"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL029
  • bsl-disable:
Value = "example";  // bsl-disable: BSL029
  • compatible BSLLS form:
Value = "example";  // BSLLS:MagicNumber-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL029
// code without this diagnostic
// noqa-enable: BSL029

// bsl-disable: BSL029
// code without this diagnostic
// bsl-enable: BSL029

// BSLLS:MagicNumber-off
// code without this diagnostic
// BSLLS:MagicNumber-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

Magic numbers are any number in your code that does not immediately become apparent without being immersed in context.

Examples

Bad

Function GetsTheInterval(Duration)

     Return Duration < 10 * 60 * 60;

End Function

Good

Function GetsTheInterval (Duration in Seconds)

    MinutesHour     = 60;
    SecondsMinute   = 60;
    SecondsHour     = SecondsMinute * MinutesHour;
    HoursIninterval = 10;
    Return Duration < HoursWininterval * SecondsHour;

End Function

Exceptions

Magic numbers used in structures and correspondences are not considered errors, as they are used as keys or values in data structures where the context is clear:

// Structure insert - no error
Structure = New Structure;
Structure.Insert("MyVariable", 20); // No error
Structure.Insert("AnotherVariable", 42); // No error

// Structure constructor - no error
Structure2 = New Structure("Field1, Field2", 5, 15); // No error

// Direct structure property assignment - no error
StructureWithFields = New Structure("MyVariable, AnotherField");
StructureWithFields.MyVariable = 20; // No error
StructureWithFields.AnotherField = 50; // No error

// Fixed structure - no error
FixedStructure = New FixedStructure("Value", 200); // No error

// Correspondence - no error (both key and value)
Correspondence = New Correspondence;
Correspondence.Insert("Code", 123); // No error
Correspondence.Insert(1980, "Olympics in Moscow"); // No error