Code Smell 212 - Elvis Operator
Tags: webdev, javascript, beginners, programming
Published: 2023-05-13 13:08:46

*Your code is not safer using this operator*

> TL;DR: Don't propagate nulls.

# Problems

- [NULL](https://dev.to/mcsee/null-the-billion-dollar-mistake-1h1c) propagation

- Harder to read code

- Hacky code

# Solutions

1. Remove the nulls. 

2. If you can't remove it, deal explicitly with them.

# Context

The Elvis operator is also known as the null-coalescing operator or the null-safe operator.

It is a shorthand operator used in some programming languages to simplify null-checking.

The Elvis operator takes the form of ?. and is used to access a property or method of an object only if that object is not null. 

If the object is null, the operator returns null without attempting to access the property or method, thus avoiding a potential null reference exception.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/a976521e75a08ca0c57cb0153039e67e)
```kotlin
val shipTo = address?: "No address specified"

```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/1f0771dc50c77d39c6577405142e52e8)
```kotlin
val shipTo = if (address != null) address else "No address specified"

// This keeps the billion-dollar mistake error 
```

# Detection

[X] Automatic 

We can detect this operator usage and replace them with more strict checks.

# Tags

- Null

# Conclusion

The code can be difficult to follow and may require additional comments or explanations to make it clear what is happening.

The operator hides potential errors or bugs in the code. 

For example, if an object is null and the Elvis operator is used to return a default value, this may mask the fact that there is a problem with the code that is causing the object to be null in the first place.

n several languages, such as Common Lisp, Clojure, Lua, Object Pascal, Perl, Python, Ruby, and JavaScript, the OR operator (typically || or or) has the same behavior as the above: returning its first operand if it would evaluate to true in a boolean environment, and otherwise evaluating and returning its second operand. 

When the left hand side is true, the right hand side is not even evaluated; it is "short-circuited." This is different than the behavior in other languages such as C/C++, where the result of || will always be a boolean. 

# Relations

{% post https://dev.to/mcsee/code-smell-149-optional-chaining-jk %}

{% post https://dev.to/mcsee/code-smell-06-too-clever-programmer-37lb %}

{% post https://dev.to/mcsee/code-smell-12-null-la4 %}

{% post https://dev.to/mcsee/code-smell-140-short-circuit-evaluation-20kc %}

# More Info

- [Wikipedia](https://en.wikipedia.org/wiki/Elvis_operator)

{% post https://dev.to/mcsee/null-the-billion-dollar-mistake-1h1c %}

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Susan Mohr](https://unsplash.com/@theinnervizion) on [Unsplash](https://unsplash.com/photos/INLHpZKShao)
    
* * *

> You can't communicate complexity, only an awareness of it.

_Alan Perlis_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}