 Okay, so I want to welcome everyone to this month's meeting of the Houston functional programming user group. Today we have joining us yet again is Eric Normand. I think this is your third time presenting with us. So you're you're a regular now. And so we always enjoy when you present and we always have very, very good spirited discussions. So we're looking forward to what you're going to present today. And I think that is it. I will turn it things over to you. Okay, everybody, Eric Normand. Thank you so much, Claude. I'm, I'm Eric, and I've, I've got a talk today that is a continuation of another talk, because I'm working on the contents of my book. And so I find that making the slides helps me develop the content. And last year I was here about this time and I had the discussion was really good because I was getting questions that I wasn't expecting and really told me that I'm presenting it in the wrong way. And so after, after that presentation, I kind of went back to the drawing board and thought a lot about what I was trying to say and how to say it best. And I think I got, I got something really good now. So this isn't going to be like a rehash of the stuff we did last year. This is just a total new reorganization and a new way of presenting the stuff I have. So I want to say thank you and I am very open to discussion. I really want to, I would love to have that experience again, even though I would dread having to reorganize all of this again. All right, but with that preface, I'm going to share my screen. Do we all see that? Yes, awesome. Okay. So this one is titled for more domain modeling lenses. So this is a talk about software design. And software design is subtle. It's something I have to keep reiterating to myself. And I want to make sure that it's it's very clear. So here it is. Here's the slide about that. And if I could summarize what I'm trying to say very succinctly, it's that good information leads to good design decisions, which leads to good design. So design is all about making decisions and you need good information to make those decisions. So I've organized my ideas on domain modeling as it pertains to design in terms of lenses. So each lens gives you like a little people view of your software, your domain. And if you get enough of them, you start to hopefully be able to give it a bigger picture of what's going on. Currently, in the organization, this, this keeps changing, especially the order keeps changing, but I've got nine different lenses. The ones on the left, the data operations composition and time are kind of like basic building blocks. And then the ones on the right are more like orthogonal ways of viewing your software to just get more information that informs how you use the ones on the left. And this didn't this talk is like part two in a series. I gave another talk called better software design with domain modeling at Funk Prague, Sweden. And the folks here on this call were asked to preview it before this talk and I just wanted to make sure that was part of the recording. That one goes over these lenses, these three data operations and volatility. And in this talk, we're going to go over four different ones, composition, scope, platform and runnable specifications. Okay, now a quick, quick preview or review of our domain. So we're doing a coffee shop. There are three sizes super mega galactic. There are three roasts raw burnt and charcoal. And then there's a bunch of different add ins like soy shots and espresso shots that you can add it. And to the right we see the Jason that I'm going to be referring to. Hopefully you've seen the talk. We like go in depth to how we choose all this stuff. But basically there's a key for size a key for roast. And then the add ins are a map of the add in to the number of shots or, you know, pumps of the syrup that go in. All right, so let's get to the composition lens. So when I say composition, I mean, something very simple, it's two or more calls to the operations in your domain working together. I'm sorry, Eric. I want to jump in here. Can can you just for people who might not have seen the talk. Can you just explain the lenses. Oh, sure. Yeah, so so like I was saying, at the beginning software design is really hard. And it's like multi dimensional. And it's like a changing landscape like as you move in one dimension, the dimensions change. Everything's interconnected. And a lot of the design, the software design advice out there is based on rules of thumb or principles. And I've always found them to be lacking. And you can tell they're lacking because they have so many exceptions to them. Like this, you know, this rule applies except don't do it here because that's this thing and then don't do it over here because of that thing. And you're like, is it a rule or not? Is it a principle or not, you know, and my attempt to avoid doing rules is to say, look, we're, we're in this very difficult space problem space. The best we can do is arm the programmer or the designer with a bunch of ways of looking at their software, give them some tools. Give them perspectives on that they can use while they're in the field while they're in their code base making decisions, and, you know, pat them on the back and say like good luck. And so the another way to look at it is they're kind of like figure ground exercises. So if you're, oh, I was reading this book after I gave this talk last year here at Houston functional programmer group. I, I was disappointed with how I did. And so I was reading this book, this like totally unrelated. It's about drawing. And it's called drawing on the right side of the brain. The author has taught a lot of people how to draw. And she's broken it down into, I think it's like five different skills. Because the thing about drawing is when you, when you look at like a plate or a table, you say, oh, that's a circle or that's a rectangle, but you're not seeing a circle. There's like, it's very unlikely that you're looking at it straight on so that it's a perfect circle. It's always foreshortened in some way. And things don't often have like easy edges to draw. They're like very irregular. And so people will like draw an eye like they think an eye should look instead of drawing the lines that they actually see. So a lot of what you have to do when you're learning to draw is learning to not to is to perceive more directly. And so she has an exercise that is trying to help you draw the outline of a shape like of a figure. And it's draw the background. Don't draw the figure, draw the background. And she has like four other exercises like this. And it's trying to trick your brain into perceiving more directly. And I thought that that was perfect for, for what I'm trying to do is try to get people to see more clearly past their biases, past their habits of how they they're they've been taught to to see a problem and convert it into code. So each of these lenses is trying to be one of those things like look at it from this perspective and it gives you some information. You can use that information to make better design decisions. Awesome. Okay. All right. So this lens is called composition. This is a lens about how the operations work together and even like an operation could be working with itself, right? So rarely do we see one operation doing all the work, right? You've got a bunch of methods or functions and they're going you're going to compose a solution with with all of these together. So here's an example of something that it's kind of a scenario of what might happen when a barista is typing in your order. You know, you're speaking your order and she's typing it in. So you set the size to galactic. Well, then you change your mind. You want it mega. And then you set the roast, you add an add in, and then they accidentally press the remove almond button. And so this is an example of composition, right? It's a simple example. It's a thing happening in sequence, but it's a good example of what might happen. So we could test each of our operations in isolation. So we could test the semantics of set size. So we have some example based tasks where we assert that after we set it to galactic, it should be galactic. And after we set it the roast to charcoal, the roast should be charcoal. But frankly, these are just these are boring and they don't capture the interactions between the different the different operations. What we want is to look for those. That's what this lens is all about. How do these operations work together? What are they supposed to be doing? So let's go through a scenario where we're building a test that says that we can add the add ins in any order and we'll get the same coffee at the end. So here's one order. So for to start, we'll just start with these three. You're going to add soy almond and espresso in that order. And then in a different order, we're going to have almond espresso and soy. Same ones. If you added these to a coffee, they're the same coffee. The customer doesn't care how you write them down. It's going to get they're going to get the same coffee. So these should be the same. So we can reduce over those add ins the first list of add ins to make a coffee. And then we can reduce over the second list of add ins to make a second coffee. And then we can assert that they're the same coffee. Right. So this is this is a test that we could run to somewhat test that we can add these things in any order. But it's not generally enough. We want it to be a little bit more useful. So we're going to change it from new coffee, which always returns like a default coffee to any coffee. Calling any coffee will generate a random coffee. And then we're going to make an array of arbitrary length of any add ins. So now it's going to have some random array. And then we're going to use that same array, but then shuffle it in some order. And now, because that's only one, we want to run it 100 times with 100 different random randomizations and assert that they all work. So this will test a whole bunch more. And it is a better guarantee that the property that we're looking for this relationship that it doesn't matter what order the add ins come in is maintained. Right. And we can do this. We can write this code without even having a data representation and without even having implementations for these operations. We can write this down in our code and, you know, it wouldn't run obviously if we don't have the add add in implemented. But no matter what data representation we use, we want this to be true. So it's kind of a constraint on what what data modeling we use. And so we have this property that keeps the barista happy, because now he can say, I can press the buttons in whatever order I want. And I'll always get the right order. Okay, so what we did was we converted this code at the top to this code at the bottom. And if we squint, because JavaScript is not math notation, but if we squint, we can see that we've made some unknowns, right. And in math, you would use like letters like X and Y and Z to represent unknowns. They're called variables. And so this is why these are often called algebraic properties that we're saying that something is true of this operation add add in. No matter what X and Y and Z are. Okay, so this is an algebraic property of our function. This algebraic property happens to have a name. A lot of the mathematical, I mean, a lot of the algebraic properties have mathematical names if they come up enough. It's called commutativity. And it means that the order of function calls doesn't matter. So it's, it's not so clear here, but let's rewrite it in a simpler way. So we got this, this for loop to do 100 times. We're going to make any coffee, and we're just going to make two add ins this time. And we're going to assert that the, if we add in a and then add in B, it's the same as adding in B and then adding in a. Notice I also did a little switcheroo where I made it that it's using the dot method notation. Just same semantics just easier syntax for seeing this order. Otherwise you'd have like nested function calls which are hard to read in JavaScript. Any questions at this point. Yes. So the word commutativity is usually applied to binary operators. But in this case, you're applying it to a set of functions. But you're not saying that function position is commutative. You're saying that this set of functions right to the jitter. Is it really the same word? Yeah, yeah, they use the same word. So it is unfortunate and we're going to get to the binary operation one. Which is basically that the argument, the order of arguments doesn't matter. This one is function calls don't matter. The functions commute and mathematically you would write it like this. That if I apply G to f of a, it's the same as applying F to G of a. But good catch because I'm going to get to that. That's community where the order of arguments doesn't matter. And if you, if you, if you wanted to, you could write it out using, what's that? I mean using currying basically and make the functions, you know, you auto curry the functions and then you can change the order. Right. If you had, if you had unary functions, the binary function becomes a unary function. There you go. So community, commutativity where order of arguments doesn't matter. Let's look at what this is. Right. So let's say we have some, we have two of those add in sets or add in maps. And we want an operation that, that combines them together like this. And we want to say, wait, it shouldn't matter if I put the espresso one first or the soy one first, right? That's my property that I want to maintain. So I could write it like this. So I create an array of add ins, ray of add ins. Okay. It shouldn't be a ray of should be a map of sorry about that. And then we combine them. Doesn't matter if I combine them a first or B first. We should get the same add ins. And this type of commutativity is, is expressed like this. It's so much cleaner in math. Once you have to start writing out like long function names and variable names, it gets messy. Item potents. This is an important one that we use a lot. Basically means doing a thing twice is the same as doing it once. So let's go through a property for that. So we've got a coffee and a size. And we're going to assert that if I set the size on the coffee, it's the same as setting the size again on this on the coffee, right? And it's kind of unclear, like I said, the nested function calls are not easy to read in JavaScript. But it's basically this, that if I do f of a, it's equal to f of f of a. And you'll notice if you, if this is true, then it's also true of f of f of f of a, right? Which is why similarly we could do that with commutativity where we could only work on the binary case where you do a first and then B first instead of having an arbitrarily long array of add ins. Kind of works out that if two can be out of order than any number can be out of order. Much simpler. Right. And so then we could also write it like this just to make it clearer. Okay. One more property. No, two more properties. Associative order of operations doesn't matter. This one, this one always screws me up because it's very similar. It's taught like similarly to commutativity because a lot of operations that we use are both commutative and associative, but it's order of operations. Basically think the parentheses. Right. What we want to say is that if I have two sets of add ins or sorry, three sets of add ins and I want to combine them. I can combine them like this where I combine B onto A and then C onto the result of that. Or I can do the B dot combine C first and then combine that onto A. But notice that I've maintained the order ABC. That's important. But what I'm doing is combining the two on the right here. It's clearer for you to see in the math. I'm saying that if I combine the two on the left first, it's the same as combining the two on the right first. The parentheses are indicating the order of operations. And this is why when you're writing out like a big sum like A plus B plus C plus D, you don't have to put parentheses because it doesn't matter. Right. They're the same. However you put parentheses, it's going to give you the same. All right. One more property. This one is the inverse. It's basically like an undo of a particular operation. So one operation can be the undo of another. So we can write out this property. So we got a coffee and an add in. And we're asserting that if I have this coffee, this coffee is going to be the same as adding the add into the coffee and then removing. And that looks like this in math. Okay. But this one. I can already tell that there's going to be a problem because my manager has said there's a maximum number of add ins. This affects our property. He says we can't sell a coffee with more than five add ins. So you can't have like five espresso shots in your coffee because then it's like there's no coffee in there anymore. The cup is too small. So if we put, we could do this. We could take this add add in function and just have this guard at the beginning. It's like if it's more than five, don't add it. Right. So just return the coffee unchanged. Okay. I'm just repeating the property up there so we can have it for reference. But I can imagine a scenario where this property is going to break. So I have a new coffee. So there's nothing in it. It's just no add ins. I'm going to add the soy. I'm going to add five soys to it. Oops. Forgot to close my quotes. Okay. And so then I'm going to add a sixth soy, which should be a no op because I'm guarding against if it's more than five. And then I'm going to remove the soy. When I assert that they're equal, it's going to fail. Right. Because I didn't add it, but I did remove. So the coffee two is going to actually only have four soys. Even though I added a, I wanted to add a sixth. So what we would say in this case is that this is a partial property. It works until you get the five. Right. I think this is kind of a, I looked around for a term for this. I'm going to make this up, but there's the idea of a total function, which is a function that has a valid return value for every valid, you know, set of arguments. Basically doesn't throw an exception. And then the partial function does throw an exception. In this case, I'm saying it's the property that's total or partial. So we want to prefer total properties because they are much cleaner, much easier to handle, but we could write this as a partial property. So we take our property and we just put an if statement around it. And we say, look, if this coffee that we just generated randomly has, it only works if this has less than five. Okay. But we don't want to do that. That is not ideal. Ideally, we'd have a total property. And so to figure out what to do with that, one thing we could do is go to the scope lens. So I'm moving into the next one. Okay, so here we have our, we have some of the operations that are related to add ins. So we have add add in and remove add in. This is the notation I'm using for the function signatures, because TypeScript signatures get kind of long. But you can tell what type something is just by the name of the argument. And then this is the return value at the end. So the first two return a coffee, normalize takes a coffee and turns it into an equivalent coffee that can be compared for something like equality. So that you don't have different, in case you have different representations that mean the same thing. And then is valid is telling you whether you've kind of messed up your data model in some way. And so normally we would put a coffee with more than five ingredients or add ins as an invalid coffee or return false. But it messes up our property because it would be really nice to be able to go above five so that we could remove it later. You know, imagine, I know the property, it's not just some abstract, like, you know, ivory tower idea that we're going to keep a total property. It actually helps the barista. So if the barista accidentally presses, you know, plus soy and then wants to correct it, they can just hit minus soy and not worry about, well, did it actually add it or not? You know, so it's it's much more ergonomic or ergonomic as well. And you can see why like having this condition that they have to maintain in their mind, like how many, how many soys are in there. Maybe they don't even maintain in their mind, but they have to stop and look and see how many add ins there are already. So the idea is, let's draw a line. Make a new layer. So this layer was just going to say this is for all coffee shops. This is the domain of coffee shops. And then above that, we're going to put code that just relates to our coffee shop. We can justify this. We can say, hey, a different coffee shop might have a maximum of three add ins or no maximum. I can imagine not having a maximum. Maybe the maximum is 10. You can imagine it being any number or not existing at all. So it's not it's justifiable to say that we don't have to do that in the coffee shop domain. We could just make that our coffee shop where we put our business rules so we can define another. Oops, sorry. Another function called is valid. Sorry for the name collision, but it's in a different namespace. And this is where we put the businesses, our businesses, particular business rules. Okay, so we have this add in function. Let's comment out that guard and just always add the add in. And then up in this is valid function, we can, you know, add this check that it's less than or equal to five. And that's a business rule. Not a domain name rule to business. So what we're doing, what I'm trying to show is that when we're thinking about these decisions of like, do I, you know, how do I code this? Where do I put this? Often it's helpful to defer a decision to a higher layer because it helps you eliminate a corner case or make a partial property into a total property. But you should only do it if you can justify it semantically. Like, you can't just have a million layers and then like, well, what's that layer about? Well, this is this one little subtle thing. No, don't do that. Like, you have to have some kind of semantic justification for it. Now, similarly, you can go down a layer. And this is useful if you have some really complicated scenarios, some complicated domain logic that is hard to organize at the current layer. Sometimes you have to go down a layer. So this one is going to be hard to explain. So bear with me. But the thing I thought of for a coffee shop is they have sales and promotions. So there's all sorts of different ways promotions can work. You can have coupons. You can have like these weekly promotions that operate at the whole store level. You can have loyalty perks like those little cards that you punch. And then the discounts can work in different ways. Some of them are percentage. Some of them are fixed. So the percentage would be like 10% off. Fixed would be, you know, $3 off. Then you can have stuff like buy one, get one free. And then sometimes you get 10% off of a particular kind of coffee like they want to promote their pumpkin spice latte. So you only get 10% off of that one, not the rest of your order. And then the requirements can get really complicated. So like you have to buy five and then you get the sixth one free. And how does that combine with like a loyalty perk? Sometimes it applies to any latte. But then sometimes you have a thing where, no, it has to be a coffee with almond and another coffee in the same order with soy. And then you get the discount. So it gets really complicated. And if you try to just like say, well, I want to make a thing that's in like kind of pick and choose. It's not going to work so well, especially when like you combine them like what if someone goes in with a weekly promotion. When there's a weekly promotion going on and they have a coupon. And, you know, it just kind of gets out of hand. So the recommendation is to go down a layer and I'm not going to do the complete thing because it would take too long. But I want to show what that might look like. So we can make a type that is this union of these types. So it has no discount percent discount. So no discount is like, you know, you don't get any money off percent discount, 10% off fixed discount, $3 off. Both discounts is going to have two separate branches and both apply. And then highest discount is only one of them is going to apply, but we're going to give the customer the highest of the two. That's how the business likes to operate. Okay. And then we're going to have some types like these basic types like a discount. What does a discount mean? Because discounts can be so complicated, we're going to just say it's a function. It's going to take an order and it's going to return a discount amount. Okay. And so this discount can check, you know, does it have the pumpkin spice latte and then it's going to give a discount amount, one of these things up top. And then we can say, well, we can have a higher order discount function called guard discount that is going to return a, it's going to take a discount and modify it to return a new discount that guards it in some way. So like you only get this discount if you've ordered a pumpkin spice latte, right? Otherwise it's going to always return no discount. And then you can combine two. So you can say, well, I'm going to, if they have a coupon and a loyalty card, how do those two work together? And so you can combine the two and it will return, you know, the highest or a both or something like that. Okay. Sorry. I wanted to say one more thing. So this, I'm sorry. This has happened to me before. And so I want to give like one little one more illustration. I worked at a company that did voter registration and the difficult complex scenario we were dealing with was that every state has different registration laws. And we struggled because there were, there were experts on the team who knew all the laws. They like had memorized them all and they could talk about them. But in trying to organize them, whenever we would organize it was like something would slip out and then we couldn't do it. And we could find a way to like kind of have 70 to 80% of the cases work out. But then there are just so many exceptions. Like it just seemed like the best we could do is just have a giant if statement. There's like, if you're in Alaska, these are your rules. If you're in California, these are your rules. But then I kind of sat on it for a while. And I realized, wait, the basic question we are trying to answer is can this person vote in this election? Yes or no. That's a function of person and election to a Boolean. And once I had seen that, that like it is a simple problem when you look at it at the function signature level. And then you can start to write rules to combine them like we can have a rule that's like, you know, of majority age right 18 or over. And it always returns false no matter what the election is if you're not 18. And if you are 18 it returns true doesn't look at the election because the election doesn't matter. So you can have all these rules that are just small, small nuggets of rules instead of trying to organize it by kind of like a hierarchy of like there's this kind of state because some states are like you don't need to register some. You need to bring an ID. Some you need to register two weeks before you know there's all sorts of like different categories but instead of looking at it as categories we went down a layer and looked at it as just a bunch of simple functions that we can then combine using you know a function that combines to that says like an and of these two rules or an or of these two rules. And just starting from that we were able to organize it in into something that we could manage. And then of course you still had you had the Alaska rule which was like this big composite rule. But we were reusing a bunch of the sub rules in the other states as well. All right, before I move on to platform, are there any questions. All right. So just jump in if you have a question, I'll continue. All right, so sometimes your platform is complex enough that you need to model it as a sub domain. This is an example that I've seen a lot. I still see it Ajax loading. So if you load a page and something like if the page is going to then do an Ajax to fetch the data that it needs from the server sometimes if you read it really before it loads. If you read the message, it'll say something like, Oh, there's no messages here or there's nothing to show. But there is it just hasn't loaded it yet. And it's, I mean, it's it's a pretty clear thing like why it's broken. This is actually a picture from Slack that it'll show your direct messages like you don't have any, even though you do, they just haven't loaded yet. And really what they should show is a loading spinner, right, like just just wait. Oh, and this is from Chris Jenkins blog, I couldn't find a picture myself like I couldn't find the bug and take a picture of it like while I was looking so luckily he had taken a picture of it. Okay, so we're going to try to this is like a piece of complexity of your platform. You're on the web, you got to do Ajax. Like instead of trying to get it together with like if statements and stuff, let's just model the thing. Alright, so this is what is happening in this slack client. You like initialize the messages to the empty array. And then you do a get and then when the get succeeds, you just take the data, you grab the messages from it and you reassign this variable to messages. And somewhere else there's a view. I'm using like a kind of react notation. If there's nothing in the messages, then you just say no messages. And otherwise you make a list and you map over the messages in there and make list elements. And you're done. Right. And this is the problem. It can't distinguish between I fetched the messages and it was empty versus I started empty and it's still empty. And I'm still waiting for the messages to come back from the server. So Ajax is more complex. There are more than two cases. What about the when it's loading and then what about errors? We haven't handled errors either. Let's model this. It's complicated. It's worth thinking about. All right. So this is a swim lane diagram. We got the browser and the server. The browser makes a request. And then the browser gives us a response. Very simple. But I needed to draw it out because we can see that from the browser's perspective, we don't know what's going on on the server. We're the browser. But from the browser's perspective, there are actually three different states times that we need to account for. There's this initialized time before we've sent the request. There's the loading while the request is in flight and there's success. Right. So we can actually model this out with a bit of JSON. We have a status initialized status loading status success, which also comes with the value, whatever data we got from the server is stuck in there too. And then of course it's more complicated. Like I said, there's errors. So this actually the timeline splits here. Sometimes you get an error. So we need a fourth state where you have an error and it's instead of a value, it's going to have some error message. All right. Now we can make operations that operate on that data type that we just made. So new Ajax value is just going to start initialized. Right. And notice we're not giving it an empty array. It's nothing. It has no value yet. Doesn't make sense to have a value. Okay. Then when we request, we're going to pass in that Ajax value, but it doesn't use it yet. It's just we're going to return loading. All right. When we succeed, we're going to return this, obviously, and then the error is going to look like that. Pretty simple, but it doesn't really capture everything yet. Because after we do an error, get an error, we actually want to retry. So we want to go back up and load again. So what we need to do, oh, well, right. Sorry. So it's also, if we get a success, we sometimes need to get a new version of it. You know, after five minutes, we want to refresh the data. We want to load it again. So what we need to do is capture these optional error and values in the loading and the error state. So if we're loading, we might have the old value still. If we were in this, this branch with this loop, you can see my mouse, right? Yeah. Okay. So if we're in this left-hand side, we got, we went through success. So we have a value. So when we're loading again, we want to maintain that value. We might put a spinner, but we're still going to show the old value because it might still be useful to the user. Likewise, if we're reloading after an error, we might show the error that happened. And then, of course, after we do the success and we load, we come, we might come back down the error branch, right? So that could happen, too. And so we want to put in the status error that we had a value from before, from that original time through the loop. We did succeed. So we want to show that, too. It still might be useful. And so I feel like this is a much more complete view of what might happen because you can always reload. And so then that changes how we are going to write our functions. So initialize doesn't change. But this time we are going to return a copy of Ajax value, but with the status loading. So that will keep, if there's an error in there or a value, it'll keep it in there. Now succeed is going to always wipe out any error that was in there. We don't need to keep it anymore. So it's just a literal. And then the error is going to keep a copy of the current Ajax value, but this time set the status to error and put the message. So that'll keep any value that might be in there. Okay, so I put that in there because a lot of times we, I've noticed people don't want to, they might model their domain really well. But they don't model that there's all this complexity in the platforms that they choose. They're just throwing if statements at the problem and not capturing the true complexity of it in something simple and a simple thing like this that you can kind of put in a namespace and reuse. Notice value. We didn't specify a type for it. It could be anything. It's just, it's just there. And you can, it can compose with your domain types easily. We can model a lot of the stuff that our platform does file IO. So the files on your, that you need in your application, they are going to have some failure modes. You can build in a new type that would handle those error modes in the way that you want them to be handled instead of the way the platform handles them. Likewise for databases, query could fail in some way. Threads and concurrency. There's always complexity there. And then exceptions like model, model your errors. Like you can, you can do a lot by like coming up with some kind of system-wide way that we're going to handle errors. Instead of like whatever this library we're using happens to use. All right. Any questions on that before I move on to the final, final lens? Yeah. So I think. I think I didn't quite catch what is the problem that we're solving with this lens. Right. So the problem is people not stopping to solve the complex problems of their platform. To say, hey, look, we're on the web. Like that's not going to change. Ajax has its problems. Instead of handling it bespoke every time we do an Ajax request. Let's just sit down and think about it. Let's do some design. Right. It's like we handle it this way on a system I worked on recently. We were doing a lot of distributed system stuff and a lot of times we had to make operations item potent that weren't item potent. So like we had to do the get and see, you know, and so we're like, let's just model this. How do we want to do this? How is this going to happen? Because we don't want every call to this to have to do that. We want to wrap it in something that, that like makes it happen every time. Right. Yeah. Okay. So that's helpful. And so. I don't think in what I'm like in your podcast or in your newsletter, I don't think I've read about this lens before. This seems new to me. So, or I missed, I missed it. I did change the name. I used to call it, I think I used to call it the stratified design lens. And the idea. Infrastructure architecture. Architecture. Okay. Yeah. That's confusing it with the one from before. Yeah. Okay. Okay. So, yeah, so I have, I have heard about it before then, but. So, so one thing that I guess I'm trying to think about two things here. One is that. We are dealing with platforms all the time. Now. And is this a way of. Just trying to think about. The limitations, the problems of your platform. Because that seems. That seems a little more negative. Or more snarky. It's more like the contours, the contours of your platform. Okay. Right. So Ajax. Is there's always going to be the possibility that it fails. Right. And. You shouldn't. Your approach to it as your software matures, like I understand at first, you're just kind of hacking it together. Right. But as your software matures, you're going to want to say, how do we handle Ajax? Right. How do we handle when we, when we. Want to refresh a piece of data that we already have, but we want a more recent version. Like these are the kinds of things that. Are just distributed system problems. Like you need to call back to the server. And. They have. Like structure to them. So let's capture that structure. In a space, in a place. And, and treat it uniformly. I've seen too many code bases where it's like, you know, call back hell or, you know, async await hell. Every time, every time there's an Ajax request, they're just like rewriting the same code over and over. So. Yeah. And obviously it still happens because you can still find these bugs out there. These Ajax bugs. All right. Thank you. Thank you. Thank you. I'm not missing any questions in the chat. Am I? No. Okay. All right. Runnable specifications. This is the final lens that I'll talk about tonight. Just like a little review of the. Of what. Of what this process is. So we have this domain. And we need to write code. So. We abstract away the ideas in, in the domain into this model that we've got in our head. And then we encode that model into code. Now, once we've got it into code, we can run it. We can play with it. We can just read it and see sometimes even that's enough. And we can evaluate, like, is this really, does this encoding really say what is in my head? And then sometimes it does, but you're like, oh, but it doesn't work the way I thought it would. So you have to change your model. You have to go back to the domain and say, why did that break? Because the code is right. It's my model in my head. That's wrong. So this is the process we're going through. And basically what runnable specifications is about. Is to tighten this right hand side loop. So that we can get more information about. About our model by running it. By actually putting it into code. And running it. It's. Instead of, you know, one thing I want to avoid is like the big upfront design. I don't believe in it because you're never right. You could spend a year writing out the specification. And then after you convert it into code. You find out, oh, you forgot about this case and that case. Like you want to get it into code. As quickly as possible. So that you can find that sooner. So just to, to pick on someone. UML they also do modeling. That's what the M stands for. So you got this. You're here on the left hand side. That's now. And you're trying to get code into production. And what the UML approach. Basically says it's like, well, go off. In a different direction. Totally orthogonal direction and build a model out of, you know, pictures. And then you take that and you put it into production. And if it's wrong. You go all the way back here. And redo the model. So it's kind of a, I'm trying to show it's like a big triangle that you're making here. Now, you know, what if the production is wrong? Yeah. Okay. So runable specifications is a little different. Same idea you're trying to go from now to production. In our case, you're writing the model, but it's code. It's mostly. You're encoding this thing. You know, it's function signatures that you could easily just turn into code. It's data models that run in memory. It's not production code. Right. It is an encoding. But it is on the way. And then boom, then you go to production. But if you find out, you can, there's a wave now that you can find out that your model is wrong much sooner. Right. So you can run your model and find out. And then come back. So like the feedback loop there is greater. And even though you're, you're working on the model and not production code, it runs and it will be useful. It's kind of on the way to production code. So you get a faster feedback. Okay. And then, of course, no design. You just go straight to production and you have to go back here. All right. I don't know if that will last one lands very much. No design seems like the line should be like that. Okay. All right. So basically by runnable specifications, I mean, you're doing the design in your programming language. That's what I mean. So you, but you're making it's a model. It's not production code. It's more like a prototype. So you're doing an in memory, you know, you're doing data in memory, you're going to run tests on this model to make sure like the kinds of property tests that we wrote before where you can test like that, you know, if I add and then remove and add in, it's the same one, that kind of thing. Then you've got this code that you can basically stepwise refactor it into a production implementation. And then you can also, because you have the model still around, you can use it as an oracle to test your implementation to make sure that it's functioning the same way. Now we'll go through each of those. So make in memory models. So just skip any platform specific stuff. Like even if it's in the browser, don't make any Ajax requests. Just, just, you know, don't do any async, no database, nothing, just remove all the complexity. You want to focus on the domain. No platform complexity in memory data structures and functions instead of, you know, remote procedure calls, just use like whatever the language gives you, JSON or types or objects, whatever you got, and then figure out how to tell if they're working. So like one thing you can do if it is something that's visualizable is like, show it. Like if you're working on some kind of, I mean, the idea I came up with is like, why don't we make a graphical system? Right. Like if we're, if we're doing some kind of geo geometrical graphics, like let's just show it. And even if we're supposed to output the SVG eventually, that's a platform tail. Let's just plonk it onto a canvas in the browser. So visualize it and you can see, oh, it's working or it's not working. And then tests. Of course you got your manual tests, your automated tests. So let's talk about the tests. So you're really trying to ensure that you have the behavior and the properties that you want. So manual testing, of course you can do that. You can write like little scenarios, you know, the barista. Was that a question? Okay. So yeah, you got, you know, code up a little scenario. Okay. The barista adds this and this and this and then removes this. And, you know, you can, you can see if it's working and use your intuition about if it's doing the right thing. Automated testing, obviously you'd want to do that. And then the cool thing is that it's, because it's all in memory, it's like these things can run really fast. So as you're changing your model and updating it, you can just keep running those tests and get lots of really fast feedback. And then because you're in memory and doing like removing all the platform complexity, there's very little investment to throw away or to fix if your problem is found. Okay. Then we're going to refactor the model into the implementation. So you can stepwise convert stuff. So you can take like a JSON and just convert it into database schemas, however you need to do that. You can take those function calls and turn them into Ajax calls and then of course you're, you're adding in the complexity of your platform now. So you'd have to turn it into a call that uses that Ajax, that Ajax value that we made, et cetera. One thing that's, another thing that I like to do is turn my functions into an interpreter. So instead of having, you know, functions that are opaque, you would turn it into like a piece of data that can then be interpreted. And so then you, you can print it out and see it and the interpreter will run the same thing that the function would have done. And then I do want to say this, like ideally in an, in a perfect world, you would not need to refactor it into an implementation. You could just run it, right? But we're not there. Like JavaScript, if you have some JSON object, it's not going to persist. You need a database, you know, you're, you're, you are going to have to make Ajax calls because the data is on the server. It would be really cool to somehow have it automatically do that, but we're not there. So you have to manually do it. And then this is the coolest thing that I think is a pos, like a big possibility, which is you have a system, you've been working on it with really fast feedback. You've tested it, it does everything you want. Now you have an oracle to test your implementation with. So you can test that you're refactoring in the date to the, to the, to the production code is actually functioning the same way. So you can run the two model, you can run the model and the final implementation with the same input. You compare the output, they should be the same. I mean, to me that this is like, it's paying for itself now, right? That little, you know, the part that where it wasn't quite lined up with that white line, the red arrow went off the line. Well, it pays for itself because you have a thing to test. You can just throw millions of tests at it and they should all come out the same. All right. My final slide, if you're interested in what I'm talking about here, I do talk about these ideas on my newsletter. I also talk about them in my podcast, but I'm not focusing on that so much right now. So get on my newsletter, ericnormand.substack.com. It's totally free. Cool. Thank you very much. Great. Thank you so much. I really appreciate this. This was really interesting. A ton of fun. There was some comments in the chat and I think we'll just open things up to discussion. What we traditionally do is we do sort of the first part of the discussion, record it, and then we'll turn off the recording and continue asking questions and asking any questions that we feel that we don't want to be public. So I will open up the Q&A to people. So please just unmute yourselves and ask your questions, please. So I do have a question. So eric, have you used any property-based testing frameworks, like Quick Check or FS Check? Yeah. Okay, cool. Yeah, because what you were saying early on was pretty much property-based testing and the various stock invariance. I like the angle you approached it from. It was a really nice intuitive angle that you came at it from and I really dug that. So cool. Thank you. I have used property-based testing. I think it's amazing, changed my life, didn't have to write so many example-based tests. And I do want to put it in my book. I think that I do have a take on property-based testing because most of the tutorials out there don't really help you come up with the properties. They're just like kind of theoretically like isn't this great? They can generate hundreds of thousands of tests automatically. But I feel like what people need is, you know, the pre-existing algebraic properties and given to them is like, these are templates. You can take this and kind of modify it, you know, you can generate like partial properties from them. Sometimes it's commutative, sometimes it's not. But just start there. Like maybe you don't want equality. You want some other comparison function. You know, just whatever changes you need to make, just start with these as a template because people do seem to have trouble coming up with properties. And my idea is that, I mean, it's not my idea. It's common in the functional world. But what people need to do is think about the properties first. Like a lot of people think about their methods on their object and they don't think about how those methods work together. So that's why I was focusing on the inverse. And I was thinking about, you know, like some of these are going to be undoing and you probably know, you probably know that already. So build that in. So can I follow up on that? Actually, first, can you turn off your slides? We can just have a discussion. And I'll encourage other people to like turn on their cameras because it's always, oh, that would help. Yeah. The pressing to just look at black boxes. I say this as a professor. Thank you. Can you say more about sort of figuring out the properties? Because I was thinking about that with my own work. And. I think that that is, it's just, it's really hard sometimes. And it also assumes that you understand your domain. And. Right. Often we're figuring that out along the way. I mean, so. Some of the properties are things that you wouldn't even think of the test. So like one thing might be, hey, no matter what buttons I press on this UI. The, it has to have a price. Like it has, I have to whatever. Coffee is I add to this order. Like I should be able to calculate the price. That is a problem. Right. Like there shouldn't be some combination of buttons that gets me into like a note non-priced. Non-priced order. Right. So yes, you do have to know your domain. I can't help you with that. But the things that. The things that are properties, I think people just aren't used to thinking of them. Like upfront, right? They're like, oh no, wait, how did we get here where we have to do that? You can't run calculate price on. Like what's the problem? So it's a bug at that point. And you should be thinking about it ahead of time. Like every coffee should have a price. There's, and that's, and yeah, that's, that's a really good point. I did post a link to the F sharp for fun and profits. They had a list of some good properties. And you're right. Trying to find the properties is challenging. But it's, and it's also interesting that these stock properties, you could often, you know, use a series of stock properties and get very close to that solution. Like, oh yeah, well, you know, you know, I didn't put it in plus, you know, plus commutativity plus this. And I'm like 90% of the way to what my function should be, which is really cool when you do that. There was something else. I wasn't sure if you used any of those, like any of the model checkers, like kind of like a lot of similar line, like things like alloy. Okay. They're pretty interesting because like, like if you, like, especially like with alloy, like if you put it in, like, you know, you're sort of like your domain model as types, right? And you can put in like, you know, keep properties that have to hold or not hold. And then you have it look for counter examples. And I think it actually uses a solver to where it actually tries to find things that disprove like the states you're telling it, your coach should never be in. So instead of like the random based approach or property based testing, you know, I think I actually try to do it directly. Yeah. And it's really cool for finding those kinds of states you were bringing up like, you know, talking to each other. At the place I used to work, they use some people use TLA plus. And that does an exhaustive search. It basically is like every possible combination of inputs, it will test. And it is much more exhaustive than like randomly choosing a hundred. I've never done it. It seems like, okay, one problem is you're now in another language, right? You're writing TLA plus for your C program or whatever. I would much prefer writing the properties in my, you know, C and using the actual data structures, the actual code that's going to go into either my model or into production. But I understand that some things are like, you know, so important, like the brake system on a car that you would actually want to have a total exhaustive check. That said, like you can always just 10x the number of tests you're running. Like often it finds the problem, right? Yeah. I don't know. Yeah. The next translation step is definitely going to be an issue, right? It would be ideal if it could do it on the code you have. And that is kind of a bit of a weak point with some of them. Well, that's the kind of stuff that TypeScript does, right? Like the TypeScript checker is actually running a version of your code that can exhaustively check stuff. Like so when you do a plus, it's actually using a SAT solver that represents binary notation for your floating point number and has an adder built in and it's a logic. I mean, that stuff is pretty cool. So you can exhaustively check, yeah, this is always going to return one or something like that. Thank you. Yeah. You're welcome. Would it be rude to ask for a preview on what the time lens is like? I can give a short thing here. The time lens is basically that when your domain model becomes sophisticated enough, it starts to incorporate time into it. So think of double entry bookkeeping. As banking became more sophisticated, it wasn't just enough to keep track of how much money was in each account. You actually wanted to know how did it get that way? What were the transactions in what order that moved the money around? And so you start to want to have a ledger, right? So even for something as simple as a UI, like let's imagine you're developing the UI for someone to order a coffee on their phone, you want to have undo. So you add a coffee and add a bunch of stuff. Oh, I want to undo all that and go back to where I was. Like that becomes a model of time, that undo, redo, because you're moving backwards in time. And so how do you do that? And basically there's two ways to model time. One is you keep the history of all the previous states, right? The other one is you keep a history of operations that happen. So instead of having, instead of a function called, you know, set size, you would actually just transact a piece of data that was like set size to gigantic or galactic. And that data would get put in an array or a list. And you could run through that list and generate the current state from it, right? As opposed to, you know, the other option, which is like, you jump back to the previous state, it's like all saved there. This is saving all the operations that happen. So you just reduce over it. Well, I was wondering, that's sort of related to in the, the earlier talk in Sweden or Sweden. You gave a diagram that sort of looked like it would be the UI for the person taking the order. And it seemed like each of those little icons as you touch them would map directly to a function. And then there are two ways to build the final coffee. One is you're building the coffee or changing the size by just, oh, I tapped wrong. I just tapped the other one and it changes the size. Or you don't do anything until everything's filled out on the screen and then you somehow turn that into one final coffee. That's sort of like two ways of handling time. Sure. So let me see if I get this. You're saying that you would have some other representation for like a coffee that hadn't been like finalized. Yeah, but, but you were, you were talking about undo and, and undo would be that's a different button. It wasn't showing on your original slide. But I guess the, the other one would be submit or finalize. Okay. But, but in general that sounded like evented architectures. Aren't those really popular now? Yeah. It's like event sourcing event sourcing does this a lot. Yeah. Also again, I hope that this stuff isn't really new. I just trying to teach it better. Like I noticed someone was like, Oh yeah, this free monad when I mentioned interpreter. That one popped up. Yep. This free monad. Sure. Much better to teach it without those words. Yes. You can bring it up later like, Hey, just in case you ever hear someone say free monad, this is it. This is what you're talking about. Right. And I used to call the composition lens, the algebra lens, and then people had all sorts of questions that were just distractions. So I did mention algebra, algebraic properties. But I thought coming at it from, from that side was better. I do want people to learn the real words for stuff. But often if you start with the words, they're scared. And that activates like their, their lizard brain and they can't learn anymore. Yeah. I mean, I liked your, I mean, I definitely liked your approach because you sort of avoid these words until you introduced the concepts. And I think, and I think, I think with, I mean, I think one of the big downfalls with like the way functional program is presented to many people is that a lot of these technical terms kind of come from front and center. You know, you'll get, you know, you'll get, you'll get a lot of like very useful concepts, but you kind of lead from, oh yeah, let's talk about input and output, you know? Okay. Well, let's talk about the abominat from category three. And, you know, it's kind of like, hey, I could just go to, you know, language X and say just do, just do a print, right? But at least by introducing and kind of showing an aid for something, you prime them. And then, and then you could sort of introduce that concept because, so I do like, I do like your approach because the way you kind of segue it into the property-based testing, I thought it was really, and I think that's a really good way to present it. And it's something I think a lot of function programming advocates could really take a page from to really make this sort of more accessible to a lot more people or even get their interest, right? Because again, you're competing with so many other, you know, paradigms and technologies that if you lead with, you know, what sounds like jargon, you're like, you know, I think I'm going to go take a look and see what Python's doing over here. That's right. That's right. Yeah. Yeah. Thanks for, thanks for that confirmation. I do think that functional programming has a marketing problem. And also we have a teaching problem. Like, I think I talked about this last year that we tend to teach it backwards, right? So we want to teach, we're like, oh, the most important thing is Monads when it's really just the, like the most recent cool thing that you learned, like it's, it can't be the most important thing. It's not possible because Monads are the super abstract things that are only applicable to certain situations, whereas something like pure functions are necessary for Monads. And also useful in a much broader context than Monads are. And so they must be more important because you need them. And so, and then they're right there. They're just a step away for most people if they already know how to write a function. It's easy to show how to make a pure function. So we teach it backwards. Like we want to like start with the Monads. We need to start with the more concrete stuff first. Can I ask a question? Yeah. I think one of the things in the scope section where you were talking about moving down a level. And so one was when, and I always like, I think your example of the voter registration. Project is super interesting and really good because it's super complicated and it's something that like it's very real. And in a way that like, you know, coffee shop is, I mean, coffee shops are real, but they seem to be more straightforward and like we can all There's an incentive on the business to stay simple enough for the customer to understand, right? That's a good point. Yes. Yes. That's a good point. There is an incentive at the state level to make voter registration as complicated as possible. Sure. Or even easy for their citizens, but it doesn't, it doesn't help the person who's got to map out the whole 50 states. Well, but I live in Texas where we don't want people to vote. And I put that on the recorded part. So, well, to be fair, we don't, we don't want certain people to vote. Anyway, we're in, I'm in Wisconsin. It's the most gerrymandered state there is. So I understand. So, so in this moving down a level, I thought that there was something interesting going on that. I don't know that I have a real question, but I'm hoping you can sort of say a little bit more. One is that, that moving down a level and writing sort of a library of digital functions is very much the functional programming approach. And it's one of the places that I still, even after having done this for so long, get frustrated of like, I go to open like the documentation for a module and it's just dozens and dozens of, you know, tiny, tiny little functions that all kind of do the same thing and are slightly different. So anyway, I just, I saw that parallel. The, the, and I'm wondering if you have anything to say about that. But then the larger sort of question that I had was in the past, you've very strongly emphasized the onion architecture. And this struck me a sort of a different approach where now you're sort of suggesting, whereas the onion architecture is like you start sort of small and then you build up layers of abstraction here. It seemed like you were more emphasizing, oh, you could go up or you could go down as you sort of are teasing out these different levels of abstraction. And so I'm wondering. Yeah, I'm just wondering if you have any thoughts about those. So my first thought is the onion architecture does not really dictate a direction of how you're building it. Right. The, I mean, I am kind of suggesting build your domain model first, right, before it's productionized. And the productionized is now you're dealing with IO, you're dealing with external servers and stuff. So then, yeah, that is kind of like adding in that layer of interaction layer that's, you know, on top of that. The thing about stratified design is that I think the more common thing is to go down is to say, I can't, I can't organize it at this level. And the mistake people make is to keep trying to organize it instead of saying this needs, you know, going back to first principles, which is basically what it is, right? So like you're like the classic example from stratified design literature is the thing from structure and interpretation of computer programs. It's, it's an MC Escher art language that they make in scheme. And you're at a certain level of abstraction, whatever level you're at, and you're like, I can't, I can't seem to get any leverage to draw these images better. Right. The answer is to go down. What, and look at your domain from a new perspective, like what I'm not, it's not just this image is like this image and this every image, like what is going on? Oh, I see there's symmetry going on. There's rotation going on. There's repetition going on. And so then those have to become the building blocks that that are at a lower level that you're going to use at this level. And that's what, that's what we, that's what I was trying to express with my story. Like what is going on? It's a simple, it's a very simple function signature of like, can this person vote in this elections to inputs and a Boolean output? Like this, this has a regular structure to it that structure is only apparent. If you, if you abstract away the details and you're like, what question are we trying to answer? And that's at a lower level. At least that's how I see the layers going on. Similarly, what is an image? Ah, it's like a collection of pixels or whatever, whatever you want it to be. But, but you have to ask those questions that are like a level below. I think that's really interesting. I mean, in part just, just I was struck by. The way you phrase of like, you're abstracting away the details and then working at a lower level. Usually when we think about abstraction, we think about moving up. Right. But, but I think you're right. Yeah. You're right. Okay, go continue. I'm going to, I'll jump in after. I'm not sure how much more I have to say because I think you're right. That's it. That's a sentence in and of itself. No, I'm thinking about my own work as an academic. And you and I have talked about it a bit before. And part of what I'm always doing is trying to. Oh, I've got all these cases of whatever it is. It's companies or it's people or it's hospitals or whatever. And I've got all the details and I'm looking for the patterns. And. And trying to figure out which details are essential and which are just. It is a crack incident. Yeah. And trying to get at something that is more fundamental so that so that is. It is going down a level, but that is not how we talk about it. Right. Okay. And so this is. This is what I wanted to jump in on, but I stumbled on stopping myself. The word abstraction is really overused. In rocking simplicity, I decided not to use it. And I talked about general and specific. And general goes down the layers. Right. So general means anybody more people can use it. Right. Because you use in more domains, using more applications. So like you go all the way down the stack and you're at the computer. This is a universal during machine. Right. Like. One more specific thing up is JavaScript. Right. JavaScript is. More general than your application. And so you guys keep going up all the way until you've got your main function, which only does one thing. It runs your software. Right. So that's the most specific thing. The thing is each of those is at an abstraction layer. You're not thinking about bytes when you're working at them, you know, main, you're not thinking about bits and NAND gates. But when you're thinking about NAND gates, you're not thinking about like, what is the particular like coffee shop that's running on this NAND gate right now? Like you're not thinking about that. Each one is an abstraction layer. It's a different. A different layer. Right. And so moving up or down. You are. Choosing a different abstraction. What details am I ignoring? Right now. So, yeah, that's my, my answer. But yeah, you're right. People think of like, oh, I'm going to make a class. That's an abstraction on top of. The four things that are fields in that. Like, no, that's. Not an abstraction. I'm going to ask if other people have questions that they want before I stop the recording. Okay. So I want to thank you, Eric, so much for the discussion. It was great as always. And we've had a good discussion so far. I'm going to. You know, what I wanted to ask you about Eric is, is, you had talked about in, in the previous, about the book. So apparently it's, you're publishing it for free on online. Is that the plan? I, I, I, that is the plan. Okay. So I'm, I'm working on getting it into like an HTML page that I could post online. And I'll have a print copy that you can buy too. Okay. Okay. Excellent. Excellent. All right. So I'm going to start, stop the recording now and then we can continue the discussion for a little bit longer.