rspec/rules/S2912/java/rule.adoc

44 lines
1.0 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
One thing that makes good code good is the clarity with which it conveys the intent of the original programmer to maintainers, and the proper choice of ``++indexOf++`` methods can help move code from confusing to clear.
If you need to see whether a substring is located beyond a certain point in a string, you can test the ``++indexOf++`` the substring versus the target point, or you can use the version of ``++indexOf++`` which takes a starting point argument. The latter is arguably clearer because the result is tested against -1, which is an easily recognizable "not found" indicator.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
String name = "ismael";
if (name.indexOf("ae") > 2) { // Noncompliant
// ...
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
String name = "ismael";
if (name.indexOf("ae", 2) > -1) {
// ...
}
----
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[]