Code Smell 295 - String Concatenation


TL;DR: Avoid string concatenation for complex strings, use templates.
Problems π
- Readability
- Maintainability
- Error-prone code
- Security concerns
- Unexpected outputs
- Context fragmentation
- Translation nightmares
- Context loss
- (You will not see Performance Issues in this list)
Solutions π
- Implement message templates
- Separate text and logic
- Maintain translation context
- Abstract string creation.
- Use sprintf() or equivalent in your programming language.
Context π¬
String concatenation often starts innocently but quickly becomes a mess.
When you build strings by joining multiple fragments, you create complex and hard-to-translate code.
Translation requires context, but concatenation splits natural sentences into disconnected fragments.
This creates a perfect storm of confusing code that breaks when languages with different word orders or grammatical structures are introduced.
Performance is rarely a concern and optimizing string concatenation is a Premature Optimization smell.
The clean code argument is always stronger than making premature optimizations thinking you are clever than the compiler.
Sample Code π
Wrong β
name <- 'Art Vandelay'
age <- 30
city <- 'New York'
message <- paste0('User ',
name,
' is ',
age,
' years old and lives in ',
city,
'.')
# Same problem
message <- "User
" %<% name %>
" is " %<% age %>
" years old and lives in "
%<% city %>
"."
print(message)
Right π
name <- "Art Vandelay"
age <- 30
city <- "New York"
message <- sprintf(
"User %s is %d years old and lives in %s.",
name,
age,
city)
# Easier to understand and translate
# Some languages might change the order
# of the subparts
print(message)
Detection π
[X] Semi-Automatic
You can detect this smell by looking for concatenation operation abuse.
Many linters can also look for multiple string literals mixed with variables inside these functions.
You can also watch for combined string fragments that would form natural sentences.
Code with many single-character string literals (like spaces or punctuation) concatenated to variables is a strong indicator.
Tags π·οΈ
- Declarative Code
Level π
[x] Beginner
Why the Bijection Is Important πΊοΈ
In natural language, sentences represent complete thoughts with proper grammar and structure.
When you fragment these into concatenated pieces, you break the Bijection between human-readable text and your code representation.
This mismatch causes multiple problems: for translators who need complete sentences to maintain context, for developers trying to understand the final output, and for maintenance when requirements change.
The world has many cultures and languages and the string order might change.
Templates maintain this bijection by keeping sentence structures intact, making your code a closer representation of the real-world language it produces.
AI Generation π€
AI code generators often create this smell because they use the most direct approach to string manipulation.
When prompted to "create a message with a username," they frequently default to basic concatenation without considering the translation or maintenance implications.
AI generators may not understand the broader context unless you explicitly instruct them to use template systems.
AI Detection π₯
Most AI tools can detect and fix this smell with specific instructions.
Try Them! π
Remember: AI Assistants make lots of mistakes
Suggested Prompt: use string templates instead of concatenation
Without Proper Instructions | With Specific Instructions |
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
Gemini | Gemini |
DeepSeek | DeepSeek |
Meta AI | Meta AI |
Qwen | Qwen |
Conclusion π
String concatenation creates fragile code that's hard to maintain and nearly impossible to translate correctly.
By switching to template-based approaches with packages like glue, you create more readable and maintainable code that preserves the natural structure of human language.
This approach makes translation far easier as translators work with complete sentences rather than fragments. Your future self (and your translators) will thank you for using templates instead of cobbling strings together one piece at a time
Relations π©ββ€οΈβπβπ¨
Disclaimer π
Code Smells are my opinion.
Credits π
Photo by Amador Loureiro on Unsplash
Programming is the art of telling another human what one wants the computer to do.
Donald Knuth
This article is part of the CodeSmell Series.
Subscribe to my newsletter
Read articles from Maxi Contieri directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Maxi Contieri
Maxi Contieri
Iβm a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.