Hi this is horrible do you know a more elegant way...
# codereview
i
Hi this is horrible do you know a more elegant way to refactor that ? ( sorry cor the format i am on mobile the three format ``` are not picked up) if (count() > 5) take(5).forEach { firstFiveSubsectionItems -> buildAlwaysVisibleLogic(alwaysVisibleView, firstFiveSubsectionItems) } else { forEach { itemsSubsection -> buildAlwaysVisibleLogic(alwaysVisibleView, itemsSubsection) } }
s
You can do
take(5)
even if there are fewer than 5, and you'll just get all of them.
i
Doh thanks!
a
In case
take
or any other function did not work for both cases you should write it like this:
Copy code
val items = if (condition) a() else b()
items.forEach { commonAction(it) }
1
i
Thanks