rspec/rules/S1157/java/rule.adoc

28 lines
827 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Using ``++toLowerCase()++`` or ``++toUpperCase()++`` to make case insensitive comparisons is inefficient because it requires the creation of temporary, intermediate ``++String++`` objects.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
boolean result1 = foo.toUpperCase().equals(bar); // Noncompliant
boolean result2 = foo.equals(bar.toUpperCase()); // Noncompliant
boolean result3 = foo.toLowerCase().equals(bar.LowerCase()); // Noncompliant
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
boolean result = foo.equalsIgnoreCase(bar); // Compliant
----
2021-04-28 16:49:39 +02:00
== Exceptions
No issue will be raised when a locale is specified because the result could be different from "equalsIgnoreCase". (e.g.: using the Turkish locale)
----
boolean result1 = foo.toUpperCase(locale).equals(bar); // Compliant
----