Photo by Edge2Edge Media on Unsplash
Code Smell 218 - Magic Concatenation
If you miss a comma, you are concatenating
TL;DR: Watch out for fancy language assumptions
Problems
Possible defects
The least surprise principle violation
Hidden Assumptions
Solutions
Be declarative
Use good linters
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
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']
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