HerokuJason DraperExplainer
Static Typing in Ruby with a Side of Sorbet
Summary
An experiment introduced Sorbet into a greenfield Ruby codebase with four developers to test whether static typing could catch bugs earlier, ease refactoring, and improve code design. Sorbet combines static linting with runtime checks that verify method parameters and return values against signatures, while RBI files describe methods from gems and Rails models. Setup required sorbet init, running sorbet tc, and adding that command to CI; however, Rails metaprogramming required model signatures, and sorbet-rails could miss or mistype fields. Sorbet immediately exposed issues including repeated method calls, unintended nullable database columns, and unnecessary explicit returns or guard clauses. The team reports a very positive short-term experience, while noting that the project’s young age caused most of the bugs encountered and that long-term value remains uncertain.
Context
The experiment evaluated whether adding Sorbet’s static type checking to a Ruby codebase could catch bugs before production, make refactoring easier, and improve code design for a four-developer team.
Approach / What changed
The team added Sorbet to a greenfield Ruby codebase, initialized its configuration with sorbet init, ran sorbet tc locally and in CI, and used method signatures and RBI files for application, gem, and Rails model code. They also used sorbet-rails to generate model RBI files.
Takeaways
Sorbet uses runtime checks to verify method parameters and return values against declared signatures, in addition to static linting.
Sorbet exposed nullable database columns that conflicted with the team’s preference to avoid nils, prompting a database migration.
Rails metaprogramming complicates static typing; sorbet-rails helped generate RBI files but still missed or mistyped some fields.
Source and freshness
- Source
- heroku.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.