62 lines
1.8 KiB
Plaintext
62 lines
1.8 KiB
Plaintext
This rule raises an issue when there is a method and a field in a class with names that differ only by capitalization.
|
|
|
|
== Why is this an issue?
|
|
|
|
Looking at the set of methods in a class, including superclass methods, and finding two methods or fields that differ only by capitalization is confusing to users of the class. It is similarly confusing to have a method and a field which differ only in capitalization or a method and a field with exactly the same name and visibility.
|
|
|
|
|
|
In the case of methods, it may have been a mistake on the part of the original developer, who intended to override a superclass method, but instead added a new method with nearly the same name.
|
|
|
|
|
|
Otherwise, this situation simply indicates poor naming. Method names should be action-oriented, and thus contain a verb, which is unlikely in the case where both a method and a member have the same name (with or without capitalization differences). However, renaming a public method could be disruptive to callers. Therefore renaming the member is the recommended action.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,text,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
public class Car{
|
|
|
|
public DriveTrain drive;
|
|
|
|
public void tearDown(){...}
|
|
|
|
public void drive() {...} // Noncompliant; duplicates field name
|
|
}
|
|
|
|
public class MyCar extends Car{
|
|
public void teardown(){...} // Noncompliant; not an override. It it really what's intended?
|
|
|
|
public void drivefast(){...}
|
|
|
|
public void driveFast(){...} //Huh?
|
|
}
|
|
----
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
[source,text,diff-id=1,diff-type=compliant]
|
|
----
|
|
public class Car{
|
|
|
|
private DriveTrain drive;
|
|
|
|
public void tearDown(){...}
|
|
|
|
public void drive() {...} // field visibility reduced
|
|
}
|
|
|
|
public class MyCar extends Car{
|
|
@Override
|
|
public void tearDown(){...}
|
|
|
|
public void drivefast(){...}
|
|
|
|
public void driveReallyFast(){...}
|
|
|
|
}
|
|
----
|
|
|