Latest / CyberSecurity Summary / Component-Based Rails Applications: Large Domains Under Control
Transcript
- 0:00Speaker 1: Have you ever been knee deep in a software project
- 0:04Speaker 1: watching it grow and suddenly that familiar feeling of dread
- 0:07Speaker 1: creeps in. Oh yeah, but feeling that your elegant creation
- 0:11Speaker 1: is slowly but surely turning into well a ball of mud.
- 0:15Speaker 2: Mm hmm, complex, hard to change.
- 0:17Speaker 1: Exactly, Test suites, crawling bugs playing hide and seek. Yeah,
- 0:22Speaker 1: I think we've all been there. Definitely. Today we're diving
- 0:24Speaker 1: deep into a powerful solution for those growing pains. While
- 0:30Speaker 1: rails is absolutely fantastic for getting applications off the ground quickly,
- 0:34Speaker 1: it really is, it can face significant challenges when those
- 0:37Speaker 1: applications scale into large, complex beasts.
- 0:39Speaker 2: That's the crux of it.
- 0:40Speaker 1: And that's where component based Rails applications or CVRA comes in.
- 0:44Speaker 1: It's an architectural approach designed to bring order and control
- 0:47Speaker 1: back to those unruly rails code.
- 0:49Speaker 2: Bases right, and we're drawing heavily from Steven Hagman's excellent
- 0:53Speaker 2: book on this Component based Rails Applications, Large Domains under Control,
- 0:58Speaker 2: a really solid source.
- 1:00Speaker 1: Absolutely, So our mission today is to give you a shortcut.
- 1:03Speaker 1: We're going to unpack what CBI is, why it matters,
- 1:07Speaker 1: and how can transform a chaotic application into something well structured, maintainable.
- 1:13Speaker 2: And hopefully find some surprising insights along the way, things
- 1:16Speaker 2: that maybe go beyond the usual architecture talks.
- 1:18Speaker 1: Okay, so let's unpack this. Many rails apps start out elegant, right,
- 1:23Speaker 1: they're monoliths, a single cohesive unit. But what really happens
- 1:27Speaker 1: when they grow, say, beyond just a handful of developers
- 1:30Speaker 1: or features. Where does that wall of mud feeling actually
- 1:34Speaker 1: come from?
- 1:35Speaker 2: Well that's a critical point in an applications life cycle,
- 1:38Speaker 2: isn't it. You start seeing these telltale signs, like what
- 1:41Speaker 2: feature development starts to grind to a halt? It just
- 1:43Speaker 2: gets incredibly expensive to add anything new your test suites.
- 1:47Speaker 2: They get agonizingly slow, brittle. Sometimes honestly, they just get
- 1:53Speaker 2: abandoned because maintaining them is too painful.
- 1:55Speaker 1: Yeah, I've seen that happen.
- 1:56Speaker 2: And those elusive bugs they become increasingly difficult to track down. Now,
- 2:00Speaker 2: sheer size plays a role, for sure, but the root
- 2:03Speaker 2: cause is often complexity just spiraling out of control.
- 2:07Speaker 1: So it is about complexity spiraling. We're aiming for maintainability, obviously,
- 2:13Speaker 1: but how do we even begin to understand what a
- 2:15Speaker 1: complex code base truly means? Is there like a way
- 2:19Speaker 1: to measure that or is it just this gut feeling.
- 2:22Speaker 2: That's a fascinating question. And what's really interesting here is
- 2:24Speaker 2: how network theory.
- 2:26Speaker 1: Applies network theory in code.
- 2:28Speaker 2: Yeah, imagine your codebase as a sort of social network.
- 2:32Speaker 2: Your classes are the nodes, right, okay, and their interactions
- 2:36Speaker 2: the calls between them, those are the connections. Now, concepts
- 2:39Speaker 2: like Metcalf's law suggest utility, and you could argue complexity too,
- 2:43Speaker 2: grows with n squared.
- 2:45Speaker 1: Connection n square Okay, so it grows fast, it gets worse.
- 2:48Speaker 2: Read's law suggests it could even be two to the
- 2:50Speaker 2: power of n with subgroups. Think about adding one more
- 2:52Speaker 2: person to a small party. They don't just add one
- 2:55Speaker 2: new interaction, They potentially start new conversations with everyone already there.
- 2:58Speaker 2: The more people, the fast or the social connections, and
- 3:01Speaker 2: the complexity just explodes.
- 3:03Speaker 1: Wow, Okay, that sounds kind of depressing actually, like it's
- 3:08Speaker 1: an inescapable fate for any big.
- 3:09Speaker 2: App uh huh No, not depressing, Well, maybe a little daunting. Yeah,
- 3:13Speaker 2: But look, while adding more code always increases potential complexity,
- 3:17Speaker 2: that's just physics. Almost Good architecture is about staying.
- 3:20Speaker 1: In control, staying in control.
- 3:22Speaker 2: Introducing components, even with their own little bit of overhead,
- 3:25Speaker 2: actually helps manage this complexity, especially as the application gets bigger.
- 3:29Speaker 2: The goal isn't to eliminate complexity entirely. That's impossible. It's
- 3:33Speaker 2: more like learning to sail effectively. When the tide is rising.
- 3:37Speaker 2: You just want to stay afloat afloat.
- 3:39Speaker 1: I like that. So if the big monolist is the
- 3:41Speaker 1: ball of mud, how do we break it apart without
- 3:45Speaker 1: just creating, you know, smaller balls of mud everywhere? What
- 3:49Speaker 1: exactly is a component in this CBI context? Are we
- 3:52Speaker 1: talking micro services here?
- 3:53Speaker 2: Good question? Not necessarily microservices, though there are olapse in
- 3:57Speaker 2: the philosophy. The core idea is some Really, a component
- 4:01Speaker 2: is just a piece of software with really clear responsibilities,
- 4:04Speaker 2: a well defined public interface so you know how to
- 4:07Speaker 2: talk to it, and an explicit dependency on its surroundings.
- 4:11Speaker 2: It says exactly what it needs from the outside world.
- 4:13Speaker 1: Explicit dependency, and crucially this is key.
- 4:16Speaker 2: Unlike typical objects, maybe components cannot have circular dependencies. A
- 4:21Speaker 2: can depend on B, but B cannot depend back on
- 4:24Speaker 2: A directly or indirectly.
- 4:25Speaker 1: Uh okay, no loops.
- 4:27Speaker 2: No loops. Think of it as a self contained, independent unit.
- 4:31Speaker 1: Right, So here's where it gets really interesting for me
- 4:34Speaker 1: in Rails, what does that look like practically? What are
- 4:37Speaker 1: we actually building these components out of?
- 4:38Speaker 2: Stephan Hageman in the book really emphasizes using standard Ruby tools,
- 4:43Speaker 2: Ruby gems, and specifically Rails engines.
- 4:46Speaker 1: Rails engines.
- 4:46Speaker 2: Okay, I know those, and since Rails three point zero,
- 4:49Speaker 2: every Rails application is actually just an engine itself under
- 4:53Speaker 2: the hood.
- 4:54Speaker 1: Oh interesting, I didn't realize that. Yeah.
- 4:55Speaker 2: So engines are basically miniature applications that provide functionality to
- 4:59Speaker 2: their host application. Think about gems. You probably already use
- 5:02Speaker 2: like devise for authentication, right, yeah, device or threaded for forums,
- 5:08Speaker 2: or spree for e commerce. These are all basically components
- 5:12Speaker 2: implemented as engines. Oh okay, they're isolated, you can test
- 5:15Speaker 2: them individually, and they have those explicit non cyclic dependencies
- 5:19Speaker 2: we talked about that clear boundary is absolutely fundamental to
- 5:23Speaker 2: containing the complexity.
- 5:24Speaker 1: That makes perfect sense. So it's about clearly defined boundaries
- 5:28Speaker 1: and relationships. Is like building with Legos instead of Plato exactly.
- 5:33Speaker 2: That's a great analogy, much more structured, much easier to
- 5:36Speaker 2: reason about and change individual pieces so beyond just.
- 5:40Speaker 1: The tech definition, what are the real world, tangible benefits
- 5:44Speaker 1: for a team working on a large rails app. Why
- 5:46Speaker 1: does adopting this component based approach truly matter for you
- 5:49Speaker 1: and your project's.
- 5:50Speaker 2: Day to day Yeah, connecting it to the bigger picture.
- 5:52Speaker 2: CBRA dramatically improve several key areas. First off, improve communication
- 5:57Speaker 2: of intent.
- 5:57Speaker 1: Communication of intent? How so well.
- 6:00Speaker 2: Components force you to name and structure parts of your
- 6:02Speaker 2: application at a higher level. Imagine walking into a massive
- 6:05Speaker 2: codebase you've never seen before.
- 6:08Speaker 1: Daunting totally, But.
- 6:10Speaker 2: What if just by looking at its top level structure,
- 6:13Speaker 2: seeing component names like importer, source, master programs, you could
- 6:18Speaker 2: immediately grasp its core purpose.
- 6:20Speaker 1: I see the structure itself tells a story exactly.
- 6:24Speaker 2: CBRA helps the architecture itself scream the applications domain, not
- 6:29Speaker 2: just the framework underneath. It makes the intent clearer.
- 6:32Speaker 1: Okay, that's powerful. What else?
- 6:34Speaker 2: Second? You get improved collaboration among developers.
- 6:37Speaker 1: How does it help teams work together better?
- 6:39Speaker 2: Those explicit dependencies we keep mentioning they drastically reduce conflict zones.
- 6:45Speaker 2: The book gives this great example where maybe working on
- 6:47Speaker 2: a source component versus a user component reduces potential merge
- 6:51Speaker 2: conflicts from affecting the entire monolith down to maybe just
- 6:55Speaker 2: shared components like admin or navigator.
- 6:57Speaker 1: So fewer people stepping on each other's.
- 6:59Speaker 2: Toes precisely, Teams can work more independently on different areas,
- 7:03Speaker 2: leading to smooter development, fewer painful merges.
- 7:06Speaker 1: Nice, what's next.
- 7:07Speaker 2: Third, there's improved creation of features.
- 7:09Speaker 1: Easier to build new stuff.
- 7:11Speaker 2: Yeah, new features can often be developed as entirely new components.
- 7:15Speaker 2: Then you can simply feature flag them by like just
- 7:18Speaker 2: not loading that component's engine in production until it's ready.
- 7:21Speaker 1: Oh so, no need for complex branching strategies or separate
- 7:24Speaker 1: feature flag gems necessarily Often no.
- 7:27Speaker 2: You can even do things like duplicate an existing engine,
- 7:30Speaker 2: say reports, create Reports V two alongside it, test it,
- 7:34Speaker 2: deploy it, and then just sunset the old one later.
- 7:36Speaker 2: It allows for really agile evolution.
- 7:39Speaker 1: Okay, that's clever.
- 7:40Speaker 2: Fourth, you'll see improved maintenance. This is a big one.
- 7:43Speaker 1: Maintenance Yeah, always a pain point.
- 7:45Speaker 2: Upgrading external dependencies even rails itself becomes much easier. You
- 7:50Speaker 2: can test and adapt individual components to new versions while
- 7:53Speaker 2: the main app stays compatible.
- 7:54Speaker 1: For a while. Ah, so you avoid those massive terrifying
- 7:58Speaker 1: upgrade rails now brand inches that paralyze the team.
- 8:01Speaker 2: Hopefully it breaks the problem down. You tackle dependencies component
- 8:05Speaker 2: by component.
- 8:05Speaker 1: Makes sense and the last one.
- 8:07Speaker 2: And finally just improved comprehension of application.
- 8:10Speaker 1: Parts, easier to understand the code.
- 8:12Speaker 2: Definitely breaking that huge structure into smaller cohesive bits with
- 8:17Speaker 2: small interfaces. Let's developer's focus. You can load just one
- 8:21Speaker 2: component's context into your head at a time, reducing that
- 8:24Speaker 2: cognitive load. You're not trying to juggle the entire class
- 8:28Speaker 2: social network anymore.
- 8:29Speaker 1: Right, just focus on your lego block. I'm sold on
- 8:32Speaker 1: the benefits. So, if you're convinced and you want to
- 8:35Speaker 1: start a new large Rails project with CBR in mind,
- 8:39Speaker 1: how do you actually do it? What are the practical
- 8:41Speaker 1: first steps? How do you build that lego castle?
- 8:44Speaker 2: Well, the process starts pretty much like any rails app,
- 8:47Speaker 2: Rails new.
- 8:47Speaker 1: Repn okay standard start.
- 8:48Speaker 2: But then pretty much right away, you create a component's
- 8:51Speaker 2: folder at the root.
- 8:52Speaker 1: Level components folder, got it.
- 8:53Speaker 2: And then you use the rail's plugin generator to create
- 8:56Speaker 2: your first component inside that folder. So something like rails
- 8:59Speaker 2: plug in new zapp component.
- 9:01Speaker 1: Full mountable, full manable okay.
- 9:03Speaker 2: Stefan in the book uses a sports ball app example
- 9:06Speaker 2: for this, managing teams and games, simple enough to show
- 9:10Speaker 2: the concepts clearly.
- 9:11Speaker 1: Okay, a sports app. But wait, that name app component
- 9:14Speaker 1: that doesn't exactly scream its intent, does it? Yeah, seems
- 9:17Speaker 1: a bit generica. Is that just a placeholder?
- 9:19Speaker 2: Uh huh? Yeah, that's a sharp observation. And the book
- 9:21Speaker 2: actually uses app component intentionally at first?
- 9:25Speaker 1: Really why?
- 9:26Speaker 2: Precisely because it's ambiguous. It gives you a place to
- 9:30Speaker 2: essentially dump an entire existing application or the initial core
- 9:34Speaker 2: web stuff into one component as a starting point. You
- 9:38Speaker 2: don't get bogged down trying to find the perfect name
- 9:40Speaker 2: right at the beginning.
- 9:41Speaker 1: Oh okay, so it's a pragmatic first step exactly.
- 9:43Speaker 2: You get the structure in place. Later you refactor that
- 9:46Speaker 2: app component into multiple well named components like maybe Wabi
- 9:50Speaker 2: teams admin, games admin, once its true responsibilities become clearer.
- 9:55Speaker 2: It's a starting point, not the end goal.
- 9:57Speaker 1: Gotcha. Now. An interesting point the book brings up here
- 10:00Speaker 1: is about fundamental rails, things like database migrations. How do
- 10:04Speaker 1: those work when your app is split into these mini
- 10:07Speaker 1: applications these engines. Seems like that could get.
- 10:10Speaker 2: Messy yeah, that is a common pitfall. It catches people
- 10:13Speaker 2: out while running KEDB dot migrate might work fine inside
- 10:17Speaker 2: the component's dummy app during development, right, it won't automatically
- 10:20Speaker 2: work for the main host application initially. It's like trying
- 10:24Speaker 2: to flush a toilet in one office suite and realizing
- 10:27Speaker 2: it's on a completely separate plumbing system from the main building.
- 10:30Speaker 1: Okay, so how do you connect the plumbing.
- 10:32Speaker 2: The solution is pretty straightforward. You include a specific unctualizer
- 10:36Speaker 2: in your components engine dot RB file, something like initializer
- 10:40Speaker 2: dot ap pend migrations and the engine can fig yep,
- 10:43Speaker 2: and that tells the main application. Hey, look in my
- 10:46Speaker 2: dB migratefolder too. When you run migrations, it automatically appends
- 10:49Speaker 2: the engines migrations to the main apps list, no need
- 10:52Speaker 2: to manually copy them over, which is error prone.
- 10:55Speaker 1: Ah okay, that's clean.
- 10:57Speaker 2: It is, but you do have to be a bit careful.
- 11:00Speaker 2: The source explicitly points out not to chain commands like
- 11:03Speaker 2: RAKEDB dot drop, dB dot create, dB dot migrate when
- 11:07Speaker 2: migrations are loaded this way, as it can sometimes cause
- 11:09Speaker 2: issues depending on the order things load. It's about that
- 11:12Speaker 2: explicit configuration again.
- 11:14Speaker 1: Right, always subtleties. Okay, so dependencies. They can get messy
- 11:18Speaker 1: in any project, right. So once these components are defined
- 11:21Speaker 1: and they can find each other's migrations, the next hurdle
- 11:24Speaker 1: is making sure they actually work reliably together, which brings
- 11:27Speaker 1: us to how CBO manages dependencies between components and then testing.
- 11:31Speaker 2: And deployment exactly so dependencies. Components declare their external dependencies
- 11:36Speaker 2: like any other GEM in their dot gemspeck file standard stuff. Okay,
- 11:40Speaker 2: for internal components, the ones living in your component's directory,
- 11:43Speaker 2: you use path blocks in the main applications gem file.
- 11:47Speaker 1: So in the main gem file you'd have something like
- 11:48Speaker 1: gem teams admin path Components team.
- 11:52Speaker 2: Sadman precisely that tells Bundler where to find that local component.
- 11:56Speaker 2: The book shows examples integrating things like the slim templating
- 11:59Speaker 2: gem or even a specific true skill rating calculation library.
- 12:03Speaker 2: This way within components makes sense.
- 12:06Speaker 1: What about versioning, though, especially for those external gems that
- 12:09Speaker 1: components rely on. Do you let Bundler resolve them loosely
- 12:12Speaker 1: or do you lock them down? This causes so many
- 12:14Speaker 1: headaches and monoliths.
- 12:16Speaker 2: Yeah, this is absolutely critical. The strong recommendation from the
- 12:19Speaker 2: book is to lock down all run time dependencies and
- 12:23Speaker 2: each component's jumps back to exact versions.
- 12:26Speaker 1: Exact versions like gems library one point two.
- 12:29Speaker 2: Point three exactly like that the reason to avoid what
- 12:32Speaker 2: the book calls untested dependencies in production.
- 12:36Speaker 1: Ah right.
- 12:37Speaker 2: If your component was tested against version one point zho
- 12:40Speaker 2: of a GEM, but the main apps gemfile dot lock
- 12:43Speaker 2: somehow pulls in one point one for production, you've got
- 12:46Speaker 2: a dangerous gap. You're running code you haven't explicitly tested
- 12:49Speaker 2: in that component's context.
- 12:50Speaker 1: Okay, that makes a lot of sense, eliminate surprises. What
- 12:53Speaker 1: about development dependencies, Like if you're pointing to a specific
- 12:56Speaker 1: get commit of a GEM.
- 12:57Speaker 2: Good point for those, like maybe a specific admit of
- 13:00Speaker 2: that true skill library. You might actually need to duplicate
- 13:03Speaker 2: that get reference in the main app stem file as well.
- 13:06Speaker 2: Bundler sometimes ignores those specific refs in a dependencies gemspeck.
- 13:10Speaker 2: It's a bit of a workaround, but it ensures consistency.
- 13:13Speaker 1: Okay, so be explicit lock run time versions. Got it,
- 13:16Speaker 1: So you've built your components handled dependencies. Now how do
- 13:21Speaker 1: you actually test this structure? And then how do you
- 13:25Speaker 1: get it deployed? Does it need a whole new DEICD.
- 13:27Speaker 2: Pipeline testing is generally done per component. You navigate into
- 13:31Speaker 2: each component's directory and run its test suite, usually our
- 13:34Speaker 2: spec or minute.
- 13:35Speaker 1: Test so isolated tests yep.
- 13:38Speaker 2: The book covers setting up URSPEC for model specs, controller specs,
- 13:42Speaker 2: feature spex within an engine. One key tip, especially for
- 13:45Speaker 2: controller specs inside an engine, is you need to explicitly
- 13:49Speaker 2: tell arspec to use the engine's routes.
- 13:51Speaker 1: How do you do that?
- 13:51Speaker 2: You to add something like routes my component dot engine
- 13:54Speaker 2: dot routes inside your controller spec described block.
- 13:56Speaker 1: Okay, tells urspec which you are all helpers.
- 13:58Speaker 2: To use exactly. And then for the overall application build
- 14:01Speaker 2: you typically just have a simple shell script maybe build
- 14:04Speaker 2: dot esh that goes into each component directory, runs its tests,
- 14:08Speaker 2: and maybe runs some integration tests in the main app.
- 14:10Speaker 1: Right, a simple script orchestrating the component tests. What about
- 14:13Speaker 1: CI and deploying the platforms like Heroku or say Pivotal
- 14:16Speaker 1: Web Services.
- 14:17Speaker 2: For the most part, CBRI apps deploy like standard rails apps.
- 14:22Speaker 2: But there's a catch that non standard folder structure. Right
- 14:26Speaker 2: with the main app maybe nested in web container and
- 14:28Speaker 2: components and a separate components folder.
- 14:30Speaker 1: Yeah, build packs usually expect the rails app at the
- 14:32Speaker 1: root exactly.
- 14:33Speaker 2: So the common solution is to create a deploy artifact.
- 14:37Speaker 1: A deploy artifact.
- 14:38Speaker 2: Yeah, basically a script maybe prepare deploydirectory dotsh that copies
- 14:43Speaker 2: the necessary parts of the main app the bundled components
- 14:47Speaker 2: into a temporary directory that does have the standard structure
- 14:49Speaker 2: build packs expect.
- 14:51Speaker 1: You create a staging area with the right layout just
- 14:53Speaker 1: for deployment.
- 14:54Speaker 2: Precisely for Heroku. The book even mentions you might use
- 14:58Speaker 2: their platform API to up fload that generated artifact as
- 15:01Speaker 2: a tarball directly bypassing the standard get push hero kumine.
- 15:05Speaker 2: If your repos structure doesn't match what the build pack needs.
- 15:08Speaker 1: Clever okay, And once it's deployed, inevitably things need to change.
- 15:12Speaker 1: What about switching databases later or the big one upgrading rails.
- 15:17Speaker 1: How does CBRA help with those typically painful big lift moments.
- 15:20Speaker 2: Switching databases, say from school light to postcress School is
- 15:24Speaker 2: pretty straightforward for the main app, just like normal. But
- 15:27Speaker 2: an interesting subtle point the book makes is about achieving
- 15:30Speaker 2: dev test prod parity for.
- 15:32Speaker 1: Components parody for components, Yeah.
- 15:34Speaker 2: Setting up different database schemas for the main app and
- 15:37Speaker 2: each component like Sportsball development for the main app and
- 15:40Speaker 2: sports Ball Team sad Mind development for the team's component
- 15:44Speaker 2: actually improves testing and isolation. You ensure components aren't accidentally
- 15:49Speaker 2: bleeding into each other via the database.
- 15:52Speaker 1: Better isolation through separate schemas okay, and.
- 15:55Speaker 2: Rails upgrades still requires care. Obviously, you need to resolve
- 15:58Speaker 2: dependencies often update other gems along with Rails, but the
- 16:02Speaker 2: component structure helps. The book suggests thinking of major Rails
- 16:05Speaker 2: upgrades as long running dependency updates. You might create a
- 16:09Speaker 2: separate branch or build path upgrade components one by one,
- 16:13Speaker 2: ensuring their tests pass against the new Rails version before
- 16:16Speaker 2: merging it all back together. It gives you more granular
- 16:18Speaker 2: control over that big transition.
- 16:20Speaker 1: Breaks down the giant task. Okay, now let's shift gears
- 16:24Speaker 1: a bit. What if you're not starting fresh. What if
- 16:27Speaker 1: you're looking at an existing ball of mud, a legacy
- 16:30Speaker 1: Rails app, and you want to refactor it into components.
- 16:33Speaker 1: How do you even begin that process? Is it a
- 16:36Speaker 1: rip and replace or can you do it gradually?
- 16:39Speaker 2: That's the million dollar question for many teams, isn't it?
- 16:41Speaker 2: And the book brings in Robert Martin's idea here from
- 16:44Speaker 2: his talk Architecture the Lost Years, He argues that standard
- 16:47Speaker 2: rails apps often scream their framework, not their domain Intentscrean's.
- 16:52Speaker 1: The framework, meaning they look like rails apps, but you
- 16:55Speaker 1: can't easily tell what they do.
- 16:56Speaker 2: Kind of. Yeah. The web delivery mechanism controllers views is
- 17:00Speaker 2: front and center, but the core business logic, the actual
- 17:04Speaker 2: domain might be hidden or tangled up inside.
- 17:07Speaker 1: Okay, so how do we start changing that perception and
- 17:09Speaker 1: the structure itself, especially if we're starting from that big
- 17:12Speaker 1: ball of mud.
- 17:13Speaker 2: One really powerful, almost symbolic first step the book suggests
- 17:17Speaker 2: is to shift the main Rails application code like app
- 17:20Speaker 2: canfig gem file into a subfolder maybe call it web container.
- 17:24Speaker 1: Move the rails app itself.
- 17:26Speaker 2: Yeah, and then you elevate the components folder, even if
- 17:29Speaker 2: it's initially empty, to the root level of the project.
- 17:32Speaker 1: Oh I see that immediately changes the visual hierarchy exactly.
- 17:35Speaker 2: It signals visually that the components are the primary organizational
- 17:39Speaker 2: units and the Rails app is just one part the
- 17:42Speaker 2: delivery mechanism. It reframes how you think about the codebase.
- 17:46Speaker 1: Cool psychological trick. Okay, So after that, how do you
- 17:49Speaker 1: actually start extracting pieces.
- 17:51Speaker 2: Then you've basically got two main approaches. For the refactoring itself.
- 17:55Speaker 2: You can do bottom up extraction. Bottom up, yeah, you
- 17:58Speaker 2: analyze the existing code looking for cohesive chunks, maybe groups
- 18:02Speaker 2: of models that always work together, specific controllers and views
- 18:05Speaker 2: for one feature, files within a certain subfolder, or classes
- 18:09Speaker 2: sharing a common inheritance. You find these self contained parts
- 18:12Speaker 2: and pull them out into a component. The book uses
- 18:15Speaker 2: extracting a predictor domain gem as an example of this.
- 18:18Speaker 1: Okay, finding natural clusters in the existing code. What's the
- 18:21Speaker 1: other way?
- 18:22Speaker 2: The other way is pop down extraction. Here you start
- 18:25Speaker 2: not by looking at the code structure, but by thinking
- 18:28Speaker 2: about the application's domain or its users.
- 18:30Speaker 1: Domain driven kind of exactly.
- 18:33Speaker 2: You identify logical boundaries based on your understanding of the business. Okay,
- 18:37Speaker 2: this whole section deals with managing teams or maybe based
- 18:39Speaker 2: on user roles. This is the team's admin interface. This
- 18:43Speaker 2: is the game's admin stuff. This is the public prediction UI.
- 18:46Speaker 2: You define the component boundaries conceptually first, then pull the
- 18:50Speaker 2: relevant code into.
- 18:51Speaker 1: Them right top down, based on domain, bottom up, based
- 18:54Speaker 1: on code cohesion. What are some common roadblocks or challenges
- 18:59Speaker 1: people hit when they are doing this refactoring? It sounds
- 19:01Speaker 1: like you could easily break things or introduce new problems.
- 19:04Speaker 2: Oh definitely. One common one, especially as you pull out
- 19:07Speaker 2: UI related components, is running into what feel like seemingly
- 19:11Speaker 2: necessary circular dependencies.
- 19:13Speaker 1: Ah, the dreaded cycles again, like.
- 19:15Speaker 2: What global navigation is a classic example. Your main application
- 19:18Speaker 2: layout needs to generate links to paths defined inside your
- 19:21Speaker 2: newly extracted games admin component. Right yeah, but that games
- 19:25Speaker 2: admin component might also need access to something from the
- 19:28Speaker 2: main app or another component, creating a cycle.
- 19:30Speaker 1: So how do you break that cycle?
- 19:32Speaker 2: The book shows a couple ways. You could crudely hardcode
- 19:36Speaker 2: the paths in the main layout. Not ideal. Now, A
- 19:40Speaker 2: more elegant solution is to create an explicit contract. The
- 19:44Speaker 2: game's admin component itself provides its necessary navigation entry point.
- 19:49Speaker 2: Maybe it has a class method like self dot naventry
- 19:51Speaker 2: that returns the path and label. The main layout just
- 19:54Speaker 2: asks each UI component for its entry.
- 19:57Speaker 1: Ah and version of control. Basically, yeah, the component tells
- 20:00Speaker 1: the layout how to link to it precisely.
- 20:02Speaker 2: Another big challenge, of course, is just keeping the tests passing.
- 20:05Speaker 2: At each step, you make a small extraction, run the tests,
- 20:09Speaker 2: make another run the tests. Ideally, the book notes you
- 20:12Speaker 2: get separation of concerns reflected in broken tests, meaning when
- 20:15Speaker 2: you extract a component, ideally only tests directly related to
- 20:19Speaker 2: that component or its immediate dependence should break. If everything breaks,
- 20:23Speaker 2: your extraction probably wasn't clean enough.
- 20:26Speaker 1: Right, tests become your guide. The book uses the phrase
- 20:28Speaker 1: slippery slope when talking about extraction. Is that mends a
- 20:32Speaker 1: warning or ah huh?
- 20:33Speaker 2: No, it's actually framed as a slippery slope of.
- 20:34Speaker 1: A good kind, the good slippery slope.
- 20:36Speaker 2: Yeah. Stephen Wisely says it never hurts to extract too much.
- 20:41Speaker 2: It will always teach something, and it is always reversible.
- 20:44Speaker 2: The idea is, once you extract one component, it often
- 20:47Speaker 2: makes the next potential extraction much clearer. You start sliding
- 20:50Speaker 2: down this path of increasing modularity, and that's a good thing.
- 20:54Speaker 1: Okay, don't be afraid to extract. You can always put
- 20:56Speaker 1: it back. What about renaming components? We talked about starting
- 20:59Speaker 1: with app come How important is getting the names right?
- 21:02Speaker 1: Eventually seems like just changing names?
- 21:04Speaker 2: Oh, it's huge. The book calls renaming arguably the most
- 21:07Speaker 2: important refactor of them all.
- 21:08Speaker 1: Really most important.
- 21:10Speaker 2: Yeah, aligning a component's name, changing that vague app component
- 21:15Speaker 2: to a specific WebUI or reporting engine with its actual
- 21:18Speaker 2: discovered function is absolutely paramount. It's key to maintaining that
- 21:23Speaker 2: clarity and ensuring the architecture keeps screaming its intent.
- 21:26Speaker 1: So the name is the communication exactly.
- 21:28Speaker 2: It involves mechanical work sure, renaming folders, files, class names,
- 21:33Speaker 2: module references, but it's critical and luckily there are tools
- 21:36Speaker 2: like the Kleebray tools Jim mentioned in the book that
- 21:39Speaker 2: can help automate a lot of that grutwork.
- 21:41Speaker 1: Right tooling helps. Are there other common refactoring patterns besides
- 21:45Speaker 1: just extracting and renaming, Yeah.
- 21:47Speaker 2: The book mentions a few others. Splitting a component if
- 21:50Speaker 2: you realize it's actually doing two unrelated things. Creating dedicated
- 21:54Speaker 2: API components if you need separate back ends for a
- 21:57Speaker 2: WebUI and a public API. Creating third party service.
- 22:01Speaker 1: Adapters Adapters like wrapping external gems exactly.
- 22:05Speaker 2: Say you use brain tree for payments. Instead of scattering
- 22:08Speaker 2: brain Tree calls all over your app, you create a
- 22:10Speaker 2: brain Tree adapter component. It wraps the brain Tree gem,
- 22:14Speaker 2: exposes only the specific functions your app needs, and becomes
- 22:18Speaker 2: the single point of interaction. This reduces coupling to the
- 22:21Speaker 2: third party service and makes it easier to swap out
- 22:23Speaker 2: later if needed.
- 22:24Speaker 1: Smart isolating external dependencies.
- 22:26Speaker 2: And also creating common functionality components, maybe pulling out something
- 22:30Speaker 2: like a soft deletable concern or shared UI elements into
- 22:33Speaker 2: their own, small, reusable component.
- 22:35Speaker 1: Okay, so for those really big, scary, leazy balls of mud, Yeah,
- 22:39Speaker 1: is it always best to take these small incremental extraction
- 22:42Speaker 1: steps or is there ever a case for doing something
- 22:45Speaker 1: more drastic?
- 22:46Speaker 2: Interestingly, yes, the book suggests that sometimes the best approach
- 22:50Speaker 2: is to take one big step.
- 22:52Speaker 1: One big step sounds risky, it.
- 22:55Speaker 2: Sounds kind of crazy, right. The idea is you move
- 22:57Speaker 2: almost all the existing applications core a lot. Often most
- 23:01Speaker 2: models services anything tightly coupled into one single massive component
- 23:07Speaker 2: right at the start.
- 23:07Speaker 1: Wait, so you're just moving the entire mess into a
- 23:11Speaker 1: component folder. How does that help? Isn't that just creating
- 23:14Speaker 1: a worse application?
- 23:15Speaker 2: Like the book says, it feels like you're making it worse. Initially,
- 23:18Speaker 2: you start with one ball of mud, and now you
- 23:20Speaker 2: have maybe the thin rail shell and one slightly smaller
- 23:23Speaker 2: but still messy ball of mud inside a component. But
- 23:26Speaker 2: there's a real method to this madness. By doing that
- 23:29Speaker 2: big move, you immediately expose all that legacy code to
- 23:32Speaker 2: all the component based refactoring techniques we just discussed.
- 23:36Speaker 1: Because now it's inside a component.
- 23:37Speaker 2: Boundary exactly, you can now start applying those bottom up,
- 23:41Speaker 2: top down extractions, creating impactors splitting things within that big
- 23:45Speaker 2: component much more easily than when it was all tangled
- 23:48Speaker 2: up in the main rails app structure. It allows for
- 23:50Speaker 2: a much broader transformation to happen much more quickly than
- 23:54Speaker 2: trying to chip away with tiny extractions from the outside.
- 23:57Speaker 2: You instantly separate the framework concerns from the application core,
- 24:01Speaker 2: even if that core is still messy. It's a powerful kickstart.
- 24:05Speaker 1: Wow. Okay, that's counterintuitive but makes sense. You move the
- 24:09Speaker 1: mess to make it possible to clean the mess using
- 24:11Speaker 1: the new tools. Okay, So how does this CBRA approach
- 24:15Speaker 1: relate to other architectural patterns people might already be familiar with.
- 24:19Speaker 1: Does it replace things like say hexagonal architecture or domain
- 24:22Speaker 1: driven design or does it work with them?
- 24:24Speaker 2: That's a great question. It absolutely complements them, often by
- 24:27Speaker 2: adding a layer of rigor and enforcement. So with exxagle,
- 24:30Speaker 2: for instance, well hexagonal architecture or reports and adapters, it's
- 24:34Speaker 2: all about separating your core business logic from external concerns.
- 24:37Speaker 2: The database, the UI, third party APIs.
- 24:40Speaker 1: Right the core versus the shell.
- 24:42Speaker 2: CBRA helps enforce that separation by putting your core domain
- 24:46Speaker 2: logic into components with explicit dependencies, and your adapters like
- 24:50Speaker 2: UY or database access into other components. CBRA makes those
- 24:54Speaker 2: boundaries concrete and helps ensure the dependencies flow in the
- 24:57Speaker 2: right direction. The shell depends on the core or not
- 25:00Speaker 2: the other way around. The book shows how CBRA enforces
- 25:04Speaker 2: that directionality of the dependence.
- 25:06Speaker 1: Okay, so it makes the hexagonal boundaries physically real in
- 25:10Speaker 1: the codebase. What about something like DCI Data Context Interaction.
- 25:15Speaker 2: Yeah, DCI is more focused on modeling object interactions dynamically,
- 25:19Speaker 2: how objects play different roles within specific context to carry
- 25:22Speaker 2: out interactions or use cases. CBIRA can help here by
- 25:25Speaker 2: providing a place to actually put those DCI constructs. You
- 25:28Speaker 2: could potentially have components representing specific context or containing the
- 25:32Speaker 2: role definitions relevant to a certain part of the domain. Again,
- 25:35Speaker 2: it helps make those potentially abstract DCI concepts first level
- 25:38Speaker 2: citizens in the codebase with explicit boundaries and dependencies between them.
- 25:42Speaker 1: It at structure, so it provides the structural backbone for
- 25:46Speaker 1: these other patterns. Cool. Now, let's zoom out. If you're
- 25:49Speaker 1: listening to this and you're not a rails developer, maybe
- 25:52Speaker 1: you're building apps with Python and Django, or Java with
- 25:54Speaker 1: Spring or dot Net. Is this deep dive still relevant?
- 26:00Speaker 1: Where is CDRA really just a Rails thing?
- 26:02Speaker 2: No? Absolutely relevant. This is one of the most compelling
- 26:04Speaker 2: parts of the book. Actually, Stefan explicitly shows how this
- 26:09Speaker 2: component based development philosophy translates beautifully to other languages and frameworks.
- 26:13Speaker 1: Oh really, He shows examples.
- 26:14Speaker 2: Yeah, there are concrete examples showing project structures in Cottland,
- 26:18Speaker 2: Java using gradal and in dot nettischep that look remarkably
- 26:22Speaker 2: similar to the rail's CBRE structure.
- 26:24Speaker 1: Wow.
- 26:25Speaker 2: What's fascinating, as he points out, is that you look
- 26:28Speaker 2: not for language syntax, but for application structure, the way
- 26:31Speaker 2: dependencies are managed, the way modules or packages are organized,
- 26:35Speaker 2: the separation of concerns. The patterns are incredibly consistent across
- 26:39Speaker 2: these different textacs.
- 26:40Speaker 1: So the underlying principles are universal totally.
- 26:43Speaker 2: The core ideas of clear responsibility, explicit non cyclic dependencies,
- 26:49Speaker 2: managing complexity through modularity. These aren't Rail specific problems or
- 26:55Speaker 2: solutions they apply pretty much everywhere you build large software system.
- 27:00Speaker 2: The problems of complexity and maintainability are shared, and componentization
- 27:05Speaker 2: offers a consistent, powerful way to tackle them, regardless of
- 27:08Speaker 2: your specific tools.
- 27:09Speaker 1: That's really encouraging. So this deep dive has definitely shown
- 27:12Speaker 1: us that component based rails or component based architecture in
- 27:16Speaker 1: general isn't just some technical trick. It's really more of
- 27:18Speaker 1: a philosophy. Isn't it a way to manage complexity and
- 27:21Speaker 1: large projects?
- 27:22Speaker 2: Hidixel?
- 27:23Speaker 1: Yeah, turning that potential ball of mud into something well structured, understandable,
- 27:28Speaker 1: and crucially maintainable over the long haul. It's about regaining
- 27:31Speaker 1: control and clarity.
- 27:33Speaker 2: Indeed, and as a saying goes, knowledge is most valuable
- 27:36Speaker 2: when it's understood and actually applied. By consciously breaking down
- 27:39Speaker 2: your application into these explicit, well defined components, you really
- 27:43Speaker 2: do gain a level of control and clarity that pays
- 27:45Speaker 2: huge dividends down the road. It makes your systems more resilient,
- 27:48Speaker 2: easier to reason about, easier to evolve.
- 27:50Speaker 1: So the provocative thought for you listening, what does this
- 27:52Speaker 1: all mean for your projects? The next time you encounter
- 27:56Speaker 1: a sprawling code based maybe when you're working on right now,
- 27:59Speaker 1: instead of just feeling overwhelmed, ask yourself this, How much
- 28:02Speaker 1: clearer would its intent be if it truly screamed its domain,
- 28:07Speaker 1: not just its framework. Could components help reveal that core purpose?
- 28:12Speaker 2: Yeah, it's a powerful question to reflect on, and obviously
- 28:16Speaker 2: this deep dive is just scratching the surface. We really
- 28:18Speaker 2: encourage you to check out Stefan Hageman's book and explore
- 28:21Speaker 2: how these ideas might transform the way you build and
- 28:24Speaker 2: maintain your own applications. There's a lot more detail and
- 28:27Speaker 2: practical advice in there.