In what follows, we provide some guidance on design and coding for your Android programs.
Remember a program (classes, methods) should do one thing and one thing well. You should start to write good readable code from the start (see coding tips below). Break your programs into meaningful classes/methods. Be defensive when dealing with user input and other interfaces. Always check return values (a reference to an oject that might be null) and exit gracefully if an error has been encountered – IO failed – inform the user if needed in a meaningful way.
Keep the Model-View-View-Model (MVVM) architecture in mind when doing OO design for your Android applications:
Push as much of the UI functionality into the Layout XML and reduce programmatic logic for wiring in the controllers
Offload as much as the computation from the UI thread to for example worker threads, runnables, and coroutine tasks. If you don't you UI will freeze up.
Keep the model logic and data separate from the controller logic by using object encapsulation and getters/setters on the model data.
Don't ignore exceptions: It can be tempting to write code that completely ignores an exception, don't do it.
Write short methods: If a method exceeds 40 lines or so, think about whether it can be broken up without harming the structure of the program.
Comment your code: It should be self-evident what well written code does, but add comments to explain classes, methods, code logic in a method if it is not self-evident. Don't over comment code.
Limit variable scope: Keep the scope of local variables to a minimum. By doing so, you increase the readability and maintainability of your code and reduce the likelihood of error.
Limit line length to 100 characters. Helps with readability of your code.
Log when you need to: in this class we promote logging to understand control flow, inspect variables during run time, etc.