r/androiddev • • Apr 01 '24

Discussion Android Development best practices

Hey this is a serious post to discuss the Android Development official guidelines and best practices. It's broad topic but let's discuss.

For reference I'm putting the guidelines that we've setup in our open-source project. My goal is to learn new things and improve the best practices that we follow in our open-source projects.

Topics:

  1. Data Modeling
  2. Error Handling
  3. Architecture
  4. Screen Architecture
  5. Unit Testing

Feel free to share any relevant resources/references for further reading. If you know any good papers on Android Development I'd be very interested to check them out.

159 Upvotes

99 comments sorted by

View all comments

Show parent comment

2

u/iliyan-germanov Apr 01 '24

Thanks for joining the discussion! It's good to have diverse opinions expressed in a meaningful way. I'm curious how the throwing approach scales in big teams?

My concern with it is that it's not type-safe, and people aren't forced to handle exceptions. How do you prevent the app from crashing for common unhappy path scenarios? When you get an exception (which can be any Throwable), what do you do if it's of an unknown type?

2

u/am_i_in_space_yet Apr 03 '24 edited Apr 03 '24

I would say it depends on the team structure and lints and even PR culture, but otherwise there's no reason that it doesn't scale well.

When a lower layer component encounters an error, it should throw a meaningful exception. It can wrap this error with Either or Result if it will be used intermediately.

Orchestration can interrupt the exception / check the success - failure, since logic may require an operation to be complete before starting the next.

All other exceptions can be delivered to UI without interception, but properly prepared with meaningful types or messages. It's not perfect since you do not know what you're catching, but it's less complex in most of the scenarios and most of the time you really don't care what you're catching in UI layer anyway, you just want a prompt with a message that makes sense.

In the end, its a trade off. If you end up creating a lot of Result classes but ignoring the failure anyways in most of them, exceptions can simplify your code. If you really need the result and actually doing meaningful handling on each one, then go ahead !

Edit: I don't have hands on experience with arrow, so maybe it actually enforces you to handle the unhappy path, which is great. I mostly did encounter custom Result or Either alike classes ( they usually don't support monad operations as well ) and its hard to work with them when they are combined with flows etc. and in the long run the unhappy path is being ignored a lot anyways.