# Explicit "do nothing" construct?

**URL:** <https://discuss.kotlinlang.org/t/explicit-do-nothing-construct/1972>\
**Category:** Language Design\
**Created:** [September 16, 2016, 11:57am UTC](https://discuss.kotlinlang.org/t/explicit-do-nothing-construct/1972 "2016-09-16T11:57:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ov7a](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@ov7a](https://discuss.kotlinlang.org/u/ov7a)\
**Post date:** [September 16, 2016, 11:57am UTC](https://discuss.kotlinlang.org/t/explicit-do-nothing-construct/1972/1 "2016-09-16T11:57:38Z")

</div>

Sometimes it is useful to have explicit construct of “do nothing”. It could potentially increase readability. What comes in mind is empty methods and lambdas, prototypes, default action in `when` clause and so on.

As I know, there are two options: `{}` and `Unit`. For me, `Unit` seems misleading. Also, it is object.  
`{}` is more readable, but one can confuse it with lambda or it can be autoformatted to `{\n}`, which looks ugly:

```
when (state) {
    State.FAIL -> doSomething()
    State.FAIL -> {
        doOther()
        Thing()
    }
    else -> {
    }
}

```

compare it to this

```
when (state) {
    State.FAIL -> doSomething()
    State.FAIL -> {
        doOther()
        Thing()
    }
    else -> pass
}

```

Also, concept of empty block as “do nothing” is not obvious. There are some questions on SO like “[Do nothing keyword in C#](http://stackoverflow.com/questions/12957049/do-nothing-keyword-in-c)” or “[Empty if statements](http://stackoverflow.com/questions/16428903/empty-if-statements)”.  
Some languages do have it, e.g. Python has ‘pass’ (due to lang formatting requirements)

I think it would be easy to add a special keyword for this to language and it would greatly improve readability in some cases.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [September 16, 2016, 12:13pm UTC](https://discuss.kotlinlang.org/t/explicit-do-nothing-construct/1972/2 "2016-09-16T12:13:01Z")

</div>

Adding any keyword to the language is not easy at all, because it will break all code that has used that word as an identifier. Given the backwards compatibility promises given at the release of Kotlin 1.0, this needs _very_ strong arguments.

As you correctly write, Kotlin has two existing possibilities to express the same idea with the same amount of characters. Learning the meaning of `Unit` or an empty pair of braces in this context is not much harder than learning a new keyword. Therefore, the argument doesn’t seem strong.

Not to mention the fact that you can define it yourself if you think that it will make your code more readable: `val pass: Unit = Unit`

---

<div class="post-metadata">

**Author:** ![ov7a](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@ov7a](https://discuss.kotlinlang.org/u/ov7a)\
**Post date:** [September 16, 2016, 2:13pm UTC](https://discuss.kotlinlang.org/t/explicit-do-nothing-construct/1972/3 "2016-09-16T14:13:54Z")

</div>

Thanks for fast response. Did not think about it.  
I’ll consider using `val pass`.

---

<div class="post-metadata">

**Author:** ![lovef.code](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lovef.code/32/6898_2.png) [@lovef.code](https://discuss.kotlinlang.org/u/lovef.code)\
**Post date:** [November 4, 2019, 5:47pm UTC](https://discuss.kotlinlang.org/t/explicit-do-nothing-construct/1972/4 "2019-11-04T17:47:11Z")

</div>

Is there any chance of having IntelliJ _not_ reformat `{}` into `{\n}`? That to me would be the best option

---

<div class="post-metadata">

**Author:** ![yvolk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yvolk/32/7043_2.png) [@yvolk](https://discuss.kotlinlang.org/u/yvolk)\
**Post date:** [July 4, 2021, 7:29am UTC](https://discuss.kotlinlang.org/t/explicit-do-nothing-construct/1972/5 "2021-07-04T07:29:10Z")

</div>

> [@lovef.code](#):
>
> Is there any chance of having IntelliJ _not_ reformat `{}` into `{\n}` ? That to me would be the best option

This example (IntelliJ behavior) shows why empty curly braces ( `{}` ) aren’t a good option:  
Empty code block could be empty on purpose or missing piece of code.  
Today I see that static analysis app (SonarQube) highlights such blocks as “Code smell”.

Introducing my own `val pass` would be good, but I don’t want to pollute all my files with another import. AFAIK Kotlin doesn’t have means to make some user’s declaration globally visible without importing it in each code file?!  
But maybe I should think about some single import for all my project-wide declarations…

Reading discussions of similar questions for other languages, I started to think that `Unit` _is_ the best option here, because we are actually talking about a _value_ returned by one of `when` branches. (`when` is an expression!)  
In the above code example each `when` branch returns some value implicitly.  
Immediately returning Unit in this case is the shortest and the most explicit way to tell that we do nothing before returning the Unit?!
