Replace messy string concatenation with clean, readable text blocks
TL;DR: You can eliminate verbose string concatenation and escape sequences by using text blocks for multi-line content.
- Poor code readability
- Excessive escape sequences
- String concatenation complexity
- Maintenance difficulties
- Code verbosity
- Translation Problems
- Indentation issues
- Complex formatting
- No, Speed is seldom a real problem unless you are a premature optimizator
Code Smell 295 - String Concatenation
Code Smell 04 - String Abusers
Code Smell 03 - Functions Are Too Long
Code Smell 121 - String Validations
Code Smell 236 - Unwrapped Lines
Code Smell 122 - Primitive Obsession
Code Smell 66 - Shotgun Surgery
Code Smell 243 - Concatenated Properties
String concatenation is a common source of code noise and maintenance frustration.
When you piece together multi-line content using the + operator and manual escape sequences like \n or ", you create a fragmented version of the text that is difficult to read and even harder to edit.
This "String Abuse" obscures the actual layout of the content, making it nearly impossible to visualize the final output without running the code.
When you replace string concatenations with Text Blocks, you allow the code to look like the data it represents.
Whether it's a JSON payload, an SQL query, or an HTML template, text blocks preserve the natural indentation and formatting of the content.
This strengthens the Bijection by providing a one-to-one visual mapping between your implementation and the real-world document you are modeling, eliminating the need for translation layers and "Premature Optimization" of string building.
- Identify multi-line string concatenations or strings with excessive escape sequences
- Replace opening quote and concatenation operators with triple quotes (""")
- Remove escape sequences for quotes and newlines
- Adjust indentation to match your code style
- Add .strip() for single-line regex patterns or when trailing newlines cause issues
public class QueryBuilder {
public String buildEmployeeQuery() {
String sql = "SELECT emp.employee_id, " +
"emp.first_name, emp.last_name, " +
" dept.department_name, " +
"emp.salary " +
"FROM employees emp " +
"JOIN departments dept ON " +
"emp.department_id = " +
"dept.department_id " +
"WHERE emp.salary > ? " +
" AND dept.location = ? " +
"ORDER BY emp.salary DESC";
return sql;
}
public String buildJsonPayload(String name, int age) {
String json = "{\n" +
" \"name\": \"" + name + "\",\n" +
" \"age\": " + age + ",\n" +
" \"address\": {\n" +
" \"street\": " +
"\"123 Main St\",\n" +
" \"city\": \"New York\"\n" +
" }\n" +
"}";
return json;
}
}public class QueryBuilder {
public String buildEmployeeQuery() {
// 1. Identify multi-line string concatenations or strings
// with excessive escape sequences
// 2. Replace opening quote and concatenation operators
// with triple quotes (""")
// 3. Remove escape sequences for quotes and newlines
// 4. Adjust indentation to match your code style
// 5. Add .strip() for single-line regex patterns or
// when trailing newlines cause issues
// pro tip: If you put a known prefix
// after the string delimiter
// many IDEs will adjust the syntax highlighter and linter
// in this case SQL
String sql = """SQL
SELECT emp.employee_id, emp.first_name,
emp.last_name,
dept.department_name, emp.salary
FROM employees emp
JOIN departments dept ON
emp.department_id = dept.department_id
WHERE emp.salary > ?
AND dept.location = ?
ORDER BY emp.salary DESC
""";
return sql;
}
public String buildJsonPayload(String name, int age) {
// 1. Identified concatenation with escape sequences
// 2. Replaced with text block using """
// 3. Removed \" and \n escapes
// 4. Preserved natural indentation
// 5. No .strip() needed here
// pro tip: If you put a known prefix
// after the string delimiter
// many IDEs will adjust the syntax highlighter and linter
// in this case json5
String json = """json5
{
"name": "%s",
"age": %d,
"address": {
"street": "123 Main St",
"city": "New York"
}
}
""".formatted(name, age);
return json;
}
}[X] Semi-Automatic
This refactoring is safe.
It doesn't change the runtime behavior of strings; it only cleans up syntax and formatting.
You follow compilation rules carefully to avoid errors.
You reduce code noise caused by concatenations and escape sequences.
The multi-line strings become easier to read and maintain. Indentation and formatting are preserved without manual adjustments, making your code more natural and less error-prone.
How Does it Improve the Bijection? πΊοΈ
You make the code closer to the real-world representation of the string content, preserving layout and format as seen by the developer.
This enhances the one-to-one mapping between intent and code, minimizing translation errors from concept to implementation.
Some languages still lack multi-line string mechanisms.
Examples of languages with full support:
| Language | Feature | Syntax | Docs |
|---|---|---|---|
| Java | Text Blocks | """ |
JEP 378 |
| Kotlin | Raw Strings | """ |
Kotlin Docs |
| Python | Triple-Quoted Strings | """ / ''' |
Python Docs |
| JavaScript | Template Literals | ` ` |
MDN |
| Go | Raw Strings | ` ` |
Go Spec |
| Swift | Multiline Strings | """ |
Swift Docs |
| C# | Raw String Literals | """ |
C# Docs |
| Ruby | Heredocs | <<EOF |
Ruby Docs |
| PHP | Heredoc / Nowdoc | <<< |
PHP Docs |
| Scala | Multiline Strings | """ |
Scala 3 Docs |
- Standards
[X] Beginner
Refactoring 025 - Decompose Regular Expressions
Refactoring 002 - Extract Method
Suggested Prompt: 1. Identify multi-line string concatenations or strings with excessive escape sequences2. Replace opening quote and concatenation operators with triple quotes (""")3. Remove escape sequences for quotes and newlines
| Without Proper Instructions | With Specific Instructions |
|---|---|
| ChatGPT | ChatGPT |
| Claude | Claude |
| Perplexity | Perplexity |
| Copilot | Copilot |
| You | You |
| Gemini | Gemini |
| DeepSeek | DeepSeek |
| Meta AI | Meta AI |
| Grok | Grok |
| Qwen | Qwen |
This article is part of the Refactoring Series.
