Use visually distinct identifiers to eliminate errors resulting from misrecognizing the spelling of an identifier during the development and review of code. Depending on the fonts used, certain characters are visually similar or even identical:
- '1' (one) and 'l' (lower case el)
- '0' (zero) and 'O' (capital o)
- '2' (two) and 'Z' (capital Z)
- '5' (five) and 'S' (capital S)
- '8' (eight) and 'B' (capital B)
Do not define multiple identifiers that vary only with respect to one or more visually similar characters.
When using long identifiers, try to make the initial portions of the identifiers unique for easier recognition. This also helps prevent errors resulting from non-unique identifiers (see DCL32-C. Guarantee that mutually visible identifiers are unique).
Automated Detection
Compass/ROSE could detect violations of this rule merely by making a set of identifies for each scope. Then for each variable, it converts 'l' (ell) to '1' (one), converts '0' (capital o) to '0' (zero), and does the other conversions listed above, and then checks for collisions.
Risk Analysis
Failing to use visually distinct identifiers can result in the wrong variable being used, causing unexpected program behavior.
Recommendation |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
|---|---|---|---|---|---|
DCL02-A |
low |
unlikely |
medium |
P2 |
L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
[[ISO/IEC 9899:1999]] Section 5.2.4.1, "Translation limits"
[[ISO/IEC PDTR 24772]] "AJN Choice of Filenames and other External Identifiers," "BRS Leveraging human experience," and "NAI Choice of Clear Names"
[[MISRA 04]] Rule 5.6
DCL01-A. Do not reuse variable names in subscopes 02. Declarations and Initialization (DCL) DCL03-A. Use a static assertion to test the value of a constant expression