Which code style would you prefer?


Formerly I would refactor codes from nested parentheses

val obj = SomeObject()

to chained .let statements

val obj = SomeObject()
obj.let {actionOne(it)}
    .let {actionTwo(it)}
    .let {actionThree(it)}

Which one would you prefer?

P.S. I’d like to know why the newest kotlin plugin issue a warning in code inspect on the last code style.


I would prefer the first approach with nested calls, because the actual control flow (what result is passed where) is much better visible. If readability is your goal, I would introduce variables with a meaning:

val average = actionOne(obj)
val power = actionTwo(average)
val cost = actionThree(power)

This would add a meaning and not just superficial cosmetics.


The relative topic is pipe forward operator Pipe-forward operator |>

I think the second approach is much readability.


Users with the desire for |> can (and should!) switch to Scala or F# :wink: The ugliness of this operator is outstanding!

There is a simple solution with standard Kotlin language features.