Often I’m faced with a choice of declaring a priva...
# codingconventions
m
Often I’m faced with a choice of declaring a private function in a companion object (good for encapsulation) or at the top file level (reduces indentation). Which do you prefer?
Copy code
class Foo {
    companion object {
        private fun bar1() { ... }
    }
}

private fun bar2() { ... }

private fun Foo.Companion.bar3() { ... }
1️⃣ companion object 2️⃣ top-level (file) 3️⃣ top-level companion extension
4️⃣ 1
2️⃣ 7
a
2️⃣ for me, the only drawback is that https://youtrack.jetbrains.com/issue/KTIJ-11292 is not done yet
👍 1
g
if it private as in your case, I always prefer member function:
Copy code
class Foo {
    private fun bar1() { ... }
}
it reads better, has better locality and even more efficient (less classes created on runtime, but just a nice bonus)