rails-antipattern-premature-abstraction-and-di

Installation
SKILL.md

Antipattern: Premature Abstraction & Dependency Injection

The smell

  • Constructors that take a repo:, clock:, mailer:, gateway: keyword for every collaborator
  • Repository / gateway / port classes wrapping a single Active Record model 1:1
  • Test setup full of instance_double(...) / class_double(...) for things Rails already mocks (mailers, jobs, time)
  • Only one production implementation exists; the "second" implementation is always a test double

Why it hurts

  • Costs are real: extra files, extra wiring, harder onboarding
  • Benefits are imaginary — the second adapter never arrives
  • Tests exercise the seam, not the behavior
  • DHH/Manrubia: Rails is the abstraction layer. Don't build a second one to test it

The fix

  • Use Active Record directly — fixtures + test DB cover the "mocking" need
  • Use Rails test helpers — travel_to, assert_emails, perform_enqueued_jobs, freeze_time
  • Real objects in tests (see mock-heavy-tests)
  • Defer the abstraction until you genuinely have two implementations. YAGNI applies hardest in Rails
Installs
2
First Seen
May 8, 2026
rails-antipattern-premature-abstraction-and-di — gierd-inc/dev-skills