Now, I know, this is a far-fetched, far-fetched, I forget the word. But anyway, they have actually found these structures in large code projects as well. So, this is from a 2008 study by McCormack, Rasnack, and Baldwin who they study architectural complexity and how real people organize software and what leads to better outcomes in the software design, blah, blah, blah. But this is a network, it's called DSM, which stands for Something-something diagram, but basically the way you read this, each axis represents files and the dot between, so each axis of the matrix, each side of the matrix is a file, each dot inside the matrix represents a dependency between those two files. The reason you get that diagonal line down the middle is that every file depends on itself or they also call this visibility matrix, where the idea is which files can see into other files or into other functions.
Now this is when Mozilla some time before this study did a re-factor and you can see that before the re-factor, the code was very interdependent, there was a lot of architectural complexity and then when the engineers went in and they cleaned up the code, you see a lot fewer dependencies because they were focusing on the architectural complexity, not on the actual bad code inside each module because that turns out to be more important. And you can also see this in smaller example, in smaller codes or smaller examples. This is actually a dependency graph I pulled from our code base just last week, where we built a new feature and the way we like to build stuff is to just throw things at the wall and then later Swiss comes in and cleans it up and makes everything nice and organized. And on the top, you can see, very confusing code, there's a circle of dependency in the middle that's marked by the red circles and like everything is kind of jumbled together. And then, and you can also see that we have components folders and hooks folders and like a very child component is going three levels up to a parent component to get to a parent file to get a hook, et cetera. And then after, I just organize things by business domain. You get nicely named folders that tell you, hey, you know, build summary, inside it is an index and other components and it has a hook and it's kind of independent. You can take that folder and wherever you plop it in your code base, it's going to just work because it can take care of itself. So, look, the point of what I'm trying to tell you is that you should contain your bad code, put it in a little box and then give the box a nice interface so that other people can use your bad code without worrying about how the code itself actually works and without going to read it. This is also called an abstraction. And the main thing you should make sure when you're building abstractions is to make sure they don't leak. If you remember the slides that I showed you from the very beginning, that's my thesis why React server components and server actions are not quite there yet is that the abstraction is a little leaky but I really like the direction we're going with that where you could have self-contained components that do everything in one file.
And real quick, the easiest way to notice architectural complexity is if you use a tool that draws a dependency graph, you can see that it's all jumbled together. If you have dot, dot, dot slash, dot, dot slash, dot, dot slash imports, that just screams architectural complexity. If you have circular dependencies, something has gone very wrong. And usually if you can't tell where which code works together and which code is a public interface, that is also a bad sign. Most often I've seen that happen when people do, when people export stuff so that they can have unit test and shit. I have one more slide. So the way you fix it is you organize things by business domain, put it in a folder, make sure that anything that is tightly coupled lives together. And then one big tip that I have is to clean and validate your inputs on the boundaries of a module. And then just prop drill the data down, and that actually works really well. And you know, if you ever are in doubt between copy-pasting some code that looks similar and separating concerns, always go for separating concerns. Even if it looks similar but it talks about a different concept, it is different code and I promise you that putting it together and making it, going for the DRY principle will shoot you in the foot later. And that's what I had to say about architectural complexity. If you use that QR code, you can go, I have a newsletter, stuff like that, some further reading for any nerds who want to read some papers.
Comments