Live data from Hacker News

Avoiding Event Chains in Single Page Applications

code-experience.com

51–60 of 64 posts

Re: Avoiding Event Chains in Single Page Applications

#51
post #45

Earlier quoted context omitted.

Unit testing has been done with jQuery since it came out in 2006. Rather than one giant monolithic app, jQuery promotes putting things into plugins or separate libraries. What stops you from unit testing function calls? Nothing.

It also promotes procedural code for modifying DOM, not declarative. This makes unit testing much harder imo, since you're into the realm of either working against a real DOM, or mocking everything.

In the example from the article we are talking about, it would be easy to mock out the one function call for modules A, and B. There's no DOM manipulation in the article.

Keeping DOM modifying code separate is good practice in either case. Then you don't need to involve the DOM in much of your code. Doing end to end testing is also completely ok to do.

It's worth repeating, so I'll say it again... It's better to keep DOM manipulation code separate. Then you don't need to mock, or simulate anything. Except where needed. You do need to test DOM manipulating code somehow.

AngularJS also uses the DOM in their tests: https://github.com/angular/angular.js/blob/master/test/ngAni... Here's a jquery-ui test for comparison: https://github.com/jquery/jquery-ui/blob/master/tests/unit/s...

Re: Avoiding Event Chains in Single Page Applications

#52
post #29

Earlier quoted context omitted.

The flaw with such a strategy is that it does not scale. (We used to do exactly what you described). In a big "eventful" app, you will have way too many events to keep track, and you will constantly be browsing trough different modules to understand the chain. Lets assume the user clicks somewhere, so an event is fired "user-clicked-x" that model A listens to. So you know that model B needs to change as well and you…

This I can well believe, but I'm interested in this bit: > you fire a new event type from model A, say "model-a-changed-because-of-user-clicked-x" or a more generic event, say "model-a-changed". These two seem to be on opposite ends of a spectrum where I'd try to pick a middle point. I wouldn't want the semantics of "this is a reaction to a UI event" anywhere past the first event, it's way too detailed. I'd try to pi…

This still introduces a new link, which will overload your brain if you have too many of those.

To give you some context:

In the app I'm developing now, we've got roughtly 25 global user interactions (meaning an event that will be triggered with a DOM event).Most of these events affect more than one model. Quite a lot of those also trigger complex event chains.

Multiple models have complex dependencies that would form branched dependency chains with conditionals.

It's just impossible to have everything in your mind at all times, which you need to if you want to extend a chain without bugs.

Now you might not have this issue in your projects, because they don't require that kind of interactivity. If thats the case don't bother, keep doing what you are doing.

But if you ever realize that things get pretty complex in one of your projects, then you know where to start :).

Re: Avoiding Event Chains in Single Page Applications

#53
post #47
post #39

Earlier quoted context omitted.

Explain how smaller, simpler and faster code is worse.

Smaller: Not really. You just avoid some K of helper code already build (by the React team). Your code will end up being more than the one you'd have written on top of React -- because it will also have to reimplement and handle all common cases (either poorly, or time-consumingly). Faster: could be, could be not. Without profiling this is an empty statement. Besides, "premature optimization is the root of all evil".…

The code I mentioned is definitely smaller than in the article.

It is faster, because it is just a function call. It doesn't create any event objects, or have to go through six layers of other function calls.

It's simpler because there is less code, and there are less concepts.

No, it's not novel or new. Messages as function calls, and MVC comes from 70s smalltalk.

It's not strongly coupled because of a few reasons. Firstly because in JavaScript it is simple to dynamically reassign objects and functions. The receiving object does not care where it got the function called from. Only the sending object knows where it is sending to. However, from Coupling theory in Software Engineering, we know that if there are a small amount of outputs then the strength of the coupling is still low. Luckily in many apps there are often only a few places where you want to send a message. http://en.wikipedia.org/wiki/Coupling_%28computer_programmin...

Yes, if there are lots of outputs it may be useful to reduce the coupling. In my experience, even with large apps (100 person teams), this is often not the case.

It is often useful to know what your app is doing by looking at where the events are going. Just using function calls makes this very easy, both in source code and in the debugger. So this also needs to also be weighed up in the decision of how you are passing events around.

Re: Avoiding Event Chains in Single Page Applications

#54
post #34
post #20

Earlier quoted context omitted.

> A updates because "user-clicked-x", and B updates because A updates. But if you're working on B, you ask yourself why the heck did A update in the first place? Stack traces are a thing. Chrome even has asynchronous stack-traces. A updates because "user-clicked-x", but it might also update because "user-clicked-y". Or because "user-clicked-z". So now I have 3 things to add to A and to B, because B needs to refresh e…

> Stack traces are a thing. Chrome even has asynchronous stack-traces. Now this argument line has dissolved into plain sillyness. He showed how he made the congnitive load less and the dependencies more explicit and locally evident IN THE CODE, and you tell him to use "stack traces" for that?

From what I understand, this methodology increases code duplication, which leads to higher risks of bugs later on.

I'm trying to understand whether this is truly the case, or if I'm missing something.

Re: Avoiding Event Chains in Single Page Applications

#55
A model listening to other models? Yuck, that's just wrong. In an MVC application, only controllers should update models. Just because Backbone lets you listen to model events from anywhere doesn't mean it's a good idea.

IMHO Application events should simply bubble up through the DOM, and get caught by appropriate controllers (Backbone.View class in backbone parlance) that then modify the appropriate models. If need be it's the top-level controller for the app, eg:

document.body.addEventListener(function(e) { modelA.doSomething(e.data) modelB.doSomethingElse(e.data) });

I'd take a simple approach like that over an event bus/dispatcher/coolest-new-pattern-since-sliced-bread - any day, and sleep at night knowing if something happens to me, any employee will be able to parse and debug my code.

PS And no, I don't use Backbone, but my own 500 LOC MVC framework that's got zero dependencies and is about 100x faster.

Re: Avoiding Event Chains in Single Page Applications

#56

A model listening to other models? Yuck, that's just wrong. In an MVC application, only controllers should update models. Just because Backbone lets you listen to model events from anywhere doesn't mean it's a good idea. IMHO Application events should simply bubble up through the DOM, and get caught by appropriate controllers (Backbone.View class in backbone parlance) that then modify the appropriate models. If need…

Backbone is considered a high-quality javascript project, written by a very talented and experienced developer.

Could you explain what you do differently in your MVC framework to achieve those gains?

Re: Avoiding Event Chains in Single Page Applications

#57
post #53
post #47

Earlier quoted context omitted.

Smaller: Not really. You just avoid some K of helper code already build (by the React team). Your code will end up being more than the one you'd have written on top of React -- because it will also have to reimplement and handle all common cases (either poorly, or time-consumingly). Faster: could be, could be not. Without profiling this is an empty statement. Besides, "premature optimization is the root of all evil".…

The code I mentioned is definitely smaller than in the article. It is faster , because it is just a function call. It doesn't create any event objects, or have to go through six layers of other function calls. It's simpler because there is less code, and there are less concepts. No, it's not novel or new. Messages as function calls, and MVC comes from 70s smalltalk. It's not strongly coupled because of a few reasons.…

>The code I mentioned is definitely smaller than in the article.

Yeah, so this is a first sign of misreading the dangers.

Of course the code you mentioned is "smaller than in the article". The code in the article is a toy example to illustrate a specific point.

It's when you start to build a full app, with all the (necessary for the requirements) complexity that it's gonna get much more and more unyieldy that the code one would write on top of React.

>It is faster, because it is just a function call. It doesn't create any event objects, or have to go through six layers of other function calls.

Again: premature optimization. After you build your colossal temple of function calls in a NON TOY example, it will either be slow (because you'll have re-introduced abstractions and layers by yourself) or it would a complex spaghetti of cross-calls.

>It's simpler because there is less code, and there are less concepts.

Less concepts != simpler. Assembly has less concepts too.

And it's only "less code" because you're comparing a "toy example + framework" with a "toy example without framework". It's after that level that it gets hairy.

>No, it's not novel or new. Messages as function calls, and MVC comes from 70s smalltalk.

Yeah, and it's what all these coders going to React etc have tried already for a decade and found out that it doesn't cut it with modern apps, because the environment they run in is nothing like a Smalltalk MVC application.

Re: Avoiding Event Chains in Single Page Applications

#58
post #51

Earlier quoted context omitted.

It also promotes procedural code for modifying DOM, not declarative. This makes unit testing much harder imo, since you're into the realm of either working against a real DOM, or mocking everything.

In the example from the article we are talking about, it would be easy to mock out the one function call for modules A, and B. There's no DOM manipulation in the article. Keeping DOM modifying code separate is good practice in either case. Then you don't need to involve the DOM in much of your code. Doing end to end testing is also completely ok to do. It's worth repeating, so I'll say it again... It's better to keep…

With React there really is no notion of manipulating DOM, so there is no need to test it.

Re: Avoiding Event Chains in Single Page Applications

#59
post #22
post #9

Earlier quoted context omitted.

How? Everything about this seems incredibly straightforward

Call me old fashioned, but function calls are pretty nice events. Rather than heaps of code, and weird concepts, you can just implement firing the event into the A, and B models by calling a method on each. Simple! A.getFilteredDays(payload); B.getFilteredDays(payload); That's only two lines of code compared to 30 or so. This scales up because with more models, you have less code. In practice these are easier to debu…

I'm with you, bub.

The people arguing against you don't know what they're talking about. They're stuck in a paradigm where "de-coupled event-driven architecture" is a holy goal and can't see their feet on the ground anymore.

Re: Avoiding Event Chains in Single Page Applications

#60
post #51

Earlier quoted context omitted.

In the example from the article we are talking about, it would be easy to mock out the one function call for modules A, and B. There's no DOM manipulation in the article. Keeping DOM modifying code separate is good practice in either case. Then you don't need to involve the DOM in much of your code. Doing end to end testing is also completely ok to do. It's worth repeating, so I'll say it again... It's better to keep…

With React there really is no notion of manipulating DOM, so there is no need to test it.

Of course it eventually touches the DOM. It also has a virtual DOM.
Post reply on HN