Building on Software design basics, this lesson covers more principles and code smells, technical debt, and architecture antipatterns, using real repos to find examples.
Important
Work on this lesson in
advisormode.
Learning goals
- You can explain what a code smell is and recognize the common ones in Ruby code, e.g. feature envy, objects reaching deep into other objects, classes with several responsibilities.
- You can apply the design principles you know — single responsibility, Law of Demeter, Tell Don't Ask — to real code and propose a concrete refactoring.
- You can explain what technical debt and technical drift are, how they arise, and how a team keeps them in check.
- You can find debt and drift in a real repository and judge whether fixing it is worth the cost.
- You can recognize code that ignores module boundaries — logic for one concept scattered across models, controllers, views and helpers — and pull it back into one place. This is the most common flaw in agent-generated code.
Resources
Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.
Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.
Code smells and refactoring
- ▶️ All the Little Things Show archive.org snapshot — Sandi Metz, RailsConf 2014; turning a knot of conditionals into small objects, step by step
- 📘 Ruby Science Show archive.org snapshot — thoughtbot, free PDF; a catalog of Rails code smells, each with the refactoring that fixes it
- 📄 Code smells Show archive.org snapshot and the refactoring catalog Show archive.org snapshot — the vocabulary: feature envy, long parameter lists, inappropriate intimacy, and the moves that address them
- 📄 Tell, Don't Ask Show archive.org snapshot — refresher
- 📄 SOLID in Ruby: Single Responsibility Principle Show archive.org snapshot
Module boundaries
- 📄
Rails with protected modules
Show archive.org snapshot
— namespaces with a small public API and
private_constant: how to keep one concept in one place - 📘 Practical Object-Oriented Design in Ruby (Sandi Metz, in our library) — chapter 3 "Managing Dependencies" and chapter 4 "Creating Flexible Interfaces"
- 📄 Architecture Antipatterns Show archive.org snapshot — a catalog of structural mistakes that show up in real applications; skim the patterns and think about which ones you have seen
Technical debt
- 📄 Technical debt Show archive.org snapshot — Martin Fowler's definition and the quadrant
- 📄 Are you experiencing technical drift? Show archive.org snapshot — debt that accrues without anyone deciding to take it on
- 📄 Moving fast with high code quality Show archive.org snapshot — how a team keeps debt in check
Exercises
Find the debt
Look at the repo for Cards and find two or three examples of technical debt or drift. For each one:
- What pain does it cause today? Which changes does it make slower, harder or riskier?
- What would it cost to fix: how much time, and what else would have to change with it?
- What would we gain? Is it worth fixing?
Find the scattered concept
Take one concept in Cards that is truly scattered — the card search — and list every file that knows about it. Where does logic for that concept live in models, controllers, views and helpers? Sketch where it should live, and what a single module for it would look like.
Second opinion
Your agent has a design review that looks for the problems from this lesson: scattered concepts, mixed responsibilities, coupling, misplaced logic and inconsistency.
- Before you run it, write down the design problems you see in your MovieDB, each with the principle it breaks.
- Run
/trainee-design-reviewover your whole MovieDB. - Compare its findings with yours. Which did you both find? Which did you miss, and which did it miss? Do you agree with each finding and its direction?
- Fix one or two findings you agree with. It's fine if problems remain.
Tip
Keep running
/trainee-design-reviewon your changes after this lesson, e.g. before you ask for a review.