Which code style would you prefer?


#1

Formerly I would refactor codes from nested parentheses

val obj = SomeObject()
actionThree(actionTwo(actionOne(obj)))

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.


#2

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.


#3

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

I think the second approach is much readability.


#4

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.