38 lines
841 B
Plaintext
38 lines
841 B
Plaintext
== Why is this an issue?
|
|
|
|
Calling ``++Iterator.hasNext()++`` is not supposed to have any side effects, and therefore should not change the state of the iterator. ``++Iterator.next()++`` advances the iterator by one item. So calling it inside ``++Iterator.hasNext()++``, breaks the ``++hasNext()++`` contract, and will lead to unexpected behavior in production.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public class FibonacciIterator implements Iterator<Integer>{
|
|
...
|
|
@Override
|
|
public boolean hasNext() {
|
|
if(next() != null) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
...
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|