29 lines
777 B
Plaintext
29 lines
777 B
Plaintext
== Why is this an issue?
|
|
|
|
Calling `String.isEmpty()` clearly communicates the code's intention, which is to test if the string is empty. Using `String.length() == 0` is less direct and makes the code less readable.
|
|
|
|
== How to fix it
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
if ("string".length() == 0) { /* … */ } // Noncompliant
|
|
|
|
if ("string".length() > 0) { /* … */ } // Noncompliant
|
|
----
|
|
|
|
==== Compliant solution
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
if ("string".isEmpty()){ /* … */ }
|
|
|
|
if (!"string".isEmpty()){ /* … */ }
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* Java Documentation - https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty()[java.lang.String.isEmpty() method]
|