You see, what is the pattern that we see a lot in these codebases? And how this is going to work is we're going to go over what the pattern looks like, how do we fix it, and why do we fix it in the very end? You see, first up is systematic modularity. You know, most of the projects you're going to open are going to open to a flat components folder, 40-plus folders that tell you what components are and even what the framework is, but they don't tell you what the project is. And that's the first step for us to get this codebase into something modern and modular, is that our component structure should model what our app is doing and not the components or the framework it is in. Before you move a single line of code, you need a map of what the components in the app are going to look like. That's where you get into feature-driven components. Components, hooks, and types are all co-located based on the features, not based on the framework. And we get into this structure where if you need a specific item for a specific feature, it's found within the features folder. This makes things a lot more workable from when you're handing this project off to someone who's never seen it before or from just everyday, day-to-day maintenance of this project. This is going to be a lot easier and a lot more structured to be able to work in as a developer as well. And obviously, any shared components in the utilization of primitives, which is something we'll talk about a little bit later in the presentation, is also important as well. But all these structures and components are co-located in places that they need to be, not based in just a singular flat component library.
Now, why do we do this? We kind of shared the snippet here of what the exports would look like in our dashboard page if we met this pattern. And why? Conway's Law says your system will mirror your organization structure, usually by accident. Architecture and what this principle is we're implementing here tells us that we should be doing it on purpose. Our feature should tell us exactly what that code does, and we shouldn't have to guess what that component's doing or where that component is based off of our folder structure. This gives us a good baseline and good structure to not only modularize and make our individual components and pages better and our features better, but something that scales to the entire code base as well. Now, the next thing we're going to get into is logic extraction. We talked about the organization of the folders, and now we're going to get into the actual code, what we should be doing within these components as well. So, within our dashboard page, we had business logic baked into the actual component as well. These revenue calculations, fetching logic, validation were all living inside the component. That can be problematic in a couple of different ways. One, we have these components that are doing way too much on the front end, but we also – it's not testable. We can't unit test these individual components, and they're stuck within our component as well. You can't reuse any of these components if other features need to do the exact same thing. And if a component breaks, everything in this folder or in this file is going to break, especially all this logic and the reusable pieces as well. So, this is the pattern we're trying to get away from. Now, how do we do that? We have these specific hooks and break out this business logic into their own system, their own modular pieces of code that we import into our main dashboard. Use order filters owns a filter and source logic. Use orders owns a fetch. Each hook is independently testable with render hook.
Comments