Return to site
Java Text Blocks Guidelines
G1. You should use a text block when it improves the clarity of the code, particularly with multi-line strings.
G2. If a string fits on a single line, without concatenation and escaped newlines, you should probably continue to use a string literal.
G3. Use embedded escape sequences when they maintain readability.
G4. For most multi-line strings, place the opening delimiter at the right end of the previous line, and place the closing delimiter on its own line, at the left margin of the text block.
G5. Avoid aligning the opening and closing delimiters and the text block's left margin. This requires reindentation of the text block if the variable name or modifiers are changed.
G6. Avoid in-line text blocks within complex expressions, as doing so can distort readability. Consider refactoring to a local variable or to a static final field.
G7. Either use only spaces or only tabs for the indentation of a text block. Mixing white space will lead to a result with irregular indentation.
G8. When a text block contains sequences of three or more double quotes, escape the first double quote of every run of three double quotes.
G9. Most text blocks should be indented to align with neighbouring Java code.
G10. It is recommended to fully left justify a wide string in order to avoid horizontal scrolling or line wrapping.
G11. Similarly, it is also reasonable to fully left justify a text block when a high line count causes the closing delimiter is likely to vertically scroll out of view. This allows the reader to track indentation with the left margin when the closing delimiter is out of view.
G12. The \<line-terminator> escape sequence should be used when a text block's final new line needs to be excluded. This better frames the text block and allows the closing delimiter to manage indentation.