nachtien / kotlin-guides

Kotlin style guidelines for Android projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Style guide

Code styles shall be adhered to in the following order (whereas their precedence follows the order listed and subsequent guides can be used as fall back if the previous does not cover a specific topic):

  1. Android Kotlin Guides
  2. JetBrain's Kotlin Coding Conventions
  3. This Guide

Naming

Variables

Variable names should be named to remove ambiguity. In general, including a variable's type in it's name is overly verbose, unless it provides necessary clarity. For example, a list of locations (List<Location>) could appropriately be named locations (by virtue of being plural, it implies that the variable is likely a Collection).

Conversely, a property of type LiveData<List<Location>> would be appropriately named to include it's type in it's name (locationsLiveData). This helps to avoid confusion, as val locations is an unchanging reference to a collection of objects of type Location, while it would be useful to differentiate a property of type LiveData<List<Location>> as also being an unchanging reference but able to emit multiple List<Location> collections.

val colors = listOf(Color.RED, Color.GREEN, Color.BLUE) // Okay
val color: LiveData<Color> // Discouraged

val colorLiveData: LiveData<Color> // Okay
val color: Observable<Color> // Discouraged

val colorObservable: Observable<Color> // Okay

Abbreviations in variable names are strongly discouraged. Acronyms may be used if they are standard nomenclature (commonly used in place of their longhand counterparts).

val bookTitle = "Programming 101" // Okay

val bookTtl = "Programming 101" // WRONG!
val ipAddress = "127.0.0.1" // Okay

val ipAddr = "127.0.0.1" // WRONG!

View IDs

View ID names (android:id in XML) should be all lowercase with underscores separating words.

<android.support.v7.widget.RecyclerView
    android:id="@+id/dance_moves"
    />

When using Kotlin Android Extensions to provide synthetic view properties, use the as keyword on imports to name properties according to non-constant names Android Kotlin codestyle. The name should be camel case, postfixed with it's view type:

import kotlinx.android.synthetic.main.scan.dance_moves as danceMovesRecyclerView

License

Creative Commons License

This work is licensed under a Creative Commons Attribution 4.0 International License.

About

Kotlin style guidelines for Android projects.