markdown guide
 

I like the explanation from @neilonsoftware but I like to give examples.


Oh the ternary controversy…

In Kotlin is possible to use the if statement as an expression:

val x = if(condition) value else default

So the developers and community behind the language decided to stay with this instead of a ternary.

Now, for the Elvis Operator, it works over nullable values and the main intention is to remove the null from the equation as soon as you found one:

val x = myInstance?.someFoo ?: defaultFoo

This way you can just assign or use a default value as soon as something is missing. One example is for a response from a web service.

Let's say you call a service that gives you a JSON with the information from a person:

{
  "first_name": "Sinuhe",
  "last_name": "Jaime",
  "phone": "12345-54321"
}

So your class representing this response can be:

data class Person(
  val firstName: String,
  val lastName: String,
  val phone: String
)

But… surprise! surprise! Turns out @ben doesn't have a phone in his profile, so the response will be:

{
  "first_name": "Ben",
  "last_name": "Halpern"
}

For this case you have two options, first is make a default value for your class:

data class Person(
  
  val phone: String = ""
)

but some parsers will throw an exception. Your second option is… sadly, make it nullable:

data class Person(
  
  val phone: String?
)

in that way, if your parser doesn't have an strategy for missing fields, you can simply assign or keep the value as null, then you have a reason for the elvis operator, when using a Person in a function you can simply:

fun operateOverDataFromPerson(person: Person){
  
  val phone = person.phone ?: "-"
}

And nothing will break.

 

Thanks, it's a quite detailed example. I agree that we need to take care of missing fields. It's a very popular case.

 

I've never heard of the "Elvis operator", but looking at Kotlin's docs it seems it's Kotlin's Existential Operator. If so, I use them all the time in every programming language I use that supports them.

The basic problem is nulls are very bad, and should be avoided at all costs. The Existential/Elvis operator is one tool to help you avoid nulls in your code, by providing a default value where a null would have otherwise been assigned.

A ternary operator serves a different purpose and is distinct from an Existential operator.

 

Thank you very much for your explanation, it's good to know it. Because I often use the ternary operator in js. And I thought it's pretty the same.

 

Not a problem, it can be difficult to understand obscure operators especially when they seem so similar to other operators.

The Existential/Elvis Operator in JavaScript looks like this:


const pet = cat || dog

The way this works is:

  1. If cat is defined with a non-null value, pet will be assigned the value of cat
  2. If the value for cat is undefined or null, pet will be assigned the value of dog.

Yes, It's a simple and understandable example with an analogy. We call it as Safe Navigation in JS.

Ok

I think it's clear for everybody now. And thank you for your time :)

Classic DEV Post from Feb 26

You're Not An Impostor

Hey you. Yes, you, the reader. You clicked on this article because you have som...

Anastasia profile image
Hi everyone, I'm a full stack web developer. I always try to improve myself and learn something new for me, be passionate about what I am doing, & travel around the world.