# Code Smell 268 - Ternary Metaprogramming

> TL;DR: Avoid using ternary operators for dynamic method calls

# Problems

- Reduced code readability
- Increased debugging difficulty
- Potential runtime errors
- Decreased maintainability
- Possible refactoring problems
- Obscured program flow
- [Metaprogramming](https://maximilianocontieri.com/laziness-i-meta-programming) pitfalls

# Solutions

1. Use explicit conditionals
2. Apply the strategy pattern
3. Create descriptive methods

# Context

Ternary metaprogramming uses conditional operators to select and invoke methods dynamically. 

It leads to code that's harder to understand, debug, and maintain. 

You risk introducing subtle bugs and making your code obscure to other developers.

Clean Code is the opposite of [Clever Code](https://maximilianocontieri.com/code-smell-06-too-clever-programmer).

# Sample Code

## Wrong

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

```javascript
const method = success ? 'start' : 'stop';
obj[method]();
```

## Right

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

```javascript
if (success) {
    obj.start();
} else {
    obj.stop();
}
```

# Detection

[X] Automatic 

Your linters can detect this smell by looking for ternary operators to select method names, especially when combined with bracket notation for method calls. 

You can also watch for variables that store method names based on conditions.

# Tags

- Metaprogramming

# Level

[X] Beginner

# AI Generation

AI code generators might introduce this smell since they prioritize code brevity over readability. 

They could generate ternary metaprogramming patterns when trying to produce concise code.

# AI Detection

AI detectors can identify this smell by recognizing patterns of ternary operators used for method selection. 

They may need specific instructions about readability and maintainability.

## Try Them!

*Remember AI Assistants make lots of mistakes*

 [ChatGPT](https://chat.openai.com/?q=Correct+and+Explain+this+Code%3A+%60%60%60javascript%0D%0Aconst+method+%3D+success+%3F+%27start%27+%3A+%27stop%27%3B%0D%0Aobj%5Bmethod%5D%28%29%3B%0D%0A%60%60%60) [Claude](https://claude.ai/new?q=Correct+and+Explain+this+Code%3A+%60%60%60javascript%0D%0Aconst+method+%3D+success+%3F+%27start%27+%3A+%27stop%27%3B%0D%0Aobj%5Bmethod%5D%28%29%3B%0D%0A%60%60%60) [Perplexity](https://perplexity.ai/?q=Correct+and+Explain+this+Code%3A+%60%60%60javascript%0D%0Aconst+method+%3D+success+%3F+%27start%27+%3A+%27stop%27%3B%0D%0Aobj%5Bmethod%5D%28%29%3B%0D%0A%60%60%60) [Gemini](https://gemini.google.com/?q=Correct+and+Explain+this+Code%3A+%60%60%60javascript%0D%0Aconst+method+%3D+success+%3F+%27start%27+%3A+%27stop%27%3B%0D%0Aobj%5Bmethod%5D%28%29%3B%0D%0A%60%60%60)

# Conclusion

Ternary metaprogramming can seem clever and concise but creates more problems than it solves. 

By favoring explicit conditionals and well-named methods, you can write easier-to-understand, debug, and maintain code.

Remember that code is read far more often than written, so prioritize clarity over brevity.

# Relations

%[https://maximilianocontieri.com/code-smell-06-too-clever-programmer]

%[https://maximilianocontieri.com/code-smell-207-dynamic-methods]

%[https://maximilianocontieri.com/code-smell-21-anonymous-functions-abusers]

# More Info

%[https://maximilianocontieri.com/laziness-i-meta-programming]

# 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 [Burst](https://unsplash.com/@burst) on [Unsplash](https://unsplash.com/photos/woman-standing-in-brown-field-while-looking-sideways-aoN3HWLbhdI)  
  
* * *

> Programs must be written for people to read, and only incidentally for machines to execute.

_Harold Abelson_
 
%[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]
