29 lines
788 B
Plaintext
29 lines
788 B
Plaintext
Using ``++string.Equals++`` to determine if a string is empty is significantly slower than using ``++string.IsNullOrEmpty()++`` or checking for ``++string.Length == 0++``. ``++string.IsNullOrEmpty()++`` is both clear and concise, and therefore preferred to laborious, error-prone, manual null- and emptiness-checking.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
"".Equals(name); // Noncompliant
|
|
!name.Equals(""); // Noncompliant
|
|
name.Equals(string.Empty); // Noncompliant
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
name != null && name.Length > 0 // Compliant but more error prone
|
|
!string.IsNullOrEmpty(name)
|
|
string.IsNullOrEmpty(name)
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|