Same Method Field Names
ID |
csharp.same_method_field_names |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
naming, reliability |
Description
Reports a method whose name is identical to a field or property declared on the same
class. Although the language tolerates the collision because methods and fields live in
distinct member kinds, the duplication confuses readers and refactoring tools — at a use
site obj.X reads ambiguously, and a small typo can turn a method-group reference into a
field read with no diagnostic.
Rationale
A class is read as a vocabulary: each name should denote one role. When a method and a field share a name, every reader has to keep both in mind and decide which one is being referenced; navigation jumps to the wrong member, and renames touch the wrong one. The fix is purely cosmetic and risk-free — change one of the two names.
public class Counter
{
public int Value;
public int Value() // FLAW — method shares the field name
{
return Value;
}
public int GetValue() // OK — distinct name
{
return Value;
}
}
Remediation
Rename one of the members so the class has a single name per concept. The convention is
to keep the property/field as the noun and to prefix the method with a verb such as
Get, Read, or Compute.