Tara Czutno
07/12/2023, 6:08 PMWhen implementing an interface, keep the implementing members in the same order as members of the interface (if necessary, interspersed with additional private methods used for the implementation).
Is it 1️⃣ interspersed or 2️⃣ all public first? Something else? And does anyone have ktlint or detekt rules to enforce function ordering?
1️⃣ interspersed
class MyClass {
// Constants or properties
private val privateProperty: String = "Private"
// Constructors
constructor() {
// Constructor implementation
}
// Interspersed
fun publicFunction1() {
privateFunction1()
}
private fun privateFunction1() {
// Private function implementation
}
fun publicFunction2() {
privateFunction1()
}
private fun privateFunction2() {
// Private function implementation
}
// Companion object
companion object {
// Companion object members
}
// Overridden functions
override fun toString(): String {
// Overridden function implementation
}
}
2️⃣ all public first
class MyClass {
// Constants or properties
private val privateProperty: String = "Private"
// Constructors
constructor() {
// Constructor implementation
}
// Public functions
fun publicFunction() {
// Public function implementation
}
// Private functions
private fun privateFunction() {
// Private function implementation
}
// Companion object
companion object {
// Companion object members
}
// Overridden functions
override fun toString(): String {
// Overridden function implementation
}
}
Zsolt.bertalan
07/13/2023, 8:23 AMSam
07/18/2023, 7:35 AM