Overview
The article discusses the challenges and advancements in adopting static typing in Ruby at Shopify, particularly through the use of Sorbet. It highlights the benefits of static typing in improving code safety and developer experience while addressing specific issues encountered during implementation.
What You'll Learn
1
How to adopt Sorbet for static typing in Ruby applications
2
Why static typing can enhance code safety and developer experience
3
When to consider using Sorbet in a Rails project
4
How to handle complex type definitions in Sorbet
Prerequisites & Requirements
- Familiarity with Ruby programming language
- Understanding of dynamic typing concepts(optional)
- Basic knowledge of using Sorbet in Ruby projects(optional)
Key Questions Answered
What challenges does Shopify face when adopting Sorbet?
Shopify encounters challenges primarily with modules using ActiveSupport::Concern and those that assume inclusion in specific classes. For instance, modules that call methods like before_save without clear type definitions can create ambiguity for Sorbet. The team aims to clarify these dependencies in future implementations.
How does Sorbet compare to TypeScript for Ruby developers?
While both Sorbet and TypeScript offer static typing benefits, Ruby's dynamic nature and Sorbet's relative youth mean that Sorbet's feature coverage is less extensive than TypeScript's. However, Sorbet allows inline type annotations and does not require compilation, offering a more seamless integration for Ruby developers.
What are the first steps to start typing in a Rails project?
To begin typing in a Rails project, developers should follow the steps outlined on the Sorbet website. They can utilize sorbet-rails for generating Rails RBI files or tapioca for gem RBIs, starting with files marked as typed: false before gradually moving to typed: true.
In what scenarios should Sorbet not be used?
Sorbet may be counterproductive in environments lacking team buy-in or in codebases heavily reliant on metaprogramming. In such cases, the effort to implement Sorbet may not yield significant benefits, although some advantages can still be gained by running Sorbet in a typed: false mode.
Key Statistics & Figures
Number of Ruby files in Shopify's monolith
37,000
This large codebase presents challenges for ensuring code stability and safety.
Number of commits merged daily to the main branch
400
Frequent changes necessitate robust testing and type checking to maintain application reliability.
Number of automated tests at Shopify
150,000
These tests help ensure the stability of the monolith despite the challenges of dynamic typing.
Number of deployments per day
40
Regular deployments require a reliable development process to support ongoing changes.
Technologies & Tools
Type Checker
Sorbet
Used for adding static typing to Ruby applications at Shopify.
Rbi Generator
Tapioca
Generates RBI files for Ruby gems to facilitate type checking.
Library
Activesupport
Provides various utilities for Ruby, including concerns that pose challenges for type checking.
Key Actionable Insights
1Start implementing Sorbet gradually by targeting files with the most errors or high reuse rates.Focusing on files that generate frequent errors or are widely used can maximize the benefits of static typing, improving collaboration and reducing bugs in critical areas of the codebase.
2Utilize tapioca to generate RBI files efficiently for your Ruby gems.By automating the generation of RBI files, developers can save time and ensure that type definitions are consistently applied across the codebase, enhancing type safety.
3Consider the implications of dynamic typing when adopting Sorbet, especially with ActiveSupport modules.Understanding the limitations of Sorbet in handling dynamic features of Ruby can help developers plan their type annotations more effectively, avoiding common pitfalls.
Common Pitfalls
1
Assuming that Sorbet can handle all dynamic Ruby features without adjustments.
Many Ruby features, especially those involving metaprogramming or dynamic method definitions, can create challenges for Sorbet. Developers should be prepared to explicitly define types and dependencies to avoid confusion.
Related Concepts
Static Typing
Dynamic Typing
Type Safety
Ruby On Rails