IfSQ logo


SPM-2—Magic Strings

Defect Indicators: A string literal has been hard-coded into a statement that influences the flow of a program (e.g., in an If, While, or For statement).

Risks: If strings are embedded in code in this way, it increases the time needed to make maintenance changes, and increases the risk of error.

Assessment: Mark all binary terms that involve a string literal. An easy way to do this is by looking for the keywords and symbols that can precede binary terms in the language you are using. For example:

Remedy: Isolate a single copy of the string and refer to it. For example isolate the literal into:

Note: If your programming language does not support constants, simulate this—for example, by declaring a variable and initialising it at the beginning of the program.

Research Findings: