DEV Community

Cover image for Code Smell 295 - String Concatenation
Maxi Contieri
Maxi Contieri

Posted on

1

Code Smell 295 - String Concatenation

Untangling the string mess in your code

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 πŸ˜ƒ

  1. Implement message templates
  2. Separate text and logic
  3. Maintain translation context
  4. Abstract string creation.
  5. 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)
Enter fullscreen mode Exit fullscreen mode

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 human languages might change the order 
# of the subparts
glue("User {name} is {age} years old and lives in {city}.")

print(message)
Enter fullscreen mode Exit fullscreen mode

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

Conclusion 🏁

String concatenation creates fragile code that's hard to maintain and nearly impossible to translate correctly.

By switching to template-based approaches, 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.

Heroku

Amplify your impact where it matters most β€” building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

πŸ‘‹ Kindness is contagious

If you found this article helpful, please give a ❀️ or share a friendly comment!

Got it