在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):ditn/KotlinStyleGuide开源软件地址(OpenSource Url):https://github.com/ditn/KotlinStyleGuide开源编程语言(OpenSource Language):开源软件介绍(OpenSource Introduction):Kotlin Style GuideA style guide for Android developers writing in Kotlin. These guidelines are based on the official Kotlin coding conventions. Whitespace/Coding StyleUse ktlint to enforce styles. Apply ktlint to your IDE with Naming StyleIf in doubt, default to the Java Coding Conventions such as:
Use camelCase names for View IDs.
ColonsThere should be no space before a colon when separating a property/parameter name and it's type. There should however be a space before a colon which separates a type and supertype or interface: interface Foo<out T : Any> : Bar {
fun foo(a: Int): T
} LambdasFollow the coding conventions:
To add to this, lambda parameters (or those in destructured declarations) which are unused should be replaced with an underscore, unless leaving the parameter significantly improves readability. Android Studio will nag you about this anyway: .subscribe(
{ bitmap -> view.displayImage(bitmap) },
{ _ -> view.setUiState(UiState.FAILURE) }) Lambda ReturnsAdding a ClassesConstructorsInitialize the properties of a class via primary constructor parameters instead of using an init block. Do:class Person(val firstName: String, val lastName: String, var age: Int) {
// ...
} Don't:class Person(firstName: String, lastName: String, age: Int) {
val firstName: String
val lastName: String
var age: Int
init {
this.firstName = firstName
this.lastName = lastName
this.age = age
}
// ...
} For formatting these primary constructors, follow the Kotlin coding conventions:
class Person(
id: Int,
name: String,
surname: String
) : Human(id, name),
KotlinMaker {
// ...
} Companion ObjectsCompanion objects, such as those for class MyFragment : Fragment() {
init {
Injector.INSTANCE.inject(this)
}
@Inject
lateinit var dataManager: DataManager
fun doSomething() {
// ...
}
companion object {
const val BUNDLE_VALUE_ONE = "bundle_value_one"
fun newInstance(value1: String, value2: String): MyFragment {
// ...
}
}
} As a sidenote regarding String key = MyFragment.Companion.BUNDLE_VALUE_ONE; Delcaring the property as a String key = MyFragment.BUNDLE_VALUE_ONE; This also inlines any access to the FunctionsUnit (void) FunctionsIf a function returns Unit (eg fun foo() { // ": Unit" is omitted here
// ...
} The exception is an empty or stub method, in which case convert the method to an expression body: fun foo() = Unit With that being said, if stubbing functions prefer using Kotlin's inline fun foo() {
TODO("This is yet to be implemented")
} Expression BodiesFunctions whose bodies are single line should be converted to expression bodies where possible, unless that function returns Whilst expression bodies allow the omission of the return type, strongly consider keeping it for the sake of readability unless the function quite obviously returns a primitive type such as a Functions vs PropertiesYou'll often find that the
Extension FunctionsAs you would with creating Java util classes, create an extension package and make separate files for each type:
Extension functions are fun, but don't go overboard. Try not to hide mountains of complexity behind clever extensions. Flow ControlWhen StatementsTo keep Dowhen (aValue) {
1 -> doSomethingForCaseOne()
2 -> doSomethingForCaseTwo()
3 -> doSomethingForCaseThree()
}
fun doSomethingForCaseTwo() {
foo()
bar()
} Don'twhen (aValue) {
1 -> doSomethingForCaseOne()
2 -> {
foo()
bar()
}
3 -> doSomethingForCaseThree()
} Similarly, because Dowhen (aValue) {
1, 3 -> doSomethingForCaseOneAndThree()
2 -> doSomethingForCaseTwo()
} Don'twhen (aValue) {
1 -> doSomethingForCaseOneAndThree()
2 -> doSomethingForCaseTwo()
3 -> doSomethingForCaseOneAndThree()
} ImportsDo not use star imports. Disable these in Android Studio. If you've applied ktlint, it will have done this for you. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论