Presenting how Shopify got from no Sorbet in our monolith to almost full coverage in the span of a few months. I’ll explain the challenges we faced, the tools we built to solve them.
Overview
The article discusses Shopify's experience in adopting Sorbet, a static type checker for Ruby, at scale. It highlights the challenges faced, tools developed, and the positive impact on code stability and developer experience.
What You'll Learn
1
How to implement Sorbet in a Ruby monolith to improve type safety
2
Why using RuboCop Sorbet can help enforce Sorbet compatibility in Ruby code
3
How to generate Ruby Interface files (RBI) for gems using Tapioca
4
When to use typed: false and typed: true in your Ruby files
Prerequisites & Requirements
- Understanding of Ruby programming and static typing concepts
- Familiarity with RuboCop and Sorbet(optional)
Key Questions Answered
What challenges did Shopify face when adopting Sorbet?
Shopify faced challenges such as Sorbet's inability to understand certain Ruby constructs and the need to adapt existing code to be compatible with Sorbet. They developed tools like RuboCop Sorbet and Tapioca to address these issues and facilitate the adoption process.
How does Sorbet improve code stability at Shopify?
Sorbet improves code stability by enforcing type checks during continuous integration, which helps catch type-related errors before they reach production. As a result, 80% of their files are typed true or higher, significantly reducing production errors.
What is the purpose of Tapioca in the Sorbet adoption process?
Tapioca is used to generate Ruby Interface files (RBI) for gems, allowing Sorbet to understand the types defined in those gems. This tool automates the generation of RBI files, ensuring high accuracy and better integration of external libraries with Sorbet.
What benefits have been realized from using Sorbet at Shopify?
Shopify has realized benefits such as a decrease in NameErrors and NoMethodErrors in production after enabling Sorbet. The team observed that enabling Sorbet eradicates errors due to developer mistakes, enhancing overall code quality.
Key Statistics & Figures
Percentage of files typed true or higher
80%
As of the article's publication, this percentage reflects the level of type coverage achieved in Shopify's main monolith.
Number of Ruby files in Shopify's codebase
37,000
This statistic illustrates the scale of Shopify's Ruby codebase and the challenges faced in adopting static typing.
Number of commits merged daily
400
This high volume of daily commits highlights the rapid pace of development at Shopify, necessitating robust type checking.
Number of methods in Shopify's codebase
622,000
This figure underscores the complexity of the codebase and the importance of effective static typing to manage it.
Technologies & Tools
Static Type Checker
Sorbet
Used to enforce static typing in Ruby applications at Shopify.
Linter
Rubocop
Utilized to ensure code adheres to Sorbet's typing requirements and Ruby best practices.
Rbi Generation Tool
Tapioca
Automates the generation of Ruby Interface files for gems to enhance Sorbet's understanding of external libraries.
Key Actionable Insights
1Implementing Sorbet in your Ruby projects can significantly enhance type safety and reduce runtime errors.By enforcing type checks during development, teams can catch errors early, leading to more stable applications and a better developer experience.
2Utilizing tools like RuboCop Sorbet can streamline the process of making existing Ruby code compatible with Sorbet.These tools help identify and correct constructs that Sorbet cannot understand, facilitating a smoother transition to static typing.
3Generating RBI files for external gems using Tapioca is crucial for integrating them into a Sorbet-typed codebase.This automation saves time and ensures that the types from external libraries are accurately represented, improving type coverage.
4Aim for all application files to be at least typed: true to maximize the benefits of Sorbet.This practice helps prevent errors from propagating through the codebase and encourages a culture of type safety among developers.
Common Pitfalls
1
Using typed: ignore can lead to hidden errors in the codebase.
Ignoring files can cause issues in other files that reference them, making it harder for developers to maintain type safety and increasing the risk of runtime errors.
Related Concepts
Static Typing In Ruby
Type Safety Best Practices
Rbi File Generation
Ruby Infrastructure Team Initiatives