checkMagicNumber

Reports use of so-called magic numbers or unnamed numerical constants in your code. These are numbers used directly in the code without being assigned to a variable first. For example, replace

x = 9.81 ``*`` t;

with

g = 9.81; % Gravity constant.

x = g ``*`` t;

Using magic numbers in your code can degrade readability and the author's intent for them may not be apparent. There are some situations that will not be reported. For example, comparing the output of nargin or exist to a number that would normally be reported. Aside from these exceptions, this check's strictness can be configured using the parameters described below. In the HTML report at most one violation of the same type per line of code will be shown for this check.

Configurable parameters

  • IntegerExemptionMin & IntegerExemptionMax (double): Integer numerical constants may be exempt of this check. These parameters set the lowest and highest integer value that is allowed to be used in the code. Especially the numbers 0, 1, and 2 should probably be exempt of this check. These parameters let you configure that. Defaults are -2 and 4, respectively.

  • ExemptWholeDecimals (boolean) : Whether or not this check should allow whole decimal numbers in the range IntegerExemptionMin to IntegerExemptionMax. The default value for this parameter is set to true, so for example 0.0 and 2.00 will be allowed.

  • AllowOrderConversion (boolean): By setting this to true (default), numerical constants are allowed to be used directly in the code if they are used to multiply or divide a number by an order of ten. The following examples will be reported when this parameter is set to false, and will not be reported when set to true:

    • x = y / 100;

    • x = 1e-3 ``*`` y;

    • x = 0.01 ``*`` y;

    • x = y / 1e12;

    This parameter is especially useful for allowing unit conversions in the code.

  • IgnoreValidationBlocks (boolean): By setting this to true (default), this check will not report any violations in properties or arguments blocks.

Exemption tag: %@ok<MAGNR>