Replace Magic Number with Symbolic Constant
Refactoring & Technical Debt

Meaning

Replace hard-coded numeric literals (magic numbers) with named constants to improve code readability and maintainability.

Primary Function

Improve code readability and maintainability by giving meaningful names to numeric literals.

Communicative Purpose

Advise developers to replace unexplained numbers with descriptive constants.

Pattern

Replace magic number with named constant

Core Structure

Replace numeric literal with a named constant declaration

Função primária

Improve code readability and maintainability by giving meaningful names to numeric literals.

Propósito comunicativo

Advise developers to replace unexplained numbers with descriptive constants.

Situações de gatilho

When a numeric literal appears in code whose purpose is not immediately clear, or when the same number appears in multiple places.

Contextos

Any source code where numeric literals are used, especially in configuration, calculations, or magic numbers.

Padrão

Replace magic number with named constant

Estrutura central

Replace numeric literal with a named constant declaration

Slots de substituição

[MAGIC_NUMBER] -> [CONSTANT_NAME]

Colocados típicos

  • number
  • constant
  • constant name
  • magic number
  • literal

Substituições comuns

  • Replace 3.14 with PI
  • 31536000 with SECONDS_PER_YEAR

Erros comuns

Choosing non-descriptive constant names, exposing constants unnecessarily, using macros without type safety.

Similar / contraste

Contrast with 'Keep magic numbers' (rare) or 'Use enum for related constants'.

Interferências

Avoid over-abstracting trivial numbers; avoid misleading constant names.

Família do chunk

  • Refactoring
  • Code Quality
  • Magic Number Elimination

Nuance

The constant should be scoped appropriately (private, public) and have a clear, domain-specific name.

Efeito pragmático

Improves readability, reduces errors when values need to change, and aids documentation.

Dica de memória

Think of a magic number as a secret code; give it a name.

Nota

Place symbolic constants in a dedicated module or class to keep the global namespace clean and to group related constants together.

Upgrade path

Replace Magic Number with Enum or Constant Class for related constants

Frequência: HighFormulaicidade: FlexibleTipo de construção: refactoringPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-term

Log in to save chunks.