rspec/rules/S4838/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

61 lines
1.5 KiB
Plaintext

== Why is this an issue?
This rule raises an issue when an iteration over the items of a ``++Collection++`` is performed on a super-type of the type handled by the ``++Collection++``.
Relying on ``++Object++`` or any classes between ``++Object++`` and the real class handled by the ``++Collection++`` is not recommended. While it's accepted by the language, this practice reduces readability of the code and forces to down-cast the item of the ``++Collection++`` to be able to call a method on it while simply using the correct type in the iteration makes things more clear and simple.
=== Noncompliant code example
[source,java]
----
public Collection<Person> getPersons() { ... }
for (Object item : getPersons()) { // Noncompliant
Person person = (Person) item; // Noncompliant; it's required to down-cast to the to correct type to use "item"
person.getAdress();
}
----
=== Compliant solution
[source,java]
----
for (Person person : getPersons()) { // Compliant
person.getAddress() ;
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Change "XXX" by the type handled by the Collection.
=== Highlighting
First: the "wrong" type
Second: the Collection part of the iteration
'''
== Comments And Links
(visible only on this page)
=== on 12 Sep 2018, 16:41:38 Alexandre Gigleux wrote:
\[~nicolas.peru] Can you review?
=== on 12 Sep 2018, 16:45:47 Nicolas Peru wrote:
LGTM
endif::env-github,rspecator-view[]