Static Typing for Ruby

A look at how Shopify adopted static typing in it's core Ruby monolith by treating type checking like a product.

Alexandre Terrasa
11 min readintermediate
--
View Original

Overview

The article discusses Shopify's journey towards implementing static typing in Ruby using Sorbet, addressing the challenges and requirements of integrating this feature into a large codebase. It highlights the benefits of static typing for developers, including improved error detection and developer satisfaction.

What You'll Learn

1

How to implement static typing in Ruby using Sorbet

2

Why gradual typing is essential for large codebases

3

When to use different levels of strictness in Sorbet

4

How to measure the adoption of static typing in a project

5

Why developer happiness is crucial when adopting new tools

Prerequisites & Requirements

  • Understanding of Ruby programming language
  • Familiarity with Sorbet type checker(optional)

Key Questions Answered

What are the key requirements for a typing solution in Ruby?
The key requirements for a typing solution in Ruby include gradual typing to avoid blocking development, speed to provide quick feedback on errors, and full Ruby support to accommodate all language features and Rails constructs.
How does Shopify track static typing adoption?
Shopify tracks static typing adoption using SorbetMetrics, which collects metrics on the number of files typed, method signatures, and calls to methods with signatures, providing insights into the typing coverage across projects.
What levels of strictness does Sorbet offer for type checking?
Sorbet offers five levels of strictness: ignore, false, true, strict, and strong, each determining how much type checking is enforced, from ignoring the file entirely to requiring all methods to have signatures and all variables to be typed.
What benefits has Shopify observed from adopting Sorbet?
Shopify has observed that Sorbet catches more errors on pull requests as adoption increases, enhances code understanding through signatures, and boosts developer confidence, leading to a positive opinion about static typing in Ruby.

Key Statistics & Figures

Number of Ruby files in Shopify's monolith
37,000
This large codebase presents unique challenges for implementing static typing.
Number of commits merged daily
400
This high volume of changes necessitates a robust typing solution to maintain stability.
Percentage of files typed at least 'typed: false'
80%
This indicates a significant level of adoption of static typing in the core monolith.
Number of internal projects using Sorbet
60
This reflects the growing interest and adoption of Sorbet across Shopify's projects.

Technologies & Tools

Tool
Sorbet
A type checker for Ruby that enables gradual typing and improves code safety.

Key Actionable Insights

1
Implement gradual typing in your Ruby projects to enhance code safety without disrupting development.
Gradual typing allows developers to add types incrementally, which is crucial for maintaining productivity in large codebases like Shopify's monolith.
2
Utilize SorbetMetrics to track the adoption of static typing and identify areas needing improvement.
By measuring typing coverage and strictness levels, teams can prioritize their efforts and ensure that all critical parts of the codebase are properly typed.
3
Foster a supportive environment for developers adopting new tools like Sorbet.
Providing resources and a platform for knowledge sharing can significantly improve developer experience and satisfaction, as seen with Shopify's transition to Sorbet.
4
Regularly survey developers to gauge their satisfaction with static typing tools.
Understanding developer sentiment can guide future improvements and ensure that the benefits of static typing outweigh any inconveniences.

Common Pitfalls

1
Failing to provide adequate support for developers during the transition to static typing can lead to frustration and decreased productivity.
Without proper guidance and resources, developers may struggle with new tools, resulting in resistance to adoption.
2
Overly strict typing can hinder development speed and flexibility.
It's important to balance the strictness of type checking with the need for rapid development, especially in a fast-paced environment like Shopify.

Related Concepts

Static Typing In Dynamic Languages
Gradual Typing Methodologies
Developer Experience And Satisfaction
Type Checking Tools And Their Impact On Code Quality