Latest / CyberSecurity Summary / Practical Object-Oriented Design: An Agile Primer Using Ruby
Transcript
- 0:00Speaker 1: Welcome to the Deep Dive, the show where we sift
- 0:02Speaker 1: through the noise, snack up the sources, and extract the
- 0:05Speaker 1: most potent nuggets of wisdom just for you. You know,
- 0:09Speaker 1: if you've ever tried to create something, especially software, you
- 0:12Speaker 1: get that drive, right, that feeling of building something meaningful.
- 0:15Speaker 1: But then sometimes that joy kind of fades, doesn't it,
- 0:19Speaker 1: When the cost, the sheer effort of changing or maintaining
- 0:22Speaker 1: it just seems to outweigh the value.
- 0:24Speaker 2: Oh, absolutely is a path. Pretty much everyone in software walks.
- 0:27Speaker 2: You're excited, you ship something, it works, Yeah, But then
- 0:30Speaker 2: comes the long game. How does it live? How does
- 0:32Speaker 2: it adapt? That's where the real challenge often kicks in,
- 0:35Speaker 2: sometimes the.
- 0:35Speaker 1: Pain too exactly, And that challenge that's precisely why today
- 0:40Speaker 1: we're doing a deep dive into object oriented design ODE.
- 0:44Speaker 1: We're leaning heavily on a fantastic book, Practical Object Oriented
- 0:48Speaker 1: Design and Agile Primer Using Ruby by Sandy Mets. But
- 0:52Speaker 1: let's be clear, this isn't really about learning to code,
- 0:55Speaker 1: specifically Ruby or anything. Our mission here is more like
- 0:58Speaker 1: a shortcut, a shortcut to understand how to arrange code
- 1:01Speaker 1: well so your software stays joyful to work on productive
- 1:05Speaker 1: and it can actually handle future changes gracefully. It's about
- 1:08Speaker 1: getting really clued into those design principles that make working
- 1:11Speaker 1: on software a pleasure not a chore.
- 1:14Speaker 2: And it all starts really with one fundamental truth about
- 1:18Speaker 2: software dot change, it just happens. Yeah, you can imagine
- 1:20Speaker 2: this like fantasy world where software is written once requirements
- 1:23Speaker 2: are perfect, never changed.
- 1:24Speaker 1: Oh wouldn't that be nice? No bugs, no new features needed,
- 1:27Speaker 1: just done forever exactly.
- 1:30Speaker 2: And in that world, honestly, design wouldn't matter that much.
- 1:33Speaker 2: Your code could be a mess, but who cares, It
- 1:35Speaker 2: just runs. But reality, well, as Sandimes puts it so well,
- 1:39Speaker 2: change is unavoidable. It is ubiquitous, omnipresent, and inevitable. It's
- 1:43Speaker 2: just the water we swim in.
- 1:44Speaker 1: Okay, So if change is truly that inevitable, what's like
- 1:48Speaker 1: the most counterintuitive thing a developer should maybe start doing
- 1:51Speaker 1: differently right now, something that isn't immediately obvious.
- 1:54Speaker 2: That's a good one. I'd say the most counterintuitive shift
- 1:58Speaker 2: is probably aiming for FlexIt ability or trying to get
- 2:01Speaker 2: it perfect on day one. So many devs they want
- 2:05Speaker 2: the ideal solution immediately, trying to guess every future need
- 2:09Speaker 2: but if change is guaranteed, your perfect first guess is
- 2:12Speaker 2: almost certainly wrong or at least incomplete. So instead you
- 2:16Speaker 2: should focus on making the code easy to change, even
- 2:19Speaker 2: if that means putting off some decisions like unmanaged dependencies.
- 2:24Speaker 2: They are the real killers here, often silent ones. They're
- 2:27Speaker 2: like these invisible strings tying bits of code together. If
- 2:30Speaker 2: you don't manage them carefully, even a tiny tweak, well,
- 2:33Speaker 2: the damage can just radiate outward and overlapping concentric circles.
- 2:37Speaker 2: Suddenly nothing feels safe to touch. You start with something
- 2:40Speaker 2: small and successful, and before you know it, it's a tarpit.
- 2:43Speaker 2: Every step feels dangerous.
- 2:44Speaker 1: Right the tar pit. And that's where the books says
- 2:46Speaker 1: something really interesting. Design is thus an art, the art
- 2:49Speaker 1: of arranging code. It's not just about making it work now.
- 2:52Speaker 1: It's about setting it up so it's understandable and changeable.
- 2:55Speaker 2: Later, precisely. And we have tools for this art right
- 3:00Speaker 2: design principles, design patterns, These are our guides. Academics have
- 3:04Speaker 2: even tried to measure this stuff. You quantify goodness. There
- 3:07Speaker 2: are metrics from to Damburn camera. Back in the day,
- 3:11Speaker 2: they looked at things like how many connections does the
- 3:13Speaker 2: class have how complex are its methods? And they did
- 3:17Speaker 2: find correlations between using these kinds of design techniques and
- 3:20Speaker 2: ending up with higher quality code. But it's important to
- 3:23Speaker 2: remember bad ode metrics are indisputably a sign of bad design.
- 3:28Speaker 2: If your metrics look bad, your code's probably hard to change.
- 3:32Speaker 1: But getting good scores doesn't automatically mean your design is great.
- 3:36Speaker 2: Does it exactly right? Good scores don't prove the opposite.
- 3:39Speaker 2: You could definitely over anticipate the future. You might build
- 3:43Speaker 2: this elegant structure that anticipates all the wrong changes. Then
- 3:46Speaker 2: when the real future shows up, it's actually expensive to adapt,
- 3:50Speaker 2: which brings us to technical debt. It's like borrowing time
- 3:53Speaker 2: from the future. You take a short cut now, but
- 3:56Speaker 2: you know you'll have to pay it back later, probably
- 3:58Speaker 2: with interest. Sometimes it's a bunch's choice, but you have
- 4:01Speaker 2: to plan to repay.
- 4:02Speaker 1: That's a really useful way to think about it. So, yeah, okay,
- 4:04Speaker 1: design can fail. What does it look like when there's
- 4:07Speaker 1: just no design you just code? It works for a bit.
- 4:11Speaker 2: Yeah, that leads to code that's easy to write but
- 4:14Speaker 2: gradually impossible to change. Your programmers start saying things like yeah,
- 4:19Speaker 2: I can add that, but honestly, it's going to break everything.
- 4:22Speaker 2: That's a bad sign.
- 4:23Speaker 1: Okay, so that's one extreme. What about the other overdesign?
- 4:26Speaker 2: Ah? Yes, the complicated, beautiful castles of code. They look amazing, architecturally.
- 4:32Speaker 1: Sound, but totally inflexible.
- 4:34Speaker 2: Exactly, they're hemmed in by stone walls. Now, the response
- 4:37Speaker 2: to a new feature is no, I can't add that feature.
- 4:39Speaker 2: It wasn't designed to do that.
- 4:40Speaker 1: So no design is bad. Over design is bad. What
- 4:43Speaker 1: about trying to do all the design first, that whole
- 4:45Speaker 1: big upfront design thing, but you have to separate the
- 4:48Speaker 1: designers from the coders.
- 4:50Speaker 2: Yeah, that often creates this weird tension, you know, designers
- 4:53Speaker 2: versus programmers and the design itself. It's almost always wrong
- 4:57Speaker 2: in some way because as the books as, certainty is
- 5:01Speaker 2: unattainable in advance of the application's existence. You just can't
- 5:04Speaker 2: know everything upfront. And this is where agile comes in.
- 5:07Speaker 2: People sometimes think agile means no design, but that's wrong.
- 5:11Speaker 2: Agile does not prohibit design. It requires it. In fact,
- 5:13Speaker 2: it requires really good design. Design that's simple, flexible, ready
- 5:17Speaker 2: for change because change is the constant.
- 5:20Speaker 1: Okay, so if good design is about arranging code to
- 5:23Speaker 1: handle change, let's dig into object oriented programming itself. How
- 5:26Speaker 1: does OP help with this flexibility compared to older ways.
- 5:29Speaker 2: Well, think about older procedural languages. You generally had your
- 5:33Speaker 2: data structures over here and your functions that operate on
- 5:36Speaker 2: that data over there, separate things. OO languages like Ruby,
- 5:39Speaker 2: which the book uses, they combine them together into a
- 5:42Speaker 2: single thing, an object.
- 5:44Speaker 1: Hah.
- 5:45Speaker 2: So now you're not thinking about data and action separately.
- 5:48Speaker 2: You're thinking about things objects that no stuff and do
- 5:51Speaker 2: stuff all bundled together. It changes how you model the problem.
- 5:55Speaker 2: And Ruby, for instance, is open ended, you can invent
- 5:57Speaker 2: brand new types of your own. What happens is your
- 6:00Speaker 2: application sort of evolves into its own unique programming language
- 6:04Speaker 2: that is specifically tailored to your domain. You could express
- 6:07Speaker 2: ideas about your problem really naturally.
- 6:09Speaker 1: Building your own language. That's a cool perspective. Okay, let's
- 6:12Speaker 1: get practical. What are the core OD principles that help
- 6:15Speaker 1: us build these flexible systems? Starting with big one Single
- 6:18Speaker 1: responsibility principle SRP.
- 6:20Speaker 2: Right SRP. The core idea sounds simple. A class should
- 6:25Speaker 2: do the smallest possible useful thing. It should have a
- 6:28Speaker 2: single responsibility. If a class is juggling too many jobs,
- 6:31Speaker 2: It gets hard to reuse, things get tangled up. Changes
- 6:34Speaker 2: in one responsibility break others. Yeah, it's a mess.
- 6:37Speaker 1: Single responsibility sounds easy, But how do you actually know
- 6:41Speaker 1: if a class is doing too much? Is there a
- 6:44Speaker 1: practical test?
- 6:45Speaker 2: Yeah, there's a neat trick. The book suggests you interrogate it.
- 6:48Speaker 2: You try phrasing its methods as questions directed to the class.
- 6:51Speaker 2: So please, mister gear, what is your ratio? That sounds
- 6:55Speaker 2: totally normal. Gear should know its ratio. But please, mister gear,
- 6:58Speaker 2: what is your tire size? That just sounds wrong? Doesn't it?
- 7:01Speaker 2: A gear doesn't have a tire. If the question feels
- 7:03Speaker 2: awkward or nonsensical, the class is probably doing things it
- 7:06Speaker 2: shouldn't be. It has too many responsibilities, and this ties
- 7:09Speaker 2: into asking yourself, what is the future cost of doing
- 7:12Speaker 2: nothing today? Sometimes the best move is to not make
- 7:15Speaker 2: a big design decision yet, wait until you know more.
- 7:18Speaker 2: Making a class too early.
- 7:19Speaker 1: Is just guessing, and this idea single responsibility. It applies
- 7:23Speaker 1: to methods too, not just classes.
- 7:24Speaker 2: Oh, absolutely, methods should also do one thing well. Keeping
- 7:29Speaker 2: methods small and focused has huge benefits. It makes the
- 7:32Speaker 2: code clearer, often eliminates the need for comments Because the
- 7:35Speaker 2: method name says it all. It encourages reuse. Plus, small
- 7:39Speaker 2: focus methods are much easier to move to a different
- 7:42Speaker 2: class if you realize later that the responsibility belongs elsewhere.
- 7:46Speaker 2: Sometimes you might even start by putting a small related
- 7:49Speaker 2: responsibility inside a temporary structure like a structin Ruby, within
- 7:53Speaker 2: the main class. It's like a holding pattern before deciding
- 7:56Speaker 2: if it needs its own full class.
- 7:58Speaker 1: Okay, so keep things focused, single responsibility. But these focused
- 8:02Speaker 1: things need to work together, which brings us to dependencies
- 8:05Speaker 1: the glue. The book calls it a little dot of blue,
- 8:08Speaker 1: but warns that too much can strangle your application. How
- 8:11Speaker 1: do we manage that glue?
- 8:12Speaker 2: Managing dependencies is maybe the central challenge, and a key
- 8:15Speaker 2: technique is dependency injection. Sounds fancy, but the idea is simple.
- 8:20Speaker 2: Instead of an object creating its own collaborators, like Gear
- 8:23Speaker 2: making its own wheel, you pass the collaborator in from
- 8:26Speaker 2: the outside, so Gear just gets given a real object
- 8:29Speaker 2: when it's created. The result, Gear actually becomes smarter because
- 8:32Speaker 2: it knows less. It doesn't need to know how to
- 8:35Speaker 2: create a wheel, or even that it is a wheel.
- 8:37Speaker 2: All Gear cares about is that the object it receives
- 8:40Speaker 2: responds to the messages it needs, like diameter. That's it
- 8:43Speaker 2: immensely powerful for flexibility.
- 8:46Speaker 1: That makes a lot of sense Practically speaking, how often
- 8:49Speaker 1: do you find yourself using dependency injection? What's the biggest
- 8:51Speaker 1: win you see from it?
- 8:52Speaker 2: It becomes second nature. Really, you're constantly thinking, does this
- 8:55Speaker 2: object really need to know how to create that other object?
- 8:58Speaker 2: Usually the answer is no, So in jen acting dependencies,
- 9:01Speaker 2: especially through the constructor, becomes standard practice. The biggest win,
- 9:04Speaker 2: hands down, it's testability and substitutability. You can easily swap
- 9:08Speaker 2: at a fake test wheel when testing gear, or a
- 9:10Speaker 2: completely different kind of wheel later without changing gear at all,
- 9:13Speaker 2: as long as it provides that diameter method and related tip,
- 9:16Speaker 2: the book mentions using keyword arguments for initialization instead of
- 9:20Speaker 2: relying on the order of arguments. You name them year
- 9:23Speaker 2: dot new chane ring five bay good fifty two, cog
- 9:26Speaker 2: eleven wheel dot mywheel. This makes the code much clearer
- 9:30Speaker 2: and less brittle. If you add or change arguments later,
- 9:33Speaker 2: no more remembering was chain ring first or second? Stepping back,
- 9:36Speaker 2: the big idea here is depend on things that change
- 9:38Speaker 2: less often than you do. Concrete classes, specific implementations, they
- 9:42Speaker 2: tend to change. Abstract interfaces these common stable qualities they
- 9:47Speaker 2: change much less often. So you want your pendencies pointing
- 9:49Speaker 2: towards abstractions, not concrete details.
- 9:52Speaker 1: Okay, focus responsibilities, manage dependencies, But how do these objects
- 9:58Speaker 1: actually communicate effectively? How do we design those conversations well?
- 10:01Speaker 2: Fundamentally, an object oriented application is defined by messages being
- 10:06Speaker 2: sent between objects. The book uses a great analogy a
- 10:10Speaker 2: restaurant kitchen. The menu is the public interface. Customers order
- 10:14Speaker 2: off the menu. They don't need to know the complex
- 10:17Speaker 2: process happening inside the kitchen. That's private. Your classes should
- 10:20Speaker 2: be the same. A clear public interface, the menu of
- 10:23Speaker 2: methods others can call and private internal.
- 10:25Speaker 1: Workings and sequence diagrams come in handy here. The book
- 10:29Speaker 1: calls them a perfect low cost way to experiment. It
- 10:32Speaker 1: shifts thinking from what does this class do to who
- 10:35Speaker 1: responds to this message exactly?
- 10:37Speaker 2: That shift is crucial because, as Met says, you don't
- 10:40Speaker 2: send messages because you have objects. You have objects because
- 10:42Speaker 2: you send messages. Thinking about the messages first helps you
- 10:45Speaker 2: discover the objects you actually need and what their public
- 10:48Speaker 2: interfaces should.
- 10:49Speaker 1: Be, which leads to another key idea, asking for what
- 10:52Speaker 1: instead of telling how Take the example.
- 10:55Speaker 2: Of a trip class and a mechanic object. Instead of
- 10:57Speaker 2: trip telling mechanic every single step, check breaks and flight
- 11:00Speaker 2: tires Lubrik chain the trips. You just ask the mechanic
- 11:03Speaker 2: to do the what prepare trip and trust the mechanic
- 11:06Speaker 2: to know how to do that. This is blind trust
- 11:09Speaker 2: in ode. Objects collaborate by relying on each other's public
- 11:12Speaker 2: interfaces without needing intimate knowledge of the internal details. It
- 11:16Speaker 2: keeps them loosely coupled.
- 11:17Speaker 1: And what about controlling access? Ruby has public protected private?
- 11:21Speaker 1: Are these strict walls?
- 11:22Speaker 2: Not really? You know, the book calls them flexible barriers
- 11:25Speaker 2: in Ruby, and like some other languages, these aren't absolute guarantees.
- 11:29Speaker 2: Because of that, many experienced rubyists actually prefer using comments
- 11:33Speaker 2: or naming conventions like starting private method names with an
- 11:36Speaker 2: underscore to signal intent, rather than relying solely on the
- 11:39Speaker 2: key words. It's more about convention and trusting other developers.
- 11:44Speaker 1: Okay, so flexible barriers. This sounds related to minimizing dependencies. Again.
- 11:48Speaker 1: Are there common ways people mess up object communication anti patterns?
- 11:53Speaker 2: Oh? Definitely. A classic one is violating the law of demeter.
- 11:56Speaker 2: The simple version is only talk to your immediate neighbors,
- 11:59Speaker 2: or even simpler, use only one dot. You see code
- 12:02Speaker 2: like customer dot, bicycle, dot wheel, dot rotate. That's often
- 12:06Speaker 2: called a train wreck.
- 12:07Speaker 1: A train wreck, okay, why is that so bad?
- 12:09Speaker 2: Because it deeply couples the code calling rotate to the
- 12:12Speaker 2: entire internal structure of customer, bicycle and wheel. If any
- 12:17Speaker 2: part of that chain changes, maybe bicycle no longer directly
- 12:20Speaker 2: holds a wheel, this code breaks even though just wanted
- 12:23Speaker 2: to rotate something. These chains are not transparent, reasonable, usable,
- 12:28Speaker 2: or exemplary. They hide dependencies and make the code fragile.
- 12:32Speaker 2: When you find yourself writing code like that, it's a
- 12:34Speaker 2: strong signal. Listening to demeter tells you where your public
- 12:37Speaker 2: interfaces are lacking. You probably need a method on bicycle
- 12:40Speaker 2: itself like rotate wheel to hide that internal chain.
- 12:43Speaker 1: That makes sense. It forces better interface design. Okay, let's
- 12:46Speaker 1: shift gears to structuring behavior. How can we use dynamic
- 12:49Speaker 1: behavior like duct typing to keep costs down?
- 12:52Speaker 2: Right? Duck typing. The core idea is it's not what
- 12:55Speaker 2: an object is that matters, it's what it does. If
- 12:59Speaker 2: an object walks like a duck and quacks like a duck,
- 13:01Speaker 2: than for the purpose of the code interacting with it.
- 13:04Speaker 2: You can treat it as a duck. You care about
- 13:06Speaker 2: its behavior. It's public interface, not its specific class name.
- 13:10Speaker 2: The book uses a funny metaphor. An object is like
- 13:13Speaker 2: a partygoer at a masquerade ball that changes masks. Its
- 13:16Speaker 2: role can change depending on the context. A big warning
- 13:19Speaker 2: sign the book points out is seeing a case statement
- 13:22Speaker 2: that switches based on class names like caseobject dot class,
- 13:26Speaker 2: when wheel, when gear. That kind of toad should grab
- 13:29Speaker 2: your attention as if it were playing trumpets. It usually
- 13:32Speaker 2: means there's a hidden duck type, a shared role or
- 13:34Speaker 2: interface waiting to be discovered. For example, maybe different objects
- 13:38Speaker 2: all know how to prepare themselves. Instead of checking their class,
- 13:40Speaker 2: you just call prepare on them. You've identified a preparer
- 13:43Speaker 2: duck type. This makes your code way more flexible. You
- 13:46Speaker 2: can add new kinds of preparers later without changing the
- 13:48Speaker 2: code that uses them. It's easier to extend but casting
- 13:52Speaker 2: a veil over the underlying class, which is often a
- 13:54Speaker 2: good thing.
- 13:55Speaker 1: Okay, so that's dynamic behavior with ducks. What about more structured,
- 13:59Speaker 1: hierarchical behavior using classical inheritance.
- 14:01Speaker 2: Inheritance is basically automatic message delegation. It's like a family tree.
- 14:07Speaker 2: A subclass automatically inherits behavior from its superclass. Subclasses are
- 14:11Speaker 2: the specializations of their superclasses. A mountain bike is a
- 14:15Speaker 2: special kind of bicycle. Finding the right abstraction level and
- 14:18Speaker 2: inheritance can be tricky. The book suggests a strategy push
- 14:21Speaker 2: everything down and then pull some things up. Start by
- 14:24Speaker 2: making things specific in subclasses. Then identify common code or
- 14:28Speaker 2: behavior and carefully pull it up into a shared superclass.
- 14:31Speaker 2: Throughout this, you should constantly ask what will happen when
- 14:34Speaker 2: I'm wrong. This encourages making conservative choices, keeping the cost
- 14:37Speaker 2: of future changes low.
- 14:39Speaker 1: Inheritance can get tricky, especially with super when subclasses have
- 14:42Speaker 1: to remember to call their parents' implementation. That seems error prone.
- 14:46Speaker 2: It definitely is, and that's where the template method pattern shines.
- 14:50Speaker 2: Often using hook methods. Think of the superclass providing a
- 14:53Speaker 2: template an overall algorithm, but it defines placeholder methods. The
- 14:57Speaker 2: hooks like post initialized for customs, setup or local spares
- 15:01Speaker 2: for bike specific parts. Subclasses can then override these hooks
- 15:06Speaker 2: to plug in their specific behavior without needing to explicitly
- 15:09Speaker 2: call super in the main method. This structure removes the
- 15:12Speaker 2: need for subclasses to send super in many cases and
- 15:16Speaker 2: dramatically reduces coupling between the layers. It makes the hierarchy
- 15:19Speaker 2: much cleaner. General advice create shallow hierarchies. Deep inheritance trees
- 15:25Speaker 2: build up a lot of dependencies and become hard to manage.
- 15:28Speaker 2: Keep it simple if you can.
- 15:29Speaker 1: All right, So we have ducks for dynamic behavior, inheritance
- 15:32Speaker 1: for hierarchical specialization. What about combining distinct things together composition.
- 15:37Speaker 2: Composition is about building complex objects by combining smaller, independent parts.
- 15:42Speaker 2: It's a has a relationship. A car has an engine,
- 15:44Speaker 2: has a wheels. The whole becomes more than the sum
- 15:47Speaker 2: of its parts. Like notes forming music, the parts work together.
- 15:49Speaker 1: And the book's recumbent bike example really drives this home
- 15:52Speaker 1: right Well, it's.
- 15:53Speaker 2: A fantastic example. Using inheritance. Adding support for a new
- 15:57Speaker 2: recumbent bike took like nineteen lines of code chain just
- 16:00Speaker 2: scattered around, But by refactoring to use composition. Where a
- 16:04Speaker 2: bicycle has a parts object created by a parts factory,
- 16:08Speaker 2: adding the recumbent bike became just three lines of configuration.
- 16:11Speaker 2: That's it. It shows the power of composition for managing
- 16:14Speaker 2: variations and adding new features. The decision rule of the
- 16:17Speaker 2: book offers is pretty stark. If you cannot explicitly defend
- 16:20Speaker 2: inheritance as a better solution, use composition.
- 16:23Speaker 1: Inheritance carries more risk, so composition is generally preferred unless
- 16:27Speaker 1: there's as strong is a relationship. What are the main
- 16:29Speaker 1: benefits and drawbacks exactly?
- 16:32Speaker 2: Composition usually leads to systems that are more transparent, usable, reasonable,
- 16:36Speaker 2: and have a high tolerance for change. The parts are independent,
- 16:40Speaker 2: easier to test, easier to swamp out. The main cost
- 16:43Speaker 2: is that sometimes the overall structure the whole might feel
- 16:47Speaker 2: less obvious than a clear inheritance try and you often
- 16:50Speaker 2: need to write explicit delegation code to pass messages from
- 16:53Speaker 2: the whole object to its parts. But the guideline holds.
- 16:56Speaker 2: Use inheritance for is a relationships, true specialistation, low risk,
- 17:01Speaker 2: shallow hierarchies. Use composition for has relationships. When an object
- 17:05Speaker 2: is built from distinct parts, composition is usually the safer,
- 17:08Speaker 2: more flexible.
- 17:09Speaker 1: Bet Okay, awesome. We've designed our flexible system using these principles.
- 17:13Speaker 1: Now how do we make sure it actually works and
- 17:15Speaker 1: stays working? Testing and the book argues the purpose isn't
- 17:18Speaker 1: just finding bugs.
- 17:19Speaker 2: Not at all. Just like design, the true purpose of
- 17:22Speaker 2: testing is to reduce costs. Good tests make changes cheaper
- 17:26Speaker 2: and safer. Bad tests or hard to write tests increase costs.
- 17:30Speaker 2: And there's a direct link. Tests are the canary in
- 17:33Speaker 2: the coal mine when the design is bad. Testing is hard.
- 17:36Speaker 2: If your tests are painful to write, it's often assign
- 17:38Speaker 2: your design needs improving.
- 17:39Speaker 1: That's a powerful connection. If testing is about reducing costs,
- 17:43Speaker 1: how does that change what we test? How do we
- 17:46Speaker 1: write cost effective tests?
- 17:47Speaker 2: It radically shifts the focus away from just code coverage
- 17:50Speaker 2: towards testing behavior and interfaces. The guidelines are pretty clear.
- 17:55Speaker 2: Test incoming messages that change state, So incoming messages should
- 17:59Speaker 2: be tested for the state they return. If you call
- 18:02Speaker 2: deposit one hundred, test that the balance is now one
- 18:04Speaker 2: hundred higher. Test outgoing commands. Outgoing command messages should be
- 18:09Speaker 2: tested to ensure they get sent. If your object is
- 18:12Speaker 2: supposed to tell another object to send mail, tests that
- 18:14Speaker 2: the send to mail message was sent, not the details
- 18:17Speaker 2: of how. But importantly, don't test outgoing queries. Outgoing query
- 18:21Speaker 2: messages should not be tested if your object just asks
- 18:25Speaker 2: another object for data like get chooser name and doesn't
- 18:28Speaker 2: change anything. You don't need to test that interaction directly
- 18:30Speaker 2: in the calling objects test testing the query method itself
- 18:33Speaker 2: is enough. This approach minimizes the coupling between your tests
- 18:36Speaker 2: and the implementation details, making tests less brittle and cheaper
- 18:40Speaker 2: to maintain when the code changes. Makes sense.
- 18:42Speaker 1: What about test doubles, stubs and mocks. They can sometimes
- 18:44Speaker 1: create this alternate universe where tests pass but the real
- 18:48Speaker 1: app is broken. Right.
- 18:49Speaker 2: Yeah, that's a real danger. Your doubles might behave slightly
- 18:53Speaker 2: differently than the real objects, leading to false confidence. The
- 18:56Speaker 2: way to combat this is by using tests to document
- 18:59Speaker 2: roles or interfaces. You can actually write tests for your
- 19:02Speaker 2: test doubles. For example, create a diameterizable interface test. Any
- 19:07Speaker 2: object claiming to be usable as a diameterizable thing, like
- 19:11Speaker 2: your real wheel and your diameter double used in tests,
- 19:13Speaker 2: must pass this interface test. This ensures your test doubles
- 19:17Speaker 2: honor the interface this test expects and keeps them synchronized
- 19:20Speaker 2: with the real object's behavior.
- 19:22Speaker 1: Okay, interesting. What about testing private methods? Big debate there sometimes?
- 19:26Speaker 2: Huh. Yes, The book's advice is wonderfully pragmatic and a
- 19:29Speaker 2: bit cheeky. Never write them, and if you do, never
- 19:32Speaker 2: ever test them, unless, of course, it makes sense to
- 19:34Speaker 2: do so. Basically, testing private methods directly is usually a
- 19:38Speaker 2: bad idea. It couples your test tightly to implementation details
- 19:42Speaker 2: that should be free to change. If the public interface works,
- 19:45Speaker 2: the privates are probably okay. However, the unless is important
- 19:48Speaker 2: if you're dealing with a really complex private method, maybe
- 19:51Speaker 2: as part of a temporary refactoring. Writing a targeted test
- 19:54Speaker 2: for it can sometimes reduce the barriers to refactoring by
- 19:58Speaker 2: giving you confidence as you clean it up. But treth
- 20:00Speaker 2: those tests as temporary got it.
- 20:02Speaker 1: And finally, testing code that uses inheritance where subclasses rely
- 20:06Speaker 1: on shared superclass behavior.
- 20:08Speaker 2: This is where those interface tests and what the book
- 20:11Speaker 2: calls subclass responsibility tests really shine. You can write tests
- 20:15Speaker 2: that define the contract a subclass must fulfill to work
- 20:20Speaker 2: correctly with the superclass, template method or hooks. These tests
- 20:24Speaker 2: take all of the pain out of testing the common
- 20:26Speaker 2: behavior of subclasses. A new developer can come on, create
- 20:29Speaker 2: a new subclass, run the tests, and the tests will
- 20:32Speaker 2: tell them exactly which methods they need to implement correctly.
- 20:35Speaker 2: It makes extending hierarchies much safer.
- 20:37Speaker 1: Wow. What an amazing journey through arranging code We've covered responsibilities, dependencies, interfaces, ducts, inheritance,
- 20:45Speaker 1: composition testing. It's a lot, but it connects beautifully, it.
- 20:49Speaker 2: Really does, And that final insight from the book is
- 20:51Speaker 2: so profound. Object oriented design is fractal. The central problem
- 20:55Speaker 2: is to define an extensible way for objects to communicate,
- 20:58Speaker 2: and at every level of magnification, this par looks the
- 21:00Speaker 2: same whether you zoom out to the whole system or
- 21:03Speaker 2: zoom into a single method interacting with its variables. That
- 21:06Speaker 2: core challenge of clear communication and managed dependencies is always there.
- 21:10Speaker 1: Fractal design. That's a fantastic way to think about it.
- 21:12Speaker 1: It scales, and the book leaves us with a final
- 21:15Speaker 1: piece of wisdom, almost a challenge. The rules of design
- 21:19Speaker 1: are meant to be broken. Learning to break them well
- 21:22Speaker 1: is a designer's greatest strength.
- 21:23Speaker 2: Absolutely, It's not about blindly following rules. It's about understanding
- 21:28Speaker 2: the principles behind the rules so you know when and
- 21:31Speaker 2: how to bend them effectively. So yeah, persist, practice, experiment, imagine,
- 21:37Speaker 2: do your best work, and all else will follow.
- 21:39Speaker 1: So listening to all this, what's the one thing that
- 21:41Speaker 1: really stands out to you? Maybe it's designing for that
- 21:44Speaker 1: inevitable change or managing that glue of dependencies, or thinking
- 21:48Speaker 1: about ducks, whatever it is, let that idea challenge how
- 21:51Speaker 1: you think about building software next time See where it
- 21:54Speaker 1: takes you