Use string length to compare empty string variables
Severity: High
Rule: The String.equals() method is overkill to test for an empty string. It is quicker to test if the length of the string is 0.
Reason: The String.equals() method is overkill to test for an empty string. It is quicker to test if the length of the string is 0.
Usage Example:
package com.rule;
class Use_String_length_to_compare_empty_string_violation
{
public boolean isEmpty(String str)
{
return str.equals(""); // VIOLATION
}
}
Should be written as:
package com.rule;
class Use_String_length_to_compare_empty_string_correction
{
public boolean isEmpty(String str)
{
return str.length()==0; // CORRECTION
}
}
Comments
Post a Comment