-
Notifications
You must be signed in to change notification settings - Fork 11.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace Result
with exception handling
#906
Conversation
8afdc9a
to
126558f
Compare
Can you please explain why you made the changes? |
...va/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepository.kt
Outdated
Show resolved
Hide resolved
...va/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepository.kt
Outdated
Show resolved
Hide resolved
...n/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsViewModel.kt
Show resolved
Hide resolved
...n/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailViewModel.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksViewModel.kt
Outdated
Show resolved
Hide resolved
Great question, sure.
By unwrapping the data (by removing
In (the pretty rare) situations where reading data can result in an error we can throw an exception, and handle it in the collector using a
which again is more readable than having In summary, throwing exceptions is preferable to having a wrapper type because it avoids excessive code branching and data unwrapping. |
...n/java/com/example/android/architecture/blueprints/todoapp/statistics/StatisticsViewModel.kt
Outdated
Show resolved
Hide resolved
126558f
to
ed1dd50
Compare
ed1dd50
to
34bbb2c
Compare
Thank you for the explanation. I do not have insights into the codebase but I was asking myself why would a task but exist in the first place, motivating you to indicate the lack with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Great work!
A good data point that shows how the Result
type in the data layer is complicating the Arch is the file changes data (i.e. +198 - 332). You're removing complexity, yay!
I think the Result
class was an overkill for this project. Other apps might benefit from it but definitely not this one :)
Here's what I've done:
DefaultTaskRepositoryTest
Result.Error
withthrow Exception
catch
blocks to flow collection and added a couple of user error messages to indicate to them what went wrongResult
classgetTask
fromResult<Task>
toTask?
, returningnull
indicates that theTask
doesn't exist