Naming Single Char Var
ID |
csharp.naming_single_char_var |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Naming Convention |
Language |
CSharp |
Tags |
naming |
Description
Reports fields and local variables with a single-character name that is not one of the
conventional short names. Conventional names are i, j, k (loop counters), c
(char), b (byte), d (double), f (float), l (long), e (exception) and o
(object).
Rationale
A name like x or q carries no meaning. The reader has to track back to the
declaration and infer the role of the value from its usage, which is exactly what a good
name should make unnecessary. Short loop counters and a handful of idiomatic single
letters are widely understood, so they are allowed; everything else should describe
the value it holds.
private int x; // FLAW — single-char field, no meaning
for (int i = 0; i < n; i++) { } // OK — conventional loop counter
char c = ReadChar(); // OK — conventional char name
int total = 0; // OK — descriptive
var q = total; // FLAW — single-char local, no meaning
Remediation
Rename the field or variable to a descriptive name that states the value’s purpose, such
as index, result or currentItem.