This is a response to Bill Fisher regarding experience with Flux:
@abdullin Also, can you clarify what you mean by "solution structure"? I am thinking about revising the examples soon.
Currently all flux samples (that I've seen) group files into folders based on technical similarity. For example, stores go with stores, action creators reside in the same folder shared with the other action creators.
This pattern works quite well for smaller projects. It feels especially good for the sample projects of various MVC frameworks, when you have just a bunch of controllers, models and views.
However, as we discovered on some production projects, such approach doesn't scale well. At some point you end up with dozens of technically similar files per folder and logically messy solution.
- Doesn't scale well.
- While you work on a component, you have to suffer from extra context switching overhead (jumping between various folders).
- Solutions become more "entangled" than needed.
- More complex IDE features are needed to support the workflow (e.g. context-aware navigation and completion).
- More merge conflicts, since it is harder to bound work by a feature (and communicate that).
We discovered that aligning solution with the domain model leads to a better design in the long run. E.g. we try to group files by their functionality or feature. This would mean, that different technical elements of a single feature could be squashed into one folder or onto one file. For example, for Flux we are considering to have a single folder for "News feed", which would contain all related action creators, fetchers and stores. Of course, there could be some other components that the this chat page would use (e.g. avatars, like buttons or user stores), such components would reside in their own folders. On the overall, such component decomposition is requires more effort than simply grouping files by class type. However, we value both the process (it leads to a deeper insight) and the outcome (solution that is more simple to think and reason about).
Of course, this is just something that seems to work only in a subset of cases I've been exposed to. There can easily be a deeper pattern which I fail to recognize.
Hi Rinat,
This treatise is wonderfully thorough and quite thought-provoking. I loved reading through it and comparing your approach to our own. Many parts of this resonated with me, but of course there are details where we differ. Some of those details are more important than others.
modules
I initially stumbled on some of the terminology you have used here. For example, at Facebook we use the term module to signify a CommonJS module, which has its own closure. This has a very specific JavaScript-oriented meaning for us. But I see now that you are talking about directories used purposefully and meaningfully to support a specific way of thinking about an application.
I think your approach to module-directories could work quite well, given how you have described it. We try to do the same thing with naming conventions, but I would be interested to try the same conventions made more concrete in the directory structure. Like you said, it might help to focus how we think about the various domains in the application.
events, actions and dispatches
More significantly, I am concerned about the use of the term event being used liberally here to describe a number of different processes. While I understand that you might be talking about events in a more abstract sense, I would not want to overlook that Flux dispatches very intentionally avoid being JavaScript events, which are uncontrolled, asynchronous processes.
The mechanics of the dispatcher are vital to how we've been able to keep our applications resilient as they grow, especially the
waitFor()
method, which allows the stores to declaratively depend on one another. JavaScript promises are better than simple events, but we've found they still don't offer us enough control to ensure our code doesn't become a tangled mess. The dispatcher was our solution to the problem. The dispatcher is the core of what makes Flux different from other approaches. It is the mechanism that enforces the unidirectional data flow, helps us manage the complexity of dependencies between stores, and allows the stores to be completely in control of themselves and their data domain.stores: limiting reuse and duplicating code
This is an interesting, somewhat radical idea. But I'm having trouble imagining how different parts of the application that rely on the same data stay in sync with each other over time. I understand that we could duplicate the code in multiple places, but we would need to maintain that duplication, and I can't imagine that would happen without a small failure rate. Perhaps a small amount of duplication is not terrible, however, and like you said, it can provide a way to avoid dependencies between stores.
the past
I loved this:
This really speaks to me. I will always name my actions in the past tense from now on!
Also:
This is what I have been doing recently too, but we use simple action creators that are nothing more than a helper function that sends an action through the dispatcher. Recent projects of mine have been putting them all in one file. The large applications I've seen at Facebook have taken a more domain-driven approach to this, and I haven't yet been part of a large project that put them all in one file. I'm sure the number of the action creators would eventually get to the point where we would need to split them up. On the other hand, why break these into separate files until we really need to do it? It's much nicer to have them all in one place.
testing
It sounds like you test in very similar way to the way we test. If interested, I recently wrote a blog post about testing Flux stores with our automocking unit test framework, Jest.
Thanks again for writing such a thoughtful response. I'll be thinking it over for quite a while, and I'm sure I'll experiment with some of these ideas in my future projects.