rspec/rules/S2113/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

51 lines
1.1 KiB
Plaintext

== Why is this an issue?
The ``++hasNext++`` method of an ``++Iterator++`` should only report on the state of the iterator, not change it. Making a change to an iterator in its ``++hasNext++`` method violates all expectations of what the method will do, and almost guarantees bad results when the iterator class is used.
=== Noncompliant code example
[source,java]
----
public class MyItr implements Iterator<MyClass> {
//...
public boolean hasNext() {
if (next() != null) { // Noncompliant
return true;
}
return false;
}
----
=== Compliant solution
[source,java]
----
public class MyItr implements Iterator<MyClass> {
private List<MyClass> list;
private int index = 0;
//...
public boolean hasNext() {
return index < list.size();
}
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
=== duplicates: S1849
=== on 10 Oct 2014, 15:30:37 Freddy Mallet wrote:
I would prefer the following rule title @Ann:
* "Iterator.next()" method should never be called in the "Iterator.hasNext()" method
endif::env-github,rspecator-view[]