83: David Khourshid on XState, Statecharts, and the Future of Designer—Coder Collaboration
David Khourshid is the man bringing statecharts to the frontend.
We talk about XState, model-based testing with xstate-test, and the future designer/coder integration
For the month of february, we're chatting exclusively with Reactathon speakers.
You can hear more from David on the topic state-charts for UI development this march.
Reactathon is a top React conference in the heart of San Francisco.
David and I will be there, chewing people's ears off about better designer tooling.
Get a ticket at reactathon.com.
Featuring
Links
- XState
- Redux
- Constructing the User Interface with Statecharts by Ian Horrocks on Amazon
- Statecharts: A Visual Formalism for Complex Systems by David Harel
- Welcome to the world of Statecharts by Erik Mogensen
- RxJS
- Lodash
- Finite-state machine on Wikipedia
- State diagram on Wikipedia
- SCXML — State Chart XML on Wikipedia
- mukeshsoni/statechart-calculator
- A javascript calculator built with XState and Vue — built on the statechart above
- React Context
- RxJS Observable
- Concurrent computing on Wikipedia
- Cooperative multitasking on Wikipedia
- Screen tearing on Wikipedia
- Deterministic system on Wikipedia
- Beyond React 16 | JSConf Iceland by Dan Abramov
- Actor Model on Wikipedia
use-subscription- Write Fewer Tests! From Automation to Autogeneration — David's announcement of Xstate/test at React Rally 2019
- Model-based testing
- @xstate/test
- xstate-test-demo
- Given-When-Then on Wikipedia
- Cucumber syntax on Wikipedia
- Adobe Xd
- Sketch
- Sunil Pai on Twitter
- inVision
- Framer
- Sketch.systems
- Subform
- Storybook
- useState
- useReducer
- useMachine from @xstate/react
- keyframers
Transcript
[00:00:00] Michael Chan: This is React Podcast. I'm Chantastic. This is episode 83 of React Podcast. Today we chat with David Khourshid, the man bringing state charts to the frontend. We talk about XState, model-based testing with XState Test, and the future of designer-coder integration. For the month of February, we're chatting exclusively with Reactathon speakers. You can hear more from David on the topic of state state charts for UI development this March. Reactathon is a top React conference in the heart of San Francisco. David and I will be there chewing people's ears off about better design tooling. Get your ticket at reactathon.com. David, welcome to React Podcast.
[00:00:51] David Khourshid: Thanks for having me.
[00:00:52] Michael Chan: It's so good to have you back. I think uh you were on a couple years ago. Mm-hmm. And I think also, kind of just at the beginning of your like state chart revival that you've been starting. Yeah. Um, and uh, but that was with Michael. And so I kind of want to like, just uh get you back on, talk about like what you've been doing since. So, um, yeah. So what's uh, what's, what's new in your world right now and with, uh, you know, talking and state charts and all that kind of stuff?
[00:01:18] David Khourshid: Well, to be honest, not much. I've just been going on with state charts and really working on developing XState and seeing how many, many other developers are using it because It's gained a lot of popularity for whatever reason. Like I, I've always said that XState is sort of Redux with extra rules. So I'm like, why would anyone want to use this? And uh turns out a lot of people want to use it. So yeah, that's where a lot of my focus has been. And also just, you know, I'm thinking of new ways to use it and new applications for state. Statecharts in general.
[00:01:56] Michael Chan: Awesome. Now I know that you have you've said over and over again that statecharts are not a a new thing and like finite state machines like definitely not a new thing. I mean, if you had to, I guess, make some assumptions about why now seems to be like a good time for them or like why people are more comfortable with adopting them, like what would you theorize as the reason?
[00:02:19] David Khourshid: Well, that's a good point. In fact, I'm not even the first person to talk about statecharts and user interfaces. There's this book by Ian Horrocks called uh "Constructing the User Interface with Statecharts," which was written way back in 1998 or 1999, and statecharts themselves were created in 1989 by David Harrell. And uh in fact, I watched a talk a while ago by Erik Mogensen who talked about this idea of using statecharts and user interfaces, and that was back in the um early 2000s or 2010 or 2014 or something like that. And uh he actually created this resource called The World of Statecharts. And uh it it just includes so much useful information about why you should use statecharts and the basics of all of these different terms that were introduced by David Harold in 1989. But getting back to the original question, why now? I think We've been lost for a while, and not just us in the web development world, but also in the mobile development world and also other areas of tech, just thinking about how should we manage states. And so instead of having a solid foundation and a, a, like a guiding points for how we should manage state, instead we develop these nice developer APIs where it's like, hey, I made it really nice for you to mutate or really nice for you to send some sort of action that lets you change state wherever you want. Or you could put all of your state in this global store and you could time travel because, well, actually, I don't know, I don't know many people who actually do time travel debugging regularly. But, and, and so a lot of people have started off with this and used um tools. I'm not gonna mention them because I don't want to, you know, talk negatively or be biased against any sorts of tools, but these tools that uh that seemed like a good idea at the time, but then they gradually learned that as their app became more complex, state became harder to manage. Mm. And that's why instead of coming up with these innovative new ideas for how should we manage state, I decided, you know what, let's look at what people have done years and years ago and let's look at whether what other areas of technology have um have discovered, such as game programming and stuff like that. And the the answer sort of lies in this events-based way of thinking, where you have these finite states that change and transition based on these events. And so you could sort of think of everything as just events that happen. And that really simplifies the mental model.
[00:05:01] Michael Chan: Interesting. So so in a way, like the complication of kind of where we're at in the front-end space with, you know, a lot of asynchronous things happening and having to like sync up state from, you know, you know, server changes and, you know, with the stuff that you're doing, you know, right in front of you, it's kind of made the problem feel really big. Because that's like a lot of what we're doing all of the time. Right. And the current tooling just doesn't really have a lot of answers for that beyond like, like you said, like a nicer API. To kind of shoot yourself in the foot with.
[00:05:37] David Khourshid: Right. And I, I think it comes down to treating time as a first-class citizen, or more specifically, treating time as an event. And RxJS actually does this really well where they do treat time as a first-class citizen, which makes a lot of concepts like debouncing, throttling, uh delays, and things like that a lot easier to deal with. But if you take a state management library living in a single-threaded world like JavaScript, and then you have to deal with things like uh fetch requests or any sort of async requests, then that becomes a special- a special case thing.
[00:06:17] Michael Chan: Sure. Yep.
[00:06:18] David Khourshid: And that, that's the unfortunate reality is that, oh, you could just add middleware to do this, or oh, you could just use this custom hook that will, um, you know, handle this fetch request for you. But then you have all of these other use cases which aren't covered by that. So the idea of just thinking of everything as an event, even time, really again simplifies uh the mental model.
[00:06:42] Michael Chan: That's interesting. So you mentioned, you mentioned RxJS and I have like almost no experience with RxJS, but is that, does like statecharts and XState, does that live kind of like in the same space as RxJS or like, could they like partner well if you were to, you you know, try to use them together?
[00:07:00] David Khourshid: Pretty close. So RxJS is like Lodash for asynchronous, uh like an asynchronous collection of items. For example, an array would be something that's synchronous. You get everything at once, but an observable, you might get every item at different times. And so that's why I said it takes time as a first-class citizen. And so you could think of each of those values emitted over time as an event. And so XState and statecharts and state machines do live in that same world where you have to assume that all of these events can happen at any time. And every single time you have an event happen, that causes a state transition immediately. And um yeah, so in fact, uh XState does exports to observables, so you could use it directly with RxJS or any other observable library.
[00:07:53] Michael Chan: Interesting, interesting. So, I want to back up for for anyone who this is like their first introduction to to state charts and finite state machines and all that. I want to kind of like define some of those terms and maybe separate, tease them out from from one another. So, XState is your library for managing state charts.
[00:08:14] David Khourshid: Yes.
[00:08:15] Michael Chan: What is what is a state chart?
[00:08:17] David Khourshid: So To define a state chart, we have to quickly define what a state machine is. So a state machine is a collection of finite states where you could be in only one of those states at a time. And the way you transition from state to state, such as from loading to success or loading to error, is by an event. And so, for example, if you're in the loading state and you get a resolve event, then you'll transition to the success state. Now, this works pretty well for most common applications, but um states can get pretty complex. And then you have this thing called state explosion, where you you have uh many different states that could transition to each other, and it just becomes combinatorially explosive. And uh you know, there's just many, many different ways of connecting each of the states. So state charts help with that by introducing things like nested states. States, parallel states, uh actions, history states, and things like that. So in short, you could think of them as hierarchical state machines or nested state machines, but it's it's more than that.
[00:09:25] Michael Chan: Interesting. So a state machine is kind of like a- or a finite state machine is like kind of like an isolated set of things that could happen in like a certain sequence or or or whatnot. And state charts is maybe like a mechanism for kind of orchestrating different state machines and communicating between them?
[00:09:43] David Khourshid: Well, state charts state charts are basically the the same as state machines. In fact, you could decompose a state chart into a state machine. It's just a different way of representing a state machine for more complex use cases. That's all.
[00:09:57] Michael Chan: Interesting. Interesting. So when we're like, so if I'm looking at the, you have a tremendous visualizer for for XState. Uh state charts, um if I'm looking at that, like what I'm looking at is kind of like a composed uh state chart. Is that correct?
[00:10:13] David Khourshid: Yeah, exactly. So each of those states that you see, if you look at the visualizer, each of those states can contain states of their own. And each of those states could contain states of their own as well. And any of those states can transition to any other states. And so uh with the state chart, if you're in a child state, then you're also in the parent state. So that, that's what that means.
[00:10:37] Michael Chan: Interesting. Mm-hmm. Interesting.
[00:10:39] David Khourshid: Trying to think of a good example. Nope, I got nothing. I'll think of something.
[00:10:44] Michael Chan: Okay, cool. Now, something that kind of attracts me is, is the, the popularity of, of XState is increasing and knowledge about state charts and whatnot. It seems like there's There's a lot of power in this idea of encapsulating your logic, whether that be, you know, kind of uh stateful component logic or the logic of your application, um in something that is not a framework. So, you're when you encapsulate in statecharts, it seems like you now have this kind of like uh isolated chunk of logic that can be like applied to different frameworks. Uh is that something that you're kind of seeing as people start adopting statecharts?
[00:11:30] David Khourshid: Yeah. And in fact, that was one of the original goals. So one of the great things about statecharts, it's it's truly framework, language, even technology agnostic. People have been using statecharts even in embedded electronics for decades now. Um, and in fact, uh with XState, that was a main goal in mind. XState is based on the SCXML, which stands for Statechart XML which of course is also language, framework, implementation agnostic. And what that means is you could define your logic in these, uh, what I call machines, that it's just a big JSON config object, which some people have expressed discontent with. They're like, I don't want to write a big config object. Um, even though we, we do a lot of it every day. I mean, look at Webpack, look at- Yeah. can like DevOps, stuff like that. But anyway, and of course there's nicer ways to write XState. Um, what, what some people have done is they would take an example state machine that either I created or someone else created, and they would use it in different uh frameworks such as Vue or Svelte. I saw this one example recently where they're on the, uh on the XState documentation there's a calculator. Now, making a calculator and programming one is a lot harder than you would think. You might think, okay, if the user presses 3 5, then we just calculate that and, you know, go with it. But think about what if you press 3++ plus plus plus 05, then you really have to think about how that's supposed to work in a calculator, and that's where state charts help. So needless to say, there's a pretty complicated state chart for that. And uh this example was made in React about a year ago. Recently, someone took that exact same machine and ported it over to Vue. So all they had to do was connect the Vue- Vue- the Vue- Vue- the Vue UI, you know what I'm talking about, and uh connect that to the exact same state chart. And all of the logic worked exactly the same.
[00:13:40] Michael Chan: Interesting. So this is a way of kind of, I don't know, almost like betting against the framework wars. if if you wanted to think about it like that. Uh where your your app has like business constraints. And that's the thing that you're— like part of your job is like mapping those business constraints to like programming constraints. And it seems like this kind of sits really cleanly in the middle of like, hey, we're gonna make a mapping of of all of this logic, all of the things that it needs to do. And like it doesn't matter what framework we implement it in, uh because that's just like a like a implement UI implementation detail.
[00:14:15] David Khourshid: That's exactly the point. Do you remember when React was advertised as just the view?
[00:14:21] Michael Chan: Yep. Yeah.
[00:14:23] David Khourshid: Like it's nothing else. It's like, okay, this is why React is so bare bones. It's, uh, it's not trying to be a controller. It's not trying to model your data or provide built-in forms or logic or anything. It's just saying, you know what, the view is a function of states. And so I'm trying to return to those roots. And I think that frameworks, whichever framework a developer chooses, should simply be a both a function of states and also a way of providing these events to some sort of central data model, like a state chart. And uh yeah, just being an event emitter and an event receiver. Basically.
[00:15:06] Michael Chan: That's awesome. I I I'm I'm glad that you mentioned React and kind of the the the original roots of, you know, it being the V and MVC and and whatnot. Now I know that there's been, so there's been a lot of transition in React specifically to become more of like a full-blown framework. And we've seen a lot of that with context. And I know that like I I've seen a couple conversations where you've, you know, had some concerns about the the API of context and whatnot, how would you see like an alternate context with state uh state charts and state machines in mind?
[00:15:42] David Khourshid: So it's funny that you mentioned React context because I think that React context is one of the most powerful um concepts that React has, like the ability to share data in any component. But I feel like it's also heavily misused and it's misused because the React team themselves say in the docs that context should be used for data that doesn't change that frequently, such as a theme or something like that. Mm. Now, with, uh, in particular with my XState integration, I sort of take that idea to the next level. And this is something that I'll also mention soon in a tweet or talk or something, but the the fact that you could pass anything in context, which means Um, you could pass an observable in context, you could pass something subscribable. And so the the way that statecharts are shared uh with many different components and the ways that I mentioned in the docs are that you have this, it's called an interpreted machine. It's basically this service that you could send it events and it it emits a state. And so you could pass that object, that single object in context, which is gonna be shared by any component that consumes it. And so those components can then subscribe to it. What this means is that any in-between components are not ever gonna be re-rendered because that service is always going to stay the same. And so you have huge performance benefits and you just have a way of of broadcasting the states to any component that cares to receive it.
[00:17:19] Michael Chan: Fascinating. Now I'm gonna I'm gonna push you a little bit further and talk about even more uh React features. I know that for the On the horizon for the past like couple of years, almost the same amount of time that that you've been spreading the gospel of statecharts, uh we've been hearing about Suspense kind of, you know, being being on the horizon and being the future of React and whatnot.
[00:17:41] David Khourshid: Right.
[00:17:42] Michael Chan: How do you see that in relationship to kind of like the state the statechart vision of, you know, map your map your application logic in statecharts and then have, you know, React just handle the UI.
[00:17:54] David Khourshid: So I'll be honest, when I first heard about Suspense, uh, or when, when I first saw Dan give a talk at JSConf Iceland, I was really excited. I was like, wow, this looks like a really cool feature that they're building right into it. And then the more I learned about Suspense and the more gaps I saw with it, the more I grew distaste for it. And then once I started diving into it again, the more I realized like these are some pretty cool paradigms hidden by some unfortunate abstractions. And so those paradigms are uh concurrent programming. Mm-hmm. So concurrent programming just means uh things that coordinate with each other at the same time. And uh I made this mistake before, so it does not mean that you need multiple threads, you know, talking to each other or things like that. These could be uh virtual threads. And uh Suspense in concurrent mode is about pausing some work and re-continuing work, you know, at a different time, prioritizing work, and just basically coordinating with each other. Um, the way that this fits in with state machines and state charts is sort of the same way that it would fit in with any sort of external data store that you could subscribe to, that you could receive data from, because that's essentially what Suspense is pushing React developers towards. where instead of having React manage all of this data and all of the fetching and all of this subscription stuff, instead you say, leave it to some outside resource that's eventually going to throw a promise or something to indicate to, you know, a Suspense boundary that, hey, I'm ready, I have data for you. And so this idea is actually pretty cool. And um, it's, it it works a little bit weird with uh with observable objects because Uh, there's this thing called, um, I mean, I don't want to talk about it too much because I I haven't gone too deep into it, but there's this thing called tearing where it's like if you're subscribing to something, then two different components, because they're resuming work at different times, might get a different value or a stale value. So you need some way of getting the current value of all that. Anyway, needless to say, if you didn't understand any of that, then you're on the same page as me because I feel like it's going to add a lot more confusion to how we create React apps. And by that confusion, I mean a lot of developers are going to try something or do something that they're used to, and then someone's going to say that's not concurrent safe, and that means nothing to the majority of developers. It's just a blanket statement for you're doing things wrong. And that's, that's one of the unfortunate side effects that I see from all this.
[00:20:35] Michael Chan: Interesting. So it kind of So the concern is that it's a- it might be a little bit of a leaky abstraction because you have to know a lot about the underlying system in order to actually program against it safely.
[00:20:49] David Khourshid: Right. So that's why in that particular aspect, I feel like it's sort of at odds with state charts. The whole idea of state charts is that given a state and given a set of events that happen, you could 100% deterministically know what the next state is going to be, what the next side effects are going to be, and what could possibly happen at that state. With Suspense, it's different. There's uh it's non-deterministic, which means that there is no way of knowing what's going to happen next. If you start fetching data, you don't know what your UI is going to look like next. It might look like nothing, it might look like a loading spinner. It might show one component before the other, and that adds a lot of complexity. And I think that this is a little bit unfortunate because React's whole model was UI is a function of states. And now React is becoming UI is a function of maybe states depending on what we feel like showing you at a given time.
[00:21:52] Michael Chan: Interesting. Interesting. Now, from your- I guess from from your following of kind of the the um progress of Suspense. Have you seen things that Suspense handles that wouldn't be able to be handled inside of a state chart? Or is it all handling stuff that you, if you were a diligent enough programmer with regards to your state charts, you'd be able to handle?
[00:22:15] David Khourshid: Well, of course, anything can be handled in anything. You know, we, uh, we're, we're working in like Turing complete languages here. So, um, however, with Suspense, there are things that are sort of built in which, uh, which rely on priority. And so, for example, if the user is typing something, that's going to take priority over some other thing. And these are- Hmm. These are intuitive things that React knows that the developer might not know. And so if you, if you deem something as high priority and other things as low priority, the internals of React in concurrent mode would be able to say we're going to focus on those high priority things and pause the low priority work until the high priority work is rendered. However, my criticism to this is that 99.5% of React apps will not need this. Mm-hmm. In fact, the demo given at uh JSConf Iceland in 2018, I believe it showed that, um, I don't know, it was just like a heavy graphics processing application where things were happening at the same time as the, you know, typing in inputs, and you had all these graphs showing on, and just a lot of CPU-intensive work. Now, of course, you could hand that off to React Suspense and say, I'm going to let you prioritize this so that the UI stays smooth. But if you're a developer working on this, or even a team of developers, you might think, you know what, React- Hands off, I know how to optimize this, I know how to keep it responsive. I might use a web worker, I might use something else. So there's really going to be, uh, you know, two different sides to it depending on how much control the developers want.
[00:24:01] Michael Chan: Yeah, so it seems almost like there's this, um, maybe something happening right now in React where React, I guess, by by the needs of Facebook, is needing to control more of this stuff automatically. And uh kind of like make decisions in terms of you know UIs that that require like high, high, high performance for some reason or another.
[00:24:23] David Khourshid: Right.
[00:24:23] Michael Chan: Um but that doesn't actually prevent us from using React in um with other libraries and kind of considering it just the view layer.
[00:24:33] David Khourshid: Right, exactly. And and that's why the whole idea with statecharts and other things that you subscribe to is that you get this value and you could send it values. So uh this is sort of the basis of the actor model, which is a very, very old um model of computation. It's a mathematical model of computation where you have these entities where you could send events and receive events from, and they could communicate with each other. And so in effect, a a state chart, an interpreted state chart, is just that. You're sending in events and you're getting stuff back. Mm-hmm. And you could consider so many things to fit this exact model. For example, an input on a page, you send it events by typing in it or focusing, blurring it, whatever. And it could also send you events back via callbacks, such as onChange, onClick, on whatever. And so a lot of things fit this model. And um and yeah.
[00:25:32] Michael Chan: Interesting. Now I know that Suspense and Concurrent Mode are kind of like two different things that kind of often get overlapped. Is there anything in like concurrent mode specifically, like to your knowledge, that would make using statecharts difficult to use with just like vanilla, you know, React as a view?
[00:25:49] David Khourshid: The only thing would be, like I mentioned earlier, the concern with what's called tearing, which is having stale state in two different components or two different areas. And uh that's solved by just telling React that there's a way to get the current value. And that's why React does provide a hook called useSubscription, which is helpful for this. And right now we're testing the XState React hook using that hook. And that hook just provides a way to, um, to prevent this tearing in concurrent mode. So it's all very confusing still.
[00:26:25] Michael Chan: Yeah.
[00:26:25] David Khourshid: I like, I like half understand it. So I know that people are gonna listen to this podcast and be like, you are wrong about this, this, and that. I, I know I wrong. You can send me your angry emails later.
[00:26:36] Michael Chan: I know, I feel that way pretty much every time I have a conversation with someone. So I'm glad that it's the two of us.
[00:26:42] David Khourshid: Yeah.
[00:26:43] Michael Chan: Um, interesting. Well, it'd be interesting to kind of like see how that, see how that progresses as, you know, concurrent mode and and Suspense all get, all get rolled out a little bit more. So last year, I think it was last year. At uh at React Rally, you had um uh announced a new extension to uh the XState world of libraries, and this is XState Test. Uh could you describe that to me?
[00:27:11] David Khourshid: Sure. So XState Test is based on yet another model. Not nothing I put out there is new, really. But this is called the uh the model-based testing technique. And so it's sort of like end-to-end tests, except the whole premise of it is that you don't write each of these tests manually. Instead, you have an abstract model, which is described as a state machine or a state chart, and you generate all of the possible paths from one state to another state using this model. Now, this model is just a directed graph, so you could use like a depth-first or breadth-first search algorithm to do this pretty easily. Well, pretty easily as in provided by the library, it's just one function call. But um by doing this, you could generate full coverage for all of the possible paths that a user could take in your application without having to write each of those tests. And what's more is that if your app changes, like due to changing requirements or we want to add this feature, remove this feature, then all you have to do is change the abstract model and say, hey, I have this new feature, and all of those paths are going to be regenerated. And so this idea of generating these tests based on a model really saves a lot of time. And there's actually a lot of tools in different languages that do this model-based testing, including some commercial tools as well. And I just want to introduce it to the frontend world.
[00:28:40] Michael Chan: Interesting. Now, the example that you gave was, um, I thought, really clever. It was um, and we'll link the talk so people can actually see it in action, but it was a tic-tac-toe uh game. And there's- I don't remember, you might remember off the top of your head how many combinations there are for possible actions in tic-tac-toe.
[00:28:58] David Khourshid: Something like 51,000-54,000.
[00:29:01] Michael Chan: And like, no one would ever write 51,000 tests to make sure that all of, all of the cases didn't produce some type of error. And so this model-based testing, it seems to I guess kind of take what you're doing with state charts even further and extrapolate out all of the possible states, you know, given the model.
[00:29:21] David Khourshid: Right.
[00:29:21] Michael Chan: Now, where are some- like in in practical like web development, where are some places where you would see like the biggest bang for buck like in this type of testing?
[00:29:33] David Khourshid: Well, in any complex application. In fact, I've used it in a few applications that I had to uh create for Microsoft where there's just so many different things that a user could do and so many outcomes. Like you could imagine an application where uh you have to start some sort of process. So a user would select it, click, uh create it, wait for the API to create it, come back to you with the results, and then maybe uh check that it's running and eventually- I I I'm just describing a whole bunch of abstract use cases, but just- Sure. A lot of things happening asynchronously. And um even like regardless of if the application is written with React or with XState or anything in mind, you could still create these state charts on the side, generate these paths. And what I did was I hooked it up to Puppeteer and I was able to verify the behavior of the what's called the system under test, which is basically the the actual live app running in a test environment and be able to verify that every single path can be reached, including if an error can happen, uh, is that reachable or is that expected? Things like that.
[00:30:44] Michael Chan: Is it a pretty big shift to go from, uh, kind of very specific granular testing, which, you know, developers are maybe a little bit more accustomed to, like kind of building up the world in their mind, testing that assumption, and then kind of like going on to the next test, uh, like What's the transition like moving to this model-based uh testing where you're kind of testing your application a little bit more like at a higher level or like kind of like in theory, I guess? Like, is that a difficult transition for people?
[00:31:15] David Khourshid: Oh, it's a huge shift and it's definitely a a learning curve and a mental shift. I wouldn't say it's a difficult transition because you're no longer- you're you're sort of hands-off, right? This is sort of the autopilot of testing. You're no longer saying, okay, let's write every single step. I, I don't know how many end-to-end tests you've written, but I'm sure you've written somewhere. It's like, okay, I like all of these tests have the same first 3 steps. I'm just gonna copy and paste them or, or try to get clever and abstract them to a function, even though you really shouldn't do that. Copying and pasting is pretty much the best way to go when- Yeah. Writing tests like that. But instead you're being hands-off and you're saying, As a user, this is how I understand the app to work. When I'm in the state, here's the possible things I could do. Okay, now when I'm in this state, here's these possible things I could do. And then based on that, you give it to this test generator and it generates all these tests. An analogy I like to make is it's sort of like, what if Google Maps only works by, you know, you you tell it, hey, I want to go here. And then it sends it to a human, and a human gets back to you like 30 minutes later and tells you directions. That's not how Google Maps works. Google Maps knows, or hopefully it knows, every single uh road and how the traffic is and all the different ways that you could get from point A to point B. So when you ask it like, hey, uh what are all the possible ways I could get here? Google Maps might tell you, well, you could take the highway, or uh that's a little bit trafficky, so you should go this way. And just generating all these possible paths. So that's the model-based testing approach.
[00:32:55] Michael Chan: Interesting. It sounds, as you describe it, it sounds a little bit more like training up someone who's actually manually testing up your product and like describing to them like, hey, these are, these are the things that you can do in this state. And, you know, almost more like you're talking to a human who's gonna be like testing this product.
[00:33:12] David Khourshid: Right, right. If you've heard of, um, Cucumber tests or this style of given, when, then. It's basically saying given, when, then for every possible state, and then just generating full plans based on those schematics.
[00:33:27] Michael Chan: Fascinating.
[00:33:28] David Khourshid: And from that, from that, you could even go beyond tests.
[00:33:31] Michael Chan: Yeah.
[00:33:32] David Khourshid: And that's sort of what I'm gonna be talking about in my talk.
[00:33:35] Michael Chan: Killer. Now, one last question I have about, about that, and is priority. Are you able to like prioritize certain paths that are maybe, um, I guess, for a lack of a better word, like more common than the others in your testing?
[00:33:51] David Khourshid: Yeah, absolutely. So, um, you know, going along with directed graphs and graph theory, there's this idea called weighted graphs. And so what you could do is you could tie analytics to your graphs and say, okay, here's all of the possible routes that a user could take throughout the app. However, here's the most common ones that a user could take. And so based on those analytics, you could prioritize different tests and there are built-in ways to do that in XState Test. I just haven't documented yet because I'm also working on a tool called XState Analytics, which allows you to capture those analytics. And that's not quite out yet.
[00:34:27] Michael Chan: Fascinating. Fascinating. Well, I'm excited to see that because all of your other projects have been amazing to amazing to witness so far. Now I wanna talk, uh, kind of transition a little bit, cuz I know that you're gonna be talking a lot more about this- the next things that we could talk about in your talk at Reactathon. So, I kind of want to transition to like a different direction and talk about, I guess, maybe the theory of where- or theorize how something like XState and statecharts could map to design tooling. I know that a couple months back, I talked to with Sunil Pai and we were just kind of like going off about how crappy design development integration is right now. And talking about the ways that we could do better with regards to design systems. But those are pretty static. And it seems like in everything that I've seen from statecharts, these are really like flow documents. You know, like- Right. And they look pretty similar to what you might see in something like Adobe XD or um some type of plugin for Sketch that allows you to show like this frame going to this frame and this state and all that kind of stuff. With your visualizer, have you thought about how that might map to some type of like Adobe XD type of tool and like actually like connecting, you know, mocks or screenshots and actually having some type of usable state chart in the end?
[00:35:54] David Khourshid: Oh, definitely. And by the way, Sunil Pai is full of wonderful ideas. Like, yeah. Um, so, so I, I really recommend just following him and Uh, listening to what he has to say. But yeah, um, I have recently- it's sort of a funny story- I've recently been introduced to a couple design tools, like sort of a private testing sort of thing where, uh, these companies reached out and they were like, hey, can you please try out this design tool? Let us know what you think about it. We want to demo it for you. And I always say the same thing to all of these design tools, because truth be told, the space is getting pretty crowded. It seems like every other day there's a new design tool popping up which promises easier handoff integration with React or any other framework. By any other framework, I mean React, because let's be honest, we're all just building for React, which sort of misses the point entirely.
[00:36:50] Michael Chan: Yeah.
[00:36:51] David Khourshid: Um, and and it's all these, it's like, it's like the same way that you would code a React app, but harder because now you have to learn their visual tool for doing so. And then I'm like, well, does it generate code? They say yes. And then I say, can I edit the code? And they say, yeah, you probably shouldn't touch the code. I'm like, well then what's the point? So, um, one of the biggest things that I see all of these design tools have in common is it's all about what a component looks like, just period. Mm-hmm. Not at what can you do with the component, like what, what does the component look like in different states? How can the component transition between states? So what I'd like to see is a design tool that even allows us to say, hey, I want to create a login page or I want to create a form wizard. And I, besides just designing it, I want to really visualize how the user could go from step to step. So for a login page, I want to see here's it in the error state for each of the fields. Here's a loading page and here's a logged in page or a logged out page or a forgot password page. And I want to see how all of those connect together because that is so much more important than just providing a visual way to code your React components, which again, misses the point entirely. So, so yeah, um, there's, there are a few tools that sort of head in that direction. Uh, Adobe XD, you mentioned this one, InVision App, um, Framer to an extent, even though it's more of a prototyping tool. In fact, all these, all these tools are sort of prototyping tools. One of my favorites that's come up in the last couple years is called Sketch.systems, which is actually based entirely on state charts as well. And it's really cool. It's made by Kevin Lina, who made Subform, which is a another design tool. I'm not sure if it's still a uh a an available commercial product, but uh yeah, what you do is instead of describing how everything looks, you describe how everything flows together in certain states, and then you could prototype it using jQuery or just HTML, CSS, whatever, and just create these these visuals that let designers, other developers play around and get a feel how the app is supposed to behave. So not just how the app's supposed to look, but how it's supposed to behave. And I think that's a key thing that design tools are missing.
[00:39:17] Michael Chan: Fascinating. I I definitely agree with you. It feels weird to me that we see, as you said, the space is getting really crowded. It seems weird that everyone wants to make these React-specific design tools. And kind of back to the the point that we've been coming back to a lot, which is like when you use state charts, it allows you to kind of create these flows and these interactions in a non-framework-specific way. And it it it really feels like there is so much space to like explore in that design handoff to like these are the interactions and like this is exactly how it's gonna be. Now like map it to like you know whatever framework we're using today.
[00:39:56] David Khourshid: Right. And I'm, I'm actually working on something you alluded to earlier. I am working on a visual editor. I have the visualizer, which I do need to update with the latest XState version, but I'm working on XState 5.0 and also a visual editor that allows you to visually edit and simulate state charts. And you could really tie that into a design system as well. So another tool that I want to mention tools uh too is Storybook. I'm sure you've heard of Storybook. A lot of React developers have heard of Storybook and they're trying to reach out to other frameworks as well. Storybook is great for displaying components with different props, but not at different states. And that's another, you know, thing that's missing. So for example, if something is loading, the only way for you to see that it's loading is for you to do something with it, like click it or something, instead of saying, hey, here's what it looks like when it's in this loading states unless you add some sort of loading prop or something like that. So I envision creating a tool with um XState and statecharts in mind that allow you to visualize a component or even an entire application in all of these possible states.
[00:41:06] Michael Chan: Awesome. Now for people who- I want to come back to kind of like how people can get into using these. I know a lot of times it can feel overwhelming, like, well, I gotta get some approval to kind of install this like new library, or kind of, you know, I need to get everyone on board to, you know, with this new way of thinking, what are some easy wins that people can kind of maybe like see a smell for? Like, hey, this is this is a sign that maybe we could be using a state chart. And then kind of try it out in a really like small, isolated type of way.
[00:41:40] David Khourshid: Well, uh, I I really like using state charts as local component state if you're using it in a in a React component. For example, I'll model- if I need to drag and drop a component, uh then I will model that drag-drop behavior as a state chart. But actually, the first thing I would do instead of diving right into XState is I would useReducer. So stay away from useState because that's going to get complicated with the more, you know, the more data you have and the more things that are going on. But you start with useReducer, do your typical switch statement thing that you're used to doing with Redux or whatever, and then realize, okay, there's a lot of complex behavior here and I really need to sort out how this thing is supposed to behave. And then at that moment, that's when you would reach for not XState, but pencil and paper. So, yeah. So
[00:42:35] Michael Chan: you got me.
[00:42:35] David Khourshid: Yeah, yeah, yeah, yeah. So, and this is one of my favorite things to do is I'll stop, grab some pencil and paper and be like, okay, Let's draw this out and think about how it's supposed to behave at each state. For example, drag-drop. I'm like, I'm gonna draw two boxes. I could be in an idle state or I could be in a dragging state. And then I draw arrows between the two and I'm like, all right, how does it get between there? So I might have an onPointerDown and I'm like, okay, so onPointerDown means I'm dragging and onPointerMove means I'm still dragging but updating the state a little bit. And then on pointer up means I go back to the idle state and maybe I reset the state or something, something happens. And so visualizing it that way makes it really clear. And I'm using pencil and paper because I could easily change things and move things around. Um, that makes it visually clear what the logic is supposed to be for my component. And then I could translate that directly to XState.
[00:43:33] Michael Chan: Interesting, interesting. So useState And then like you're gonna outgrow that and then you're gonna reach for useReducer because it allows you to handle more complicated tasks or complicated interactions. Then you're like, this isn't enough. Pen and paper, draw what it's supposed to do. And then you have a state chart. Now you just gotta program it.
[00:43:54] David Khourshid: Yeah. And so that programming thing might seem like a leap of faith, like, oh, okay, gosh, now I have to configure XState and everything. I, I try to create with this little boilerplate in mind, and useMachine, that hook that comes with XState React, useMachine has the exact same signature as useReducer. So that's why I say if you're using useReducer, you could just swap it in for useMachine and you're done, because you're gonna get two things: the state and some way to send an event back to the, back to the machine.
[00:44:26] Michael Chan: Awesome. I love that. I love that. useMachine. So, we're coming up on the end of our time. And I want to give you an opportunity to kind of like tell people where to follow you and whatnot. But I I also wanted to just mention, you have an amazing— uh you're half of a YouTube channel called Keyframers, where you do some amazing, you know, live programming of of really complex interactive animations and what whatnot. Tell me a little bit about what people could find uh you doing on Keyframers.
[00:44:57] David Khourshid: Sure. So yeah, we have this weekly livestream where we do it either on Twitch or YouTube called the Keyframers, and that's keyframe.rs. And basically we take a Dribbble animation that's either user submitted or just something really cool that we found, and we try to code it in less than 2 hours completely from scratch. Like it's completely unplanned. Uh, we're using HTML, CSS, and a little bit of JavaScript. Most of the time we're not even using any frameworks. The only time we would reach for a framework such as React or Vue is if we're specifically trying to demonstrate how you would animate with these frameworks. And so in doing so over the years, we've been doing this for over 2 years now, we have discovered a lot of patterns such as the flip technique and also state machines. But funny enough, in 99.9% of episodes, I don't bring XState in. I say, here's how to build a state machine from scratch. Scratch to do this, and here's why you would do it. Here's how it makes it easier. Here's how you could use this in conjunction with data attributes and CSS variables. So yeah, I do it with my, uh, with my friend Shaw, who is @shshaw, S-H-S-H-A-W, on Twitter. And he's also really awesome to follow. He works on CodePen. So.
[00:46:11] Michael Chan: Nice. Well, I've, I've seen a handful of these episodes and it definitely, um, I tend to see stuff on on on Dribbble or or whatnot. I'm like, that's super sweet looking, but like, good luck programming that up. That would be a huge pain in the butt. Um, but but you two actually do it and it really kind of like makes me put my foot in my mouth every time I I watch an episode. It's really awesome to see that go from kind of something that's strictly visual to something that actually works in a browser.
[00:46:39] David Khourshid: Yeah, thanks.
[00:46:40] Michael Chan: Cool. Well, you're gonna be talking at Reactathon this March. It's gonna be awesome. And um, in the meantime, Uh, where can people find out about you, what you're working on, XState, upcoming talks, all, all David Khourshid?
[00:46:56] David Khourshid: So I'm DavidKPiano pretty much everywhere on Twitter, on GitHub, on LinkedIn, if you want, uh, on CodePen as well. And, um, yeah, also if you mention state machines literally anywhere on the web, I'll, I'll probably find you.
[00:47:13] Michael Chan: A wild DavidKPiano appears. I have seen that. I have seen that in real life. It's just, you can almost set a clock by it.
[00:47:22] David Khourshid: Yeah. Well, and that's because every day I do, I do a lot more research than people realize, whether it's searching online for research papers or just looking up how people are using state machines in real life, just because this is a technology I am devoted to because it's, it's not something new. Again, it's something really old. And if I'm wrong on my bets that this is not the best way to do state management, then that means that developers have been wrong for half a century. So I'm really, really betting on this.
[00:47:52] Michael Chan: Well, I do feel like, you know, so much of this resurgence of this idea has really been directly attributed to you and your tenacity for kind of jumping in on those conversations and reminding people that it's an option and creating these really cool visualizers and and um resources for people to learn. So, um I think from all of us, thank you so much for your your diligence in um kind of forcing this conversation. I think it's making uh the the frontend space a lot more uh vibrant and uh just better for the people who are actually using our products.
[00:48:28] David Khourshid: Yeah, thanks. I appreciate it.
[00:48:30] Michael Chan: Cool. Well, thanks so much, David, for uh for being on. Um I Look forward to seeing you uh in March.
[00:48:35] David Khourshid: Yeah, thank you for having me. See you in March.
[00:48:38] Michael Chan: For links and show notes, visit reactpodcast.com/83. If you like this show, there's a fast, free way to demonstrate your support. Leave us a review on iTunes. It's the best way for you to let me know what you think we're doing right and what we can improve. 2 to 3 minutes of your time helps us make the best show we possibly can. As always, links and show notes for all episodes are available at reactpodcast.com. This episode was edited by Mikael Delport. It was produced by Mikael Delport and Sarah Jackson at Speck, a network to help you level up in design and development. Check out spec.fm for other shows that are sure to fast-track your career. I'm your friend, Chantastic. Thanks for listening. We'll be in your ears again next week.
chantastic.fm