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

72 lines
1.5 KiB
Plaintext

== Why is this an issue?
The use of the ``++enum.ordinal()++`` method is regarded as suspicious since conceptually, the significance of an ``++enum++`` member is in what it represents, not its position within the ``++enum++``.
According to the method's Javadoc:
____
Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as ``++EnumSet++`` and ``++EnumMap++``.
____
Further, reliance on a particular member's position within an ``++enum++`` is a bug waiting to happen since the position of members within an ``++enum++`` is not generally regarded as significant, and maintainers are just as likely to add new members at the beginning as at the end.
Therefore this rule raises an issue for each use of the ``++ordinal++`` method.
=== Noncompliant code example
[source,java]
----
public enum Fruit {
APPLE, ORANGE, PLUM, GRAPE;
}
public void doTheThing(Fruit fruit) {
if (fruit.ordinal() == 2) { // Noncompliant
// ...
}
}
----
=== Compliant solution
[source,java]
----
public enum Fruit {
APPLE, ORANGE, PLUM, GRAPE;
}
public void doTheThing(Fruit fruit) {
if (Fruit.PLUM.equals(fruit)) {
// ...
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Make sure this call to "XXX.ordinal()" is really what's intended.
'''
== Comments And Links
(visible only on this page)
=== on 30 Sep 2015, 14:33:38 Ann Campbell wrote:
please review
endif::env-github,rspecator-view[]