# The hidden pitfalls of the Elvis operator

DevFeed: [The hidden pitfalls of the Elvis operator](<https://devfeed.tech/articles/the-hidden-pitfalls-of-the-elvis-operator-25887.md>)

Original publisher: [Read original article](<https://medium.com/google-developer-experts/the-hidden-pitfalls-of-the-elvis-operator-da536ba68161?source=rss-1331e67af4e1------2>)

Author: Danny Preussler

Published: 2020-07-28T08:05:18Z

Content type: tutorial

Language: en

Sources: [Stories by Danny Preussler on Medium](<https://devfeed.tech/sources/stories-by-danny-preussler-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [elvis-operator](<https://devfeed.tech/tags/elvis-operator.md>), [expression](<https://devfeed.tech/tags/expression.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [null-safety](<https://devfeed.tech/tags/null-safety.md>), [pitfalls](<https://devfeed.tech/tags/pitfalls.md>), [programming](<https://devfeed.tech/tags/programming.md>), [returning](<https://devfeed.tech/tags/returning.md>)

## AI overview

This Kotlin article explains that the Elvis operator can behave differently from an equivalent if/else statement when chained expressions are involved. Kotlin evaluates the full expression on the left, including the final function call, before deciding whether to evaluate the Elvis branch, so both functions may be called when the final expression returns null.

## Source excerpt

Hidden pitfalls when using Elvis operator I guess many of us love Elvis, both the artist and the operator in Kotlin. But it can lead to some hidden pitfalls if you are not aware of how it works. https://unsplash.com/photos/1LCzr14Ah5U I only realized recently when Vladimir Zdravkovic put some code on twitter and ask us to guess what it's printing: https://twitter.com/vlazdra/status/1287366531987406848?s=20 I assumed a hidden puzzle but I could not see the issue. I could not see any reason why this would print anything but it does! After thinking about the issue (Vladimir wrote an article about it) I found more and more cases where this could go wrong. But let me show you some code: Kotlin's null safety I think most of us love the way we can easily write null safe code with Kotlin like this: presenter?.onDestroy() or data?.let{ updateData(data) } And it is super easy to add an alternative case: data?.let{ updateData(data) } ?: run { showLoadingSpinner() }Let me ask you something Do you think the following code is basically the same as the above? if (data != null) { updateData(data) } else { showLoadingSpinner() } I'm sure most of us do think they are equivalent. But what if I told you, it's not? The if/else is totally binary, it's either-or. But with the Elvis operator, it might be both! To understand why we have to look closer to how it works. Other than the else that belongs explicit to an if , the Elvis operator is not tied to a single ?. Remember, we can chain them: someVariable?.someField?.doSomething() if we now add the Elvis operator here, it will get executed depending on the expression to its left side: someVariable?.someField?.doSomething() ?: run { doSomethingElse() } so if any expression in there is null, the Elvis block will get called. It will finish evaluating everything on the left before checking if the operator is needed. This includes the last expression. So this is depending on whatever doSomething() returns! If it is null, then the right side wil