# "return" vs expression body

**URL:** <https://discuss.kotlinlang.org/t/return-vs-expression-body/12485>\
**Category:** Uncategorized\
**Created:** [April 26, 2019, 8:01am UTC](https://discuss.kotlinlang.org/t/return-vs-expression-body/12485 "2019-04-26T08:01:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![medium](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/medium/32/10404_2.png) [@medium](https://discuss.kotlinlang.org/u/medium)\
**Post date:** [April 26, 2019, 8:01am UTC](https://discuss.kotlinlang.org/t/return-vs-expression-body/12485/1 "2019-04-26T08:01:20Z")

</div>

Functions with only a single expression can be written with `=` instead of `return`. In the end it is a matter of taste, but I’m interested how you handle it. Do you follow a general rule in your coding conventions? The [official style guide](https://kotlinlang.org/docs/reference/coding-conventions.html#function-formatting) recommend to use expression body syntax whenever possible.

I see the following possibilities:

- Never using expression body (`=`)
- Using expression body whenever possible
- Using expression body only for short expressions (one-liner)

Examples

Java-style `return`:

```auto
override fun findByPlant(plantId: PlantId): List<Vehicle> {
    return entityManager.createNamedQuery(Vehicle.Queries.findByPlant, Vehicle::class.java)
        .setParameter("plantId", plantId)
        .resultList
}

```

Expression body:

```auto
override fun findByPlant(plantId: PlantId): List<Vehicle> =
    entityManager.createNamedQuery(Vehicle.Queries.findByPlant, Vehicle::class.java)
        .setParameter("plantId", plantId)
        .resultList

```

---

<div class="post-metadata">

**Author:** ![vbezhenar](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar)\
**Post date:** [April 26, 2019, 9:30am UTC](https://discuss.kotlinlang.org/t/return-vs-expression-body/12485/2 "2019-04-26T09:30:57Z")

</div>

I’m using it whenever possible. I don’t specifically refactor code to achieve that goal, but if my function’s body is just one expression, I’m using this syntax, why waste tokens for unnecessary `return`, I don’t think it’ll add any clarity. On the other hand if that one expression could be better expressed as a multiple statements, I’ll use return, no problems with that. Basically it’s the same as with Java lambda expressions, the difference is that Kotlin allows more code to be expressed with a single expression.

---

<div class="post-metadata">

**Author:** ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)\
**Post date:** [April 26, 2019, 9:33am UTC](https://discuss.kotlinlang.org/t/return-vs-expression-body/12485/3 "2019-04-26T09:33:50Z")

</div>

I would use = when the expression fits into a single line. I think that was also the intention of this = thing by the Scala people.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [April 26, 2019, 3:08pm UTC](https://discuss.kotlinlang.org/t/return-vs-expression-body/12485/4 "2019-04-26T15:08:01Z")

</div>

I use expression body style for multi-line expressions also, but only if the expression is terminated with a closing brace.

Example:

```
fun String.truncate(max: Int) = when {
   length <= max -> this
   else -> this.substring(max)
}

```

---

<div class="post-metadata">

**Author:** ![werrenmi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/werrenmi/32/5230_2.png) [@werrenmi](https://discuss.kotlinlang.org/u/werrenmi)\
**Post date:** [April 30, 2019, 11:42am UTC](https://discuss.kotlinlang.org/t/return-vs-expression-body/12485/5 "2019-04-30T11:42:27Z")

</div>

I usually use body expression when possible.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [April 30, 2019, 12:19pm UTC](https://discuss.kotlinlang.org/t/return-vs-expression-body/12485/6 "2019-04-30T12:19:26Z")

</div>

I use it for “multiline” blocks as well. Basically where there is an inline function that effectively functions like a function body specialiser (whether it is `when`, `use` or something else). In such case it looks just cleaner than putting it in a function body.
