Tom
Tom

Reputation: 8180

Explain the logic behind this from the redux docs

I was wondering why index was being passed around in the 'simple' react redux todo app then I came across this.

It’s a good idea to pass as little data in each action as possible. For example, it’s better to pass index than the whole todo object.

from http://redux.js.org/docs/basics/Actions.html

To me this is a very puzzling statement. Is the author saying that passing an index is preferable to passing an object reference? I have been wondering why this is done in the example and here it's implied that it's because 'It’s a good idea to pass as little data in each action as possible'. Which doesn't make much sense to me. Surely passing an index is the same data-wise as passing an object reference. As if it mattered anyway.

Upvotes: 1

Views: 65

Answers (1)

Jake Haller-Roby
Jake Haller-Roby

Reputation: 6427

First, we have to acknowledge that the main purpose of Flux, in general, is to make a JS codebase clean and concise. This is not a framework around optimizing performance. It's about making code clean, clear, reliable and extensible. The easiest way to accomplish that, is by having very good and clear separations between your concerns.

The point of this section of the docs isn't to say "Something bad will happen to your data if you pass extra info". It's trying to say "In order to get full use of the Flux architecture, each section should have the minimum necessary information".

Why? Well the same reason it's easier to debug a 15 line file than it is to debug a 1500 line file. Both are certainly doable, but if you can have clean and clear separations between your concerns, it makes the process of debugging later much easier.

i.e.:

"The Complete_Todo method didn't work like it was supposed to. Are we passing in the right id?"

vs.

"The Complete_Todo method didn't work like it was supposed to. Is the todo object being passed correct? I know it looks like we only need the id, but maybe we should check it's other values in case we overlooked something in the code that requires them......"

Upvotes: 3

Related Questions