You want to replace all, but you replace one ocurrence
TL;DR: Bad function names will lead you to defects
Problems
The least surprise principle violation
Solutions
Avoid ambiguous or bad names
Wrap with your functions
Use mature languajes
Context
Some names in immature languages break the bijection principle.
When you use them, you agree on some semantics that are not the actual behavior.
Consequently, you must know accidental implementations to avoid this defect.
Sample Code
Wrong
const pets = '😺🐶😺';
const justDogs = pets.replace('😺', '🐩');
const catsArePresent = justDogs.includes('😺');
// returns true
Right
const pets = '😺🐶😺';
const justDogs = pets.replaceAll('😺', '🐩');
// Or
const justDogs = pets.replace(/😺/g, '');
const catsArePresent = justDogs.includes('😺');
// returns false
Detection
[X] Automatic
You can search and forbid the usage of replace() in your code and define replaceFirst() if you need to change only the first occurrence
Tags
- Naming
Level
[x] Beginner
AI Generation
All generators avoided this problem.
AI Detection
ChatGPT and Copilot use Regular Expressions to solve the problem.
Gemini and Claude failed to spot the mistake.
None of them use replaceAll() (introduced in ES2021)
Conclusion
Using replace() instead of replaceAll() would not fully achieve the intended result of replacing all occurrences.
It would only replace the first occurrence, potentially leading to incorrect behavior if there are multiple occurrences.
Relations
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--8ut8CKTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F366059%252F69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 41 - Regular Expression Abusers
Maxi Contieri ・ Dec 3 '20
More Info
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--8ut8CKTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F366059%252F69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
What exactly is a name - Part II Rehab
Maxi Contieri ・ May 23 '21
Disclaimer
Code Smells are my opinion.
Credits
Photo by Jari Hytönen on Unsplash
We must not blame programmers for their bugs. They belong to them only until the code is merged to the repository. After that, all bugs are ours!
Yegor Bugayenko
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--8ut8CKTs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://media.dev.to/cdn-cgi/image/width%3D150%2Cheight%3D150%2Cfit%3Dcover%2Cgravity%3Dauto%2Cformat%3Dauto/https%253A%252F%252Fdev-to-uploads.s3.amazonaws.com%252Fuploads%252Fuser%252Fprofile_image%252F366059%252F69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (0)