Live data from Hacker News

useStateMachine: A ½ kb state machine hook for React

github.com

101–103 of 103 posts

Re: useStateMachine: A ½ kb state machine hook for React

#101
post #96
post #39

Earlier quoted context omitted.

...this makes no sense at all.

Why would you be down voting this? Parent's statement is completely nonsensical. If you wanted to test a function irrespective of window.setInterval's implementation you would just PASS window.setInterval as a parameter to that function. You would NOT add a wrapper function for window.setInterval because it is completely useless.

The function you’re talking about would be a react functional component. I don’t think passing window.setInterval in as a prop to a react component is a particularly common approach to achieving component testability - though by all means give it a try.

I don’t think it’s ‘nonsensical’ though to suggest that it’s a fairly common practice in testing to not want your unit tests to actually set up recurring callbacks.

Isolating an external side effect to make it testable, as well as to align its abstraction with the one that your framework uses is a common pattern anyway.

I’m not sure why this angers you so much?

Re: useStateMachine: A ½ kb state machine hook for React

#102
post #32

Earlier quoted context omitted.

I think using a state machine can make the resulting code more elegant in some cases by coupling state changes and effects. But that’s not why they are useful. In my experience, they have been useful because they help map (even mentally) all individual possible states a piece of code can be in, and how the code should behave in each case. In the process, I usually discover and deal with more edge cases, more combinat…

This is important, and it's probably the main reason I like using them so much. But then surely the comeback is that what I'm then attempting to do is produce a diagram in code, when I could just write the diagram on paper, check it, then write some (possibly simpler) code. Not in all cases, naturally, but often?

Yeah, I think you articulated that better than a lot of us here. That, in a sense, your diagram becomes your psuedo-code, or even further, your actual implementation.

My initial defensive impulse to this topic was mostly to take a stand against frontend code that is becoming increasingly obtuse over the years.

Re: useStateMachine: A ½ kb state machine hook for React

#103

As someone that works with finite state machines, I can’t begin to describe how annoying I find it when everyone needlessly reduces the name down to just “state machine”. To me, it makes it seem like the people making “state machines” don’t understand the whole “finite” part and thus don’t understand half the point.

If the term "finite-state machine" were used instead, there would inevitably be people making comments like you, but instead complaining "this isn't finite, you have extra data in the machine that can be infinite". The pedantry isn't helpful.

No, your disingenuous disregard is not helpful. FSM is literally the name of it. Not all FSM's store extra data, that is only a recent concept from things like xstate.
Post reply on HN