Field Name Matches Class
ID |
csharp.field_name_matches_class |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
CSharp |
Tags |
code-style, naming |
Description
Reports a field whose name (compared case-insensitively) matches the name of the type that
declares it, for example a field Account inside class Account. Sharing the name of the
enclosing type confuses readers about whether an identifier refers to the value or the type.
Rationale
A field that carries its own type’s name reads ambiguously: at the use site it is unclear
whether Account denotes the field or the class, and refactoring tools and humans alike are
prone to confusing the two. Choosing a name that describes the field’s role instead of echoing
the type keeps the code unambiguous.
public class Account
{
private int Account; // FLAW — field shares the type name
private int balance; // OK — descriptive field name
}
Remediation
Rename the field to describe what it holds (for instance balance, current, or value)
rather than repeating the type name.