Skip to content

Code guidelines

Ivan Schütz edited this page Mar 24, 2020 · 11 revisions
  • Use Kotlin.
  • Keep it simple!
  • Keep functions and classes reasonably short.
  • Minimize side effects.
  • Avoid force unwrap (!!) and lateinit. If needed, use ?: throw Exception("Meaningful message").
  • Use extensions.
    • If they are shared, put them in a file <ClassName>Extensions (e.g. TextFieldExtensions.kt)
    • If they are specific to a class/file, make them private.
  • Use idiomatic Kotlin (let, apply, also, etc.). But keep readability in mind! Often a simple if != null is better.
  • Prefer composition over inheritance.
  • Prefer immutability.
  • Prefer explicit type declarations for instance variables.
  • Use readable names.
  • If something is difficult to understand, add some documentation.
  • Use Rx in services / repos / business logic / view models and live data only to interface with the UI.
  • Use View binding.
  • Don't use ViewPager. User RecyclerView with SnapHelper or ViewPager2
  • Use static imports where possible (e.g. enums, string or drawable resources). This improves readability. It makes writing / copy pasting a bit annoying but reading is more frequent. Place cursor over code and press ALT+ENTER. AS will offer the import.
  • Use auto format.
  • Use sealed classes (for example to model different cell types in a RecyclerView).
Clone this wiki locally