Menu

#853 Enhance rule SimplifyStartsWith description

open
pmd (543)
5
2012-10-07
2009-03-26
xxxxxxx
No

The rule explanation says:

"Since it passes in a literal of length 1, this call to String.startsWith can be rewritten using String.charAt(0) to save some time."

If you directly change all startsWith("X") calls to charAt(0)=='X' you can get ArrayIndexOutOfBoundsExceptions when the string to check has length 0. This is only a runtime error and can appear long after correcting pmd violations, making it difficult to detect the relation. Thus, I would change the text of the rule to:

"Since it passes in a literal of length 1, this call to String.startsWith can be rewritten using String.length()>0 && String.charAt(0) to save some time. "

Don't worry about performance, because we have done some tests and the double check (length+charAt) is still faster than the call to startsWith.

Discussion


Log in to post a comment.