DEV Community

Cover image for Code Smell 218 - Magic Concatenation
Maxi Contieri
Maxi Contieri

Posted on • Edited on

3

Code Smell 218 - Magic Concatenation

If you miss a comma, you are concatenating

TL;DR: Watch out for fancy language assumptions

Problems

Solutions

  1. Be declarative

  2. Use good linters

  3. Prefer declarative languages

Context

Many programming languages favor laziness over readability and clean code.

You should use them with caution and trust your tests.

Sample Code

Wrong

tools = [
    "Amazon Codewhisperer",
    "Bard" # Notice the missing comma
    "ChatGPT",
    "Dalle-E" # Also here
    "Eliza"
    ]

print(len(tools))
# This will output 3 

print(tools)
# ['Amazon Codewhisperer', 'BardChatGPT', 'Dalle-EEliza']
# Missing Commas act as hidden string concatenators


Enter fullscreen mode Exit fullscreen mode

Right

tools = [
    "Amazon Codewhisperer",
    "Bard",
    "ChatGPT",
    "Dalle-E",
    "Eliza"
]

# We added all the missing commas

print(len(tools))
# 5

print(tools)
# ['Amazon Codewhisperer', 'Bard', 'ChatGPT', 'Dalle-E', 'Eliza']
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

Many linters warn about this problem.

Also, ChatGPT and Bard can detect the problem.

Tags

  • Readability

Conclusion

Many modern programming languages come with a significant amount of accidental complexity.

They are often optimized for writing code quickly, even though they may be prone to defects.

Unfortunately, when working with these languages, it is essential to exercise extreme caution.

Relations

Disclaimer

Code Smells are my opinion.

Credits

Photo by Edge2Edge Media on Unsplash


A programming language is low level when its programs require attention to the irrelevant.

Alan J. Perlis


This article is part of the CodeSmell Series.


My new book about clean code is available for pre-order.

You will find several recipes like this one with a higher level of detail

Book

Redis image

Short-term memory for faster
AI agents 🤖💨

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

Top comments (0)

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

MongoDB Atlas lets you build and run modern apps in 125+ regions across AWS, Azure, and Google Cloud. Multi-cloud clusters distribute data seamlessly and auto-failover between providers for high availability and flexibility. Start free!

Learn More

👋 Kindness is contagious

If this **helped, please leave a ❤️ or a friendly comment!

Okay