-
Hello! I was looking at the code of TopicViewModel and I realized that the private functions are declared outside of the class. Why is that? Are there any benefits, memory-wise, code cleanup, etc related to that? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The main benefit is that you can more easily identify what inputs are used in the method. If you have a method declared inside a class, those methods (for better and for worse) have access to all of the instance's parameters, properties, other methods. A top-level method just has the parameters to the method, and that's it. If you want access to a particular parameter, or to call some instance method, you have to pass it in. For that reason, top-level methods can lead to a data flow that's easier to understand and read. If you're looking at just |
Beta Was this translation helpful? Give feedback.
-
SAMSUNG GALAXY T-289
…On Wed, Jan 18, 2023, 7:34 AM Alex Vanyo ***@***.***> wrote:
The main benefit is that you can more easily identify what inputs are used
in the method. If you have a method declared inside a class, those methods
(for better and for worse) have access to all of the instance's parameters,
properties, other methods.
A top-level method just has the parameters to the method, and that's it.
If you want access to a particular parameter, or to call some instance
method, you have to pass it in.
For that reason, top-level methods can lead to a data flow that's easier
to understand and read. If you're looking at just newsUiState
<https://github.com/android/nowinandroid/blob/eb6dbaac0bc42e422e25cec3fd7ac2cd245d1a8d/feature/topic/src/main/java/com/google/samples/apps/nowinandroid/feature/topic/TopicViewModel.kt#LL131C13-L131C25>,
you can know that it will only depend on those three parameters, and not on
topicsRepository.
—
Reply to this email directly, view it on GitHub
<#538 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A43RVMEHPIOFZGHJWJHB72LWS4T77ANCNFSM6AAAAAAT57MHJE>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
The main benefit is that you can more easily identify what inputs are used in the method. If you have a method declared inside a class, those methods (for better and for worse) have access to all of the instance's parameters, properties, other methods.
A top-level method just has the parameters to the method, and that's it. If you want access to a particular parameter, or to call some instance method, you have to pass it in.
For that reason, top-level methods can lead to a data flow that's easier to understand and read. If you're looking at just
newsUiState
, you can know that it will only depend on those three parameters, and not ontopicsRepository
.