Use visually distinct identifiers with meaningful names to eliminate errors resulting from misrecognizing the spelling of an identifier during the development and review of code. An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter.
Depending on the fonts used, certain characters are appear visually similar or even identical:
Symbol | Similar Symbols |
|---|---|
| |
| |
| |
| |
| |
| h (lowercase |
| |
Do not define multiple identifiers that vary only with respect to one or more visually similar characters.
Make the initial portions of long identifiers unique for easier recognition. This also helps prevent errors resulting from nonunique identifiers (see DCL32-C. Guarantee that mutually visible identifiers are unique).
In addition, the larger the scope of an identifier the more descriptive should be its name. It may be perfectly appropriate to name a loop control variable i but the same name would likely be confusing if it named a file scope object or a variable local to a function more than a few lines long. See also DCL19-C. Use as minimal a scope as possible for all variables and functions.
Risk Assessment
Failing to use visually distinct identifiers can result in referencing the wrong variable being usedobject or function, causing unexpected unintended program behavior.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
DCL02-C | low | unlikely | medium | P2 | L3 |
...