The recommended directory structure follows the package structure with the common root package omitted.
UpperCamelCase(PascalCase)
If a Kotlin file contains a single class or interface (potentially with related top-level declarations), its name should be the same as the name of the class, with the .kt extension appended.
If a file contains multiple classes, or only top-level declarations, choose a name describing what the file contains, and name the file accordingly.
The name of the file should describe what the code in the file does. Therefore, you should avoid using meaningless words such as Util in file names.(PaymentUtil.kt will be good).
Placing multiple declarations (classes, top-level functions or properties) in the same Kotlin source file is encouraged as long as these declarations are closely related to each other semantically, and the file size remains reasonable (not exceeding a few hundred lines).
In particular, when defining extension functions for a class which are relevant for all clients of this class, put them in the same file with the class itself. When defining extension functions that make sense only for a specific client, put them next to the code of that client. Avoid creating files just to hold all extensions of some class.
The contents of a class should go in the following order:
1.Property declarations and initializer blocks
2.Secondary constructors
3.Method declarations
4.Companion object
Do not sort the method declarations alphabetically or by visibility, and do not separate regular methods from extension methods.
-> put related stuff together, so that someone reading the class from top to bottom can follow the logic of what's happening.
class Person(val name: String, var age: Int) {
// 1. Property declarations and initializer blocks
private val id: String = generateId()
init {
println("Creating a new person with ID $id")
}
// 2. Secondary constructors
constructor(name: String) : this(name, 0)
// 3. Method declarations
fun greet() {
println("Hello, my name is $name and I'm $age years old")
}
// 4. Companion object
// Usage: val defaultPerson = Person.createDefaultPerson()
companion object {
fun createDefaultPerson(): Person {
return Person("John Doe", 30)
}
}
}
Companion object:an object that's tied to a specific class, and it's declared inside that class. It's similar to a static member in Java, but with some important differences.
A companion object can access private members of the class that it's associated with.
You can use a companion object to define factory methods, static methods, or other utility methods that are related to the class.
When 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).
interface Calculator {
fun add(a: Int, b: Int): Int
}
class BasicCalculator : Calculator {
override fun add(a: Int, b: Int): Int {
return doAddition(a, b)
}
private fun doAddition(a: Int, b: Int): Int {
return a + b
}
}