magic-value-comparison / R2004#

Message emitted:

Consider using a named constant or an enum instead of '%s'.

Description:

Using named constants instead of magic values helps improve readability and maintainability of your code, try to avoid them in comparisons.

Problematic code:

import random

measurement = random.randint(0, 200)
above_threshold = False
i = 0
while i < 5:   # [magic-value-comparison]
    above_threshold = measurement > 100   # [magic-value-comparison]
    if above_threshold:
        break
    measurement = random.randint(0, 200)

Correct code:

import random

MAX_NUM_OF_ITERATIONS = 5
THRESHOLD_VAL = 100
MIN_MEASUREMENT_VAL = 0
MAX_MEASUREMENT_VAL = 200

measurement = random.randint(MIN_MEASUREMENT_VAL, MAX_MEASUREMENT_VAL)
above_threshold = False
i = 0
while i < MAX_NUM_OF_ITERATIONS:
    above_threshold = measurement > THRESHOLD_VAL
    if above_threshold:
        break
    measurement = random.randint(MIN_MEASUREMENT_VAL, MAX_MEASUREMENT_VAL)

Note

This message is emitted by the optional 'magic-value'

checker which requires the pylint.extensions.magic_value plugin to be loaded.

Created by the magic-value checker.