KT.CLASS_ORDERING

Class contents not ordered

This rule ensures class contents are ordered as follows as recommended by the Kotlin Coding Conventions: Property declarations and initializer blocks Secondary constructors Method declarations Companion object

Noncompliant Code

Copy
class OutOfOrder {
    companion object {
        const val IMPORTANT_VALUE = 3
    }

    fun returnX(): Int {
        return x
    }

    private val x = 2
}

Compliant Code

Copy
class InOrder {
    private val x = 2

    fun returnX(): Int {
        return x
    }

    companion object {
        const val IMPORTANT_VALUE = 3
    }
}

The content on this page is adapted from the Detekt Docs. Copyright ©2022 The Detekt Team. All rights reserved. https://detekt.dev/comments.html