-
Notifications
You must be signed in to change notification settings - Fork 20
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 (
!!
) andlateinit
. 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 simpleif != 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
. UserRecyclerView
withSnapHelper
orViewPager2
- Use static imports where possible (e.g. enums,
string
ordrawable
resources). This improves readability. It makes writing / copy pasting a bit annoying but reading is more frequent. Place cursor over code and pressALT
+ENTER
. AS will offer the import. - Use auto format.
- Use sealed classes (for example to model different cell types in a
RecyclerView
).