# Code Smell 247 - Javascript Replace

> TL;DR: Bad function names will lead you to defects

# Problems

- [Misleading Names](https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab)

- The least surprise principle violation

# Solutions

1. Avoid ambiguous or bad names

2. Define your own functions

3. Use mature languajes

# Context

Some names in immature languages break the [bijection](https://maximilianocontieri.com/the-one-and-only-software-design-principle) 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](https://maximilianocontieri.com/stop-calling-them-bugs).

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/6283f6599373f54c37ad914fbbf89849)

```javascript
const pets = '😺🐶😺';
const justDogs = pets.replace('😺', '🐩');

const catsArePresent = justDogs.includes('😺');
// returns true
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/d1d819ebfaf99b4143e2545fab928adf)

```javascript
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](https://maximilianocontieri.com/code-smell-41-regular-expression-abusers) to solve the problem.

Gemini and Claude failed to spot the mistake.

None of them use *replaceAll()* (introduced in [ES2021](https://www.w3schools.com/js/js_2021.asp))

# 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

%[https://maximilianocontieri.com/code-smell-38-abstract-names]

%[https://maximilianocontieri.com/code-smell-41-regular-expression-abusers]

# More Info

%[https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Jari Hytönen](https://unsplash.com/@jarispics) on [Unsplash](https://unsplash.com/photos/four-assorted-color-tabby-kittens-on-brown-basket-YCPkW_r_6uA)
    
* * *

> 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_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
