2023-05-03 11:06:20 +02:00
== Why is this an issue?
2020-12-21 15:38:52 +01:00
It's confusing to have a class field with the same name as a method in the class. It's also confusing to have multiple fields that differ only in capitalization
2021-02-02 15:02:10 +01:00
2020-12-21 15:38:52 +01:00
Typically this situation 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. However, renaming a public method could be disruptive to callers. Therefore renaming the member is the recommended action.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-12-21 15:38:52 +01:00
----
public class Foo {
public static final String QUERY = "Select name from person";
private String query; // Noncompliant
public String query() { // Noncompliant
// do something...
}
private void doSomething() {
String tmp = query; // is this what was intended? Should this have been a call to query()?
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-12-21 15:38:52 +01:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-12-21 15:38:52 +01:00
----
public class Foo {
public static final String NAME_QUERY = "Select name from person";
private String queryString; // member has been renamed
public String query() {
// do something...
}
private void doSomething() {
String tmp = query; // results in a compile error
String tmp2 = query(); // no question now what was intended
}
}
----
2022-01-25 18:36:46 +01:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Rename the "XXX" member.
2022-01-25 18:36:46 +01:00
'''
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== replaces: S1224
2022-01-25 18:36:46 +01:00
endif::env-github,rspecator-view[]