marcglasberg / async_redux

Flutter Package: A Redux version tailored for Flutter, which is easy to learn, to use, to test, and has no boilerplate. Allows for both sync and async reducers.
Other
234 stars 40 forks source link

Passing primitive types vs passing a ViewModel? #137

Closed hummy123 closed 2 years ago

hummy123 commented 2 years ago

Hi all.

I was wondering about the comparatively verbose code in some of the pub.dev documentation, and wanted to ask about it.

To illustrate, the documentation contains a code sample like this:

MyHomePage(
    counter: vm.counter,
    description: vm.description,
    onIncrement: vm.onIncrement,
));

The parameter list (which can possibly be long) is what I want to draw attention to in particular.

With this code design, if the ViewModel needs changes in its list of parameters, we need to change:

  1. The ViewModel itself.
  2. The fromStore() function.
  3. The connector.
  4. The list of parameters accepted by the view-widget.
  5. Possibly ViewModel usages within the view-widget.

However, if you pass the ViewModel to the view-widget directly instead of primitive types like string/int, the amount of code you need to rewrite and rewire in case of any changes in the ViewModel's parameters decreases.

MyHomePage(
    viewModel: vm,
));

You will then just need to change:

  1. The ViewModel itself.
  2. The fromStore() function.
  3. Possibly ViewModel usages within the view-widget.

I think the main advantage of approach (1) is better view-widget reusability by using primitive types. The advantages I see in approach (2) are shorter code and less rewriting/rewiring in case of any changes (and it might also be easier for people learning the library?).

I might have missed some other points of value about why approach 1 is better, but I thought it was worth asking or starting of a discussion at least.

marcglasberg commented 2 years ago

Both work. As you said, approach 1 has better reusability because you can have more than one connector that uses the same dumb-widget, but in most cases you really just have one connector per widget.

I think approach 1 is more clean code, because the widget gets really very independent from the connector. But I have nothing against approach 2 and I agree that is less typing. I think I should at least add approach 2 to the documentation.