# Is it possible to add null check in if

**URL:** <https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303>\
**Category:** Language Design\
**Created:** [April 24, 2020, 3:58am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303 "2020-04-24T03:58:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![guangyya](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@guangyya](https://discuss.kotlinlang.org/u/guangyya)\
**Post date:** [April 24, 2020, 3:58am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/1 "2020-04-24T03:58:46Z")

</div>

For example:

if (null) → false

if (not\_null\_obj) → true

this can save some time on checking a variable is null like:

if (object == null)  
if (object != null)

---

<div class="post-metadata">

**Author:** ![guangyya](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@guangyya](https://discuss.kotlinlang.org/u/guangyya)\
**Post date:** [April 24, 2020, 4:00am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/2 "2020-04-24T04:00:57Z")

</div>

I am not sure whether it’s a good language design, just a thought in my mind 🙂

---

<div class="post-metadata">

**Author:** ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)\
**Post date:** [April 24, 2020, 4:17am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/3 "2020-04-24T04:17:02Z")

</div>

`if` is currently restricted to `Boolean`, not even `Boolean?`. Expanding it to `Any?` is dangerous, particularly for the previously safe `Boolean`s. If you use it for nullability, `false` isn’t `null`, so it should be accepted, but that doesn’t make any sense.

---

<div class="post-metadata">

**Author:** ![alexis.manin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexis.manin/32/8381_2.png) [@alexis.manin](https://discuss.kotlinlang.org/u/alexis.manin)\
**Post date:** [April 24, 2020, 7:23am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/4 "2020-04-24T07:23:11Z")

</div>

I agree with @derektom14, I’d rather keep if construct explicit, to ease readability. However, I’m a long damned Java dev, so maybe I’m a lost cause on this 🙂

However, There’s already an _explicit_ way to do such thing. It requires a few extra-characters though. Not sure if it is really better than a plain old `if(value != null)`:

```nohighlight
fun nullIfElse(value : Any?) {
     value?.run {
         println("Go here if $value is presentl")
     } ?: run {
         println("Go here if null")
     }
 }
 
 nullIfElse(null)
 nullIfElse("anything")

```

---

<div class="post-metadata">

**Author:** ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)\
**Post date:** [April 24, 2020, 4:49pm UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/5 "2020-04-24T16:49:12Z")

</div>

The one thing I don’t like about the `nullIfElse` setup is that using `run` will change the context of `this` and using `let` will introduce an `it` that is the same as `value`, which can get weird. Of course, if you’re doing `container.value?.let { value -> ... }`, this can be a major boon, but if it’s already an immutable value I prefer using `if (value != null) { ... }`

---

<div class="post-metadata">

**Author:** ![aimozg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/aimozg/32/1557_2.png) [@aimozg](https://discuss.kotlinlang.org/u/aimozg)\
**Post date:** [April 25, 2020, 11:20am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/6 "2020-04-25T11:20:10Z")

</div>

I’m against implicit conversion to bool, but would greatly approve some unary operator for null checks.  
Like postfix `??` or prefix `!!`:

```nohighlight
if (value?.nullableField??) {
  println(value.nullableField)
}

```

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [April 26, 2020, 10:33am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/7 "2020-04-26T10:33:01Z")

</div>

Even for booleans, there’s the question of whether `null` should be treated as `true`, or as `false`. In my experience, both cases appear about equally often, so an operator which only worked one way would be frustrating and likely to cause bugs.

Better IMHO to use some of the existing, more explicit options:

```
if (someNullableBoolean ?: true) // Treats null as true

if (someNullableBoolean ?: false) // Treats null as false

if (someNullableBoolean == true) // Treats null as false

if (someNullableBoolean != false) // Treats null as true

```

None of those are long or complicated, and none involve language changes.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [April 26, 2020, 12:05pm UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/8 "2020-04-26T12:05:39Z")

</div>

There has been a discussion about the idom for nullable boolean checks [here](https://discuss.kotlinlang.org/t/is-the-idiom-for-checking-nullable-booleans-helpful/8238).  
Also the official guidelines say that you should prefer using `someNullableBoolean == true` or `someNulullableBoolean == false` over the elvis operator.  
[https://kotlinlang.org/docs/reference/idioms.html#consuming-a-nullable-boolean](https://kotlinlang.org/docs/reference/idioms.html#consuming-a-nullable-boolean)

---

<div class="post-metadata">

**Author:** ![guangyya](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@guangyya](https://discuss.kotlinlang.org/u/guangyya)\
**Post date:** [April 27, 2020, 2:15am UTC](https://discuss.kotlinlang.org/t/is-it-possible-to-add-null-check-in-if/17303/9 "2020-04-27T02:15:48Z")

</div>

Thanks all above for the very detailed explanation, yes as a C++ developer as well, I kindof like some of the C++ ways like if for null check, and I do realize that in a strong type language like Kotlin, explicit Boolean for if check is very helpful and safe. 😀
