Pattern: Use of Unicode escape
Issue: -
Prefer using actual Unicode character (e.g. ∞
) over the equivalent Unicode escape (e.g. \u221e
) - this makes the code easier to read and understand. Use escapes for non-printable characters, and comment if necessary.
You should not make code less readable simply out of fear that some programs might not handle non-ASCII characters properly. If that should happen, those programs are broken and they should be fixed.
<module name="AvoidEscapedUnicodeCharacters">
<property name="allowEscapesForControlCharacters" value="true"/>
<property name="allowByTailComment" value="true"/>
<property name="allowNonPrintableEscapes" value="true"/>
</module>
allowEscapesForControlCharacters
: Allow use escapes for non-printable(control) characters;allowByTailComment
: Allow use escapes if trail comment is present.allowIfAllCharactersEscaped
: Allow if all characters in literal are escaped.allowNonPrintableEscapes
: Allow if all characters in literal are escaped.
Example of incorrect code:
//Poor: the reader has no idea what this is.
String unitAbbrev = "\u03bcs";
Example of correct code:
String unitAbbrev = "μs"; //Best: perfectly clear even without a comment.