by dos1 · 13y ago · 0 comments
This is a maintainability nightmare. I've gone down this route on hand rolled JS frameworks before and it leads to untold headaches. Sometimes a little coupling is the right solution. Many times it is better for components to have knowledge of their environment. In most applications (other than iGoogle maybe) one thing on a page depends on another. I feel it's far better to explicitly call out those dependencies by having a reference to the other components and directly invoking methods on them with known arguments of a known type.
Edit: Also, what kind of memory allocation implications are there with event subscriptions and no references to the components themselves?
I think the sandboxing from aura.js is pretty interesting here, since you can always look up which widgets are allowed to talk, it makes it easier to narrow down the problem.
Yes, you need coupling when debugging. But it need not be in the libraries and code you use.
If instead you declaratively define the state of a component via a shared model then you don't have to care how something was updated, just that it was. Any new way you come up with mutating the object will be reflected in all of the components that watch the object.
Say I want to add a component to a page. Which events do I need to trigger to make it do the kind of things I want? What do I need to pass as a payload to those events? How do I know if there's a consumer for my event or not? If there isn't a consumer that's probably an issue.
The eventing bus adds an additional layer of indirection. If your app can be truly decoupled, then this would be great. But I just don't see that use case being very common. I have never worked on an application where the broadcast model was more benefit than pain in the long run.
Look at the event source.
> When you're debugging in Firebug or someplace, what does your call stack look like?
Look at the data passed to the event. The callstack starts at the function attached as a listener to the event.
Is the event data incorrect? Then the problem is in the code that triggers the event.
Is the event data correct? Then the problem is in the code listening to the event.
> Say I want to add a component to a page. Which events do I need to trigger > to make it do the kind of things I want? What do I need to pass as a > payload to those events?
Documentation. That is indeed the cost of using an abstraction that decouples code, but it's also a cost that has benefits - good documentation is useful.
> How do I know if there's a consumer for my event or not?
It's not important. That's the point of an event-based system, it's a decoupling. De-coupling is an abstraction. It works on pages that are based on components or modules.
If you don't want that abstraction, then using code based on that abstraction isn't a particularly good idea.
Event-based frameworks do need documentation - but reusable code should be documented anyway. There's just more emphasis on getting the documentation done in event-based systems.
> Is the event data incorrect? Then the problem is in the code that triggers the event.
> Is the event data correct? Then the problem is in the code listening to the event.
It's difficult to reason about the event triggering code when it's call stack is not available. For a larger code base where an event is trigger in multiple locations with different logic in each location, it may be difficult to pinpoint the triggering code, let alone reason about that code. Direct function calls are much easier to debug because you can walk up the chain of calls and inspect the variables and state of each frame.
Right, we're on the same page. I agree that in certain scenarios these decoupled event based systems make sense. However, Twitter's example app is an email client. That to me is not a use case I would typically envision as being free of coupling. Thus it's more work to maintain in the long run.
Unless you're extremely new to the game I bet you have, if only in the sense that it's a very short hop from classic MVC design to this.