rspec/rules/S2176/java/rule.adoc

86 lines
1.7 KiB
Plaintext

== Why is this an issue?
Two classes can have the same simple name if they are in two different packages.
[source,java]
----
package org.foo.domain;
public class User {
// ..
}
----
[source,java]
----
package org.foo.presentation;
public class User {
// ..
}
----
However, this becomes an issue when a class has the same name as the superclass it extends or the interfaces it
implements, also known as class name shadowing. It is problematic because it can be unclear which class is being
referred to in the code, leading to ambiguity and potential bugs.
Therefore, it is recommended to use unique and descriptive class names that do not conflict with the names of the
superclass or interfaces.
This rule raises an issue when a class name shadows its superclass or interface names.
== How to fix it
Rename the class using a more descriptive name.
=== Code examples
==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
package org.foo.presentation;
public class User implements org.foo.domain.User { // Noncompliant
// ...
}
----
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
package org.foo.presentation;
import org.foo.domain.User;
public class UserView implements User { // Compliant
// ...
}
----
== Resources
=== Documentation
* https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.4.1[Java SE Specifications > Java Language Specification > 6.4.1. Shadowing]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Rename this class.
'''
== Comments And Links
(visible only on this page)
=== on 21 Oct 2014, 14:08:46 Nicolas Peru wrote:
LGTM
endif::env-github,rspecator-view[]