Skip to content
435: Cohesive Code with Jared Norman episode artwork

Jul 30, 2024 28 min

435: Cohesive Code with Jared Norman

from The Bike Shed Hosted by thoughtbot

How easy is it for a layperson to understand your systems? Jared Norman is a software consultant, speaker, and host of the Dead Code Podcast who specializes in building e-commerce applications in Ruby on Rails. This episode follows two recent talks at RailsConf and covers a theme that emerged from both of them: coupling and cohesion. Tuning in, you’ll gain insights on how to create more cohesive components to allow for change and improve your understanding of value objects, systems, and more. You’ll also hear about navigating the complexity of domain-driven design and learn how to gauge if your code is easy to understand through a simple rule of thumb. We discuss what it might look like to improve the cohesion of individual objects, identify your systems’ seams to create simplicity, and the liminal space between inheritance and composition and the role of decorators in moving through it. Join us today to hear all this and more! Key Points From This Episode: Introducing Jared Norman recent speaker at RailsConf and Ruby on Rails specialist. Jared’s interests outside of coding: cycling. Themes that emerged from Jared and Stephanie’s talks: coupling and cohesion. A rule of thumb for achieving high cohesion. How value objects tie into the idea of cohesion. Creating more cohesive components in order to have code and systems that are easier to change. The relationships between objects in increasing cohesion and how complex nestings of objects can hinder this. Rearranging systems in order to find seams and create cohesion. Simplifying code in order to facilitate it working independently to support functionality. Improving systems by identifying opportunities for decoupling and other relationships. Inheritance, composition, and decorators and the liminal space between. The complexity of domain-driven design. A rule that indicates when a system is easy to understand. Links Mentioned in Today’s Episode: Jared Norman Jared Norman on X The Most Useful Design Pattern Dead Code So Writing Tests Feel Painful. What Now? Dungeons & Dragons & Rails by Joël Quenneville Building Reusable Object-Oriented Systems: Composition Debugging at the Boundaries Working Effectively with Legacy Code Growing Object-Oriented Software Guided by Tests What’s in a Name Joel Quenneville on LinkedIn

Episode details come from the podcast's RSS feed.

Embed this episode

Ready to play

435: Cohesive Code with Jared Norman

0:00 28:45
of matches

Transcript Auto-generated, may contain errors

Hello, and welcome to another episode of The Bike Shed, a weekly podcast from your friends at ThoughtBot about developing great software. I'm Joelle Kenville. And I'm Stephanie Min. And today we're joined by Jared Norman, who was just recently a speaker at RailsConf, hosted his own podcasts over at the Dead Code podcast, and is also specialized in building e-commerce applications using Ruby on Rails. Hey, y'all. And together we're here to share a bit of what we've learned along the way.

So Jared, we ask this question every week. What's new in your world? Right now, I'm really excited because I am tagging along to Madison Ruby next week. And I'm going along with someone I work with, Sinem Soy, and she's speaking about a really cool topic. It's she does paragliding, among many other high-risk sports. And she's talking about what she's learned about being a better developer through wrapping a big parachute onto herself and running off of mountains. Whoa, that's really cool. I love those kinds of talks. Yeah, future team bonding activity. Oh, that sounds too scary for me. She's actually...

just been on, well, I actually don't know when this comes out, but she's just been on an episode of Dead Code or what is about to be. And she talks about how high she gets in terms of altitude when she's flying. And she mentioned that if she goes over 3000 meters for more than half an hour at a time, she's supposed to bring auxiliary oxygen. So that gives you some perspective of how high in the air you get on these things. Well, that is very intense, but also very cool. And I look forward to seeing that talk when it comes out.

So you prefer to stay on the ground. My understanding is that you're a pretty avid biker. I'm a big surfer. I do bike a bit, but my brother's actually the big biker. And that's why I actually was a little excited to be on this podcast because he was like, oh, you're going on the bike shed podcast. He needs advice on where to store all of his bikes because he does so much biking. So hopefully I'll learn something about that, I assume, over the course of this episode. Has he considered a shed? He's thought about it for sure. You know, that's definitely one of the prime ways to store your bikes. The big consideration if he's thinking of doing a bike shed is what color does he want to paint it?

Yeah, that's true. I think you have to spend 90% of the time thinking about how to store your bikes on the color of the shed itself. That's how you do this. Well, there we go. And I'll have something to go back to him and help him with his bike storage needs. So Jared, Stephanie and I were just recently on an episode of your podcast, the Dead Code podcast, talking about kind of the intersection of all of our talks at RailsConf, and particularly your talk and Stephanie's talk, because we saw an overlap on the theme of coupling. But there was also another theme that came out that's kind of the flip side of that. And that's the idea of cohesion.

I think we use the term coupling a lot as developers. Cohesion is one that's not encountered as much day to day. So before we dive into this topic, could you maybe try to define cohesion for our listeners? Yeah, absolutely. So cohesion is really just the idea of how much the behaviors of an object, the things that it does relate to each other, how closely they are interdependent and related. And put a little more simply, it's how much the stuff in an object belongs together. Was that another way of stating the single responsibility principle? Or are they kind of slightly different things? They're definitely related. It's the...

cohesion is sort of the measure of how much of things belong together. And the single responsibility principle, which is that the stating of it that I like is objects should have only one reason to change, though there's lots of different ways to look at that, is more of a rule for achieving really high cohesion. So your talk was focused on value objects as a pattern. How does cohesion tie into that? It's sort of interesting. So the way that I look at it, so value objects, and I also focus on just DTOs, data transfer objects, or data objects, and both of these things are

in some ways, groupings of data. Value objects represent some sort of value, like a color or like an amount of money or something like that, some sort of complex domain value that we might want to be working with in our application. And they allow us to take related ideas, like for example, in the context of a color, instead of working with red, green, blue values, we're able to take a look at a color as a single unit, which is useful to us because those values aren't really, in most contexts, useful on their own. They're only useful as a single cohesive unit. And you strongly encourage these value objects in a system, partly because they...

introduce better cohesion to a system. But I think for me, and I think for you as well, ideas like cohesion, they're not goals in and of themselves. They're kind of proximate metrics. Ideally, if we have more cohesive code, some other thing that we actually care about, about our code will be true. What is that metric that we're chasing actually when we're caring about cohesion? The whole framing of my talk is how we can take a relatively, it's not a particularly complex feature. It's just, in the example, we're generating an XML document. And we're looking at how

creating more cohesive components rather than just having one big chunk or breaking it up only a little bit allows us to have code that is much more, it's easier to change. And not just the code itself is easier to change, but the tests are also easier to change because that's fundamentally what we do as software developers. We change systems. So any kind of conversation about how we're, any technique, any pattern needs to be considered in the context of how it responds to change. How might our system change in the future? And how easily can we make those changes and make those changes in targeted ways and not have to do lots of little changes everywhere to handle those changes? And really cohesive objects, I argue, and I demonstrate in the talk, allows us to make systems that are easy to change in that way. Another aspect of cohesion that I picked up on in your talk is around the relationships between objects and make a statement about using the value object pattern when you have boundaries. And I really liked that because there is some aspect of like,

data passing through boundaries, right? And there is a way to make that cohesive because boundaries of a system brush up on each other and where do they touch, I think is always a really interesting question to ask. So I'm curious if that, yeah, what thoughts you have about the relationships of objects and increasing cohesion? Yeah. What I've seen a lot in more complicated systems is that people tend to sort of build up these complex nestings of objects, lots of objects calling more objects very deeply down. And that tends to make things really difficult to change. You end up having to do a lot of mocking, a lot of stubbing. And I don't think mocking and stubbing is necessarily bad, but if you have to do a lot of it, that's a signal that potentially there's a design issue, exactly like you showed in your talk, Stephanie. And instead, if we can find a way to just move the pieces around so that one piece of the system...

does its job and produces some data that is needed by another part of the system. And then we pass that data over, and then the other part of the system does its work. We've created this nice seam where the two sides are no longer coupled. Sure, they know about the structure of the data. One has to return it, and the other one has to consume it. But they no longer necessarily care about where any of that came from. And that creates nice seams to make testing really easy and fast. It allows us to have those two behaviors totally decoupled, both in the production code and in the tests, and results in easier to work with code. Yeah, I like the idea of the seams between those two boundaries.

do and should be where change gravitates towards. And you've prepared for that, right? You are saying, this is where I want to respond to change. And that being a very, I guess, intentional design decision. Because Joel and I have talked about a code smell called shock and surgery, where you are having to change code in all these different places, which I think kind of alludes to what you were saying when you have those nested objects. And yeah, there is a way to manage that and to make that cohesion, like I said, gravitate towards one place. One thing that's interesting, I think, when we're talking about boundaries and cohesion is a trend that I've noticed is that bugs tend to coalesce around boundaries.

sometimes the explicit boundaries that you've created, and sometimes you have almost like an implicit boundary in your code. And you're like, why do all the bugs always happen in this area? And it's like, well, there's an implicit seam here and we've not built any actual code structures around it. And now we're dealing with a lot of bugs in that area. Yeah, that's absolutely been my experience as well. And in rearranging systems to find those seams and build out the tests of the individual pieces, you end up learning about your system in the same way that in Stephanie's talk, you were using these characteristic tests to learn about the system. This is another way where you learn about the system and you likely will, as you rearrange things to try and find that seam, will likely.

uncover bugs potentially even as you're getting in there. That's been my experience. So we keep mentioning tests, but Jared, your talk didn't really go a lot into testing. In fact, I know from the speaker dinner, you were saying that you purposefully tried to not make this a talk about testing, which then was really interesting because Stephanie, you sort of went that flip side. You made it all about testing and how cohesion works. So for anyone who's listening who wants to get a complete picture, I definitely recommend watching these two talks back to back. We'll put the links in the show notes. But Stephanie, you want to talk a little bit about how cohesion ties into your view of testing?

Sure. I think that was a topic that was a bit of an undercurrent in my talk, I think, because I was very focused on helping people identify coupling and thinking kind of that cohesion is like kind of a bit of a... the next step like the 201 version of once you've identified coupling great so now what does it look like to have good cohesion instead but i did present one strategy of especially when you are like writing a test and you have everything in line which i think is something i recommend as well rather than maybe having a lot of helper methods or like in our spec let statements bringing them in line so you can see everything that's in front of you that's involved in getting your method or object under test look for repeated words i mentioned like this is something that i learned in journalism school where it's like the value of being concise right and i think there's a lot of different ways to identify that, but this is kind of both like a visual and written way of seeing where am I using the same variable name over and over again? And is there a hidden meaning there? And that's kind of, I didn't super explicitly mention cohesion, but I think I was alluding to it at least in terms of maybe there's something that can wrap or encapsulate this idea, which I think actually is a great suggestion too. Jared, you talked a lot about encapsulating data and you use the data class in Ruby. Do you want to say a little bit about that? Yeah, no, I was thinking the exact same thing that those kinds of patterns looking for them. And that's why I was doing my talk as well. I was looking for

data that's passed around together and choosing to group it and try and understand whether that represented a cohesive idea in the system. And in the example I give, realizing that there's this idea of a Google product that encapsulates Google's understanding of what products are and grouping all that data together. And I use Ruby's new data objects, which you can make value objects with them. In this case, I'm just grouping a bunch of things together that I understand as a single cohesive concept so that I can pass that around. find opportunities to use that. And you see, as I do that, how that shakes out in all the other objects, they suddenly make a lot more sense in terms of their behaviors. And I think that's what I'm trying to sort of encourage people to look for, is look for these seams and look for these groupings of data. And if you can kind of have those two pieces, then you might be able to just rearrange all the objects just right and create a much easier to work with system. So you're all about finding these themes in the plumps of data that might make sense as value objects. And you're a big fan of using the data class in Ruby. Are there ever times that you would consider something like a decorator pattern? There are.

But I rarely ever reach for that pattern. So that's something that I've seen so much to borrow from Stephanie's talk, pain around testing coming from that pattern. The pattern, whether you want to call them decorators or the facade pattern, where you have an object that wraps one or more other objects and calls methods on them to provide a different interface that's backed by something. We see it a lot in the Ruby community around things like serializers, things like that, or view objects. And I found that those objects, you're directly coupling this.

facade or decorator to the things that it operates on. And that means that you either have to use mocks in your tests, which, like I said, mocks are fine when they're necessary, but I don't want to use mocks unnecessarily. And anything that consumes this decorator or facade or whatever you have, now to construct those, we need the things that go inside them. And if those things need things to be constructed themselves, suddenly we're creating this really deep chain of things that either need to be mocked or constructed in our tests and in our application code too. That applies equally. This is why I...

I try to not be too test heavy in my talk is that these concepts are, they go both ways. The tests are just an indication of the design issue in the code. So I really shy away from that. If I can take the data that I need from somewhere, I mean, it makes me sound like a functional programmer or something in how I approach this. I'm more or less doing some transformation on some data and then taking that data and sending it somewhere else where something else needs to do some work. But as much as I really do like writing object-oriented code, those ideas are still very, very applicable to object-oriented code.

I was going to say, as I'm hearing this and I'm processing this in my mind, I'm like, oh, this is transformation of data rather than composition. I'm thinking of the functional concept of parse, don't validate. I don't know if you're familiar with that. And I'm wondering if that was maybe an inspiration for you. Yeah, yeah. I mean, it was an inspiration, but I definitely see the connection to that. And it's very interesting to see these parallels between functional programming and how we break down these objects. My talk, you end up with a bunch of different pieces, but they're very...

functional elements. They aren't objects with lots of different methods on them. There's not a lot of state being held or certainly state being changed. We're trying to pare back the complexity in a way that makes each piece work on its own in a way that you kind of have to if you're writing functional code, whether that's a personal validator or some other functional pattern. I was just thinking about... I was curious about the decorator pattern in some ways being a bit of an intermediary step between extracting further where...

I definitely see what you mean where, yeah, you are coupling whatever the decorator class is with what it's decorating, but you're at least starting to move some of that functionality from the class being wrapped over into this decoration. And then maybe that will kind of help you see more clearly, even more, like what aspects you can further untangle, which would be cool. I don't know how much that actually happens in practice. I suspect that it would have to be a pretty disciplined effort, but it's maybe one way of kind of like starting, right? And you're at least kind of...

putting a boundary, like we talked about earlier, making one where one didn't exist before. So yeah, maybe has a time and a place. Like I said, I imagine that it could work really well when you have a very clear idea of like, oh, these two things are very cohesive together. And like, I want to commit to these two things being related. And then kind of maybe also the inverse where it's like, I want to start to create some separation. And that's like one way to start.

Yeah, no, I agree. I think that's something that comes up is often you have to make the code. I think patterns like it aren't necessarily bad. Sometimes the pattern I used only worked because the thing that we could have built a decorator around, we really just needed to pull some data out of it and transform it. If that thing had functionality, then that actually had behaviors associated with it, potentially we wouldn't want to do what I did and something like a decorator would make more sense. But sort of setting that aside, I think what you touched on, which is that sometimes I hesitate to say,

worse here, but sometimes it makes sense to make the code worse in some way in order to make it better. And that comes often if you're refactoring maybe a gnarly piece of code, you might just get rid of all the objects, just put all of the code in one big method and then figure out what the pieces are from there. And that can be a really useful thing in the same way that... If you see the grouping, you don't necessarily have to go straight to a data object or a value object. Maybe you don't even know if you can get there. Instead, relating to how I started this, you might try out the decorator pattern, see if that makes the code and your tests, the design better in your eyes. And if it's like, okay, yeah, this is good, but what would be better is if we could decouple these things and there's an opportunity to do that, then you can go ahead. Or if you see that there's a bunch of behavior that you depend on that means you can't actually pull this out into a value object or a data object, that's fine too. You probably still improved your system. Yeah, I have a thought. And again, this kind of goes back to maybe...

written my just writing brain where you know the decorator pattern is often used for it's always to kind of like decorate almost an adjective to a model or an object right and so i was thinking about your google product example and it's like yeah like one interpretation of it is it's a product decorated by Google. And then if you like keep digging into that idea, I think what you ended up with using the value or the data transformation object pattern, it's like, no, actually it's a Google product. Like this is a thing separate from the product in our system. Like this is a concept that exists out in some other system, which is true. And then we happen to just kind of, you know, it's not the core to our system, but we exist around it and kind of elevating it to that, like not first class, but you know what I mean? Just kind of acknowledging that like, this is an entity that should exist kind of separate and on its own.

Yeah, and that sort of makes sense to me in context of sort of the domain-driven design side of things, because it allows you to make this distinction in your objects. Is this a special kind of the thing that it's decorating, or is this a new kind of thing? And choosing to make that relationship exist in the code as well as in the world, like having that mirror probably would help people sort of navigate and understand how the concepts relate to each other. You use the term special kind there, and my mind immediately goes to the concept of inheritance. Inheritance is often seen as a tool for specialization.

And years ago, I was writing an article series, Contrasting Composition and Inheritance and different ways to do it in Ruby. And then when I got to talk about decorators, I was like, it uses composition-based mechanics, but it really feels like inheritance. You're kind of almost using composition to implement or to reimplement the idea of inheritance where you can specialize an object. So it kind of sits in that liminal space between those two strategies. That's a wonderful framing. I never thought of it like that. And it sort of speaks both to what that pattern is good at and also what that pattern is less good at. It makes sense to use...

decoration when you want something that's pretty close to inheritance, but not quite. And then it also, you know, we try and I'm a big fan of trying to lean on composition wherever I can. And I see the sort of the issues with inheritance in terms of testability and flexibility in there as well, which is really interesting. I never thought about it like that. Yeah, that's really cool. And I think explains a lot of the coupling issues that you were talking about with when you've seen the decorator used. If that distinction is perhaps a little blurry. Yeah, it's certainly less coupled than inheritance, but like not decoupled, right?

Yeah, you could even, I know this is something I did in my talk, which was sort of just tell people to imagine what the tests look like. But if you imagine what the tests look like between these two different things, you have basically the same, they're going to look a little different, but you have basically the same stuff, the same kinds of dependencies are going to be present in each style of test, which is really, really interesting and sort of speaks to how apt that sort of analogy is. Jared, you name dropped the discipline of domain-driven design. And I kind of had that in the back of my head when I hear you talking about like the way you think about the objects and redefine the boundaries. But I was also hearing undertones of that, Stephanie, when you were talking about finding the variables and like having the names kind of stand out for you. I'm curious to what extent ideas from domain-driven design have influenced both of you in your talks. Yeah, I can start a little bit, which is that I think I'm actually really getting into it now, kind of as a result of the talk that I gave where, and there are also a lot of other really great talks about domain-driven design at this last RailsConf, where Jared had pointed out to me that like we both had a slide where we just listed the responsibilities of like a classroom method or something. And that's definitely a tactic in domain-driven design or a tool. And I, yeah, I think there is a lot of...

I don't know if it's excitement, but it's almost like relief, a little bit of like, oh, there are concepts and disciplines to help solve the predicament that we find ourselves in. And I am really curious to see where that goes for, the Rails community especially. And yeah, I've been having a lot of fun learning about it. The Domain Driven Design book is a very big and daunting read. And so I'm not surprised that it's not like something that everyone's like, oh, you gotta go read this thing. Because it is, it's a thing to work through and there's a lot in it. But I think...

You saw in our talks, and there was a talk about sort of the linguistics of naming at RailsConf that was really excellent. When you are pulling your system apart, you're refactoring it, and you're looking at a piece that from a functional perspective or a design perspective, just a pure code design perspective, makes sense. It's nice and cohesive. And you can think about, you're trying to count with a name for it, and you finally land on something. And it's a concept that if you...

brought it to someone who's not a programmer in the organization, they would understand what you were talking about. There's such a relief of, this is the right thing to call this thing. If I don't need to explain this concept to a layperson or a domain expert, whatever it is, the software we make is used by people, it exists in the world, and having that reflection there and being able to not have this big disconnect between the names of the things in our systems and the names of the things in the real world, it makes everything easier. We had a project for quite a while that... The names of all of the integrations to the warehouses were named things that were not words that anyone ever used, except for one of them, which was named after one of the companies that used the app, but not that warehouse service multiple companies. That was like the opposite of this mapping making sense. And it just makes things so much more confusing. And when you have these conversations about what the software needs to do with the people that use it or interact with it, and you can just use the same words as you would use with a programmer.

just so much less cognitive load or whatever, however you want to look at it. It's just easier to think about. In some ways, it almost feels like the layperson explanation of cohesion. If you can name the concept and it makes sense to a layperson, you probably have cohesion around that object. Yeah, no, I totally agree. I do think we need to often decompose our systems a little more than potentially the layperson needs to. And so that's where things, I think, get really hard. But if we could start from that position and go from there and only...

get more specialized or really just complex as we absolutely need to, it certainly leaves us in a better situation. The flip side of cohesion is coupling. We just recently recorded an episode over on your podcast, the Dead Code podcast. I'm not sure which podcast is going to come out first, so I can't promise we'll link it in the show notes. You might say these two podcasts are coupled in some way, but hopefully they each have enough of an identity that they're cohesive. On that note. Shall we wrap up?

Let's wrap up. All right, Jared, thank you so much for joining the show and talking about cohesion. Thanks for having me. Show notes for this episode can be found at bikeshed.thoughtbot.com. If you enjoyed listening, one really easy way to support the show is to leave us a quick rating or even a review in iTunes. It really helps other folks find the show. If you have any feedback or questions, you can reach us at hosts at bikeshed.fm via email. Thank you so much for listening to The Bike Shed, and we'll see you next week. Bye!

Frequently asked questions

How long is this episode of The Bike Shed?

This episode is 28 minutes long.

When was this The Bike Shed episode published?

This episode was published on July 30, 2024.

Is there a transcript available for this episode?

Yes, a full transcript is available for this episode. You can read the complete transcript on the episode page.

Can I download this The Bike Shed episode?

Yes. Use the download control on the episode player to save the publisher-provided media file.