Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
My Problem, My Solution
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Penelope Phippen
September 12, 2014
Technology
64
1
Share
My Problem, My Solution
A talk about typing I gave at Frozen Rails 2014
Penelope Phippen
September 12, 2014
More Decks by Penelope Phippen
See All by Penelope Phippen
Introducing Rubyfmt
penelope_zone
0
610
How RSpec Works
penelope_zone
0
6.8k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
100
Teaching RSpec to play nice with Rails
penelope_zone
2
170
Little machines that eat strings
penelope_zone
1
130
What is processor (brighton ruby edition)
penelope_zone
0
140
What is processor?
penelope_zone
1
380
extremely defensive coding - rubyconf edition
penelope_zone
0
290
Agile, etc.
penelope_zone
2
250
Other Decks in Technology
See All in Technology
プロダクトを触って語って理解する、チーム横断バグバッシュのすすめ / 20260411 Naoki Takahashi
shift_evolve
PRO
1
200
本番環境でPHPコードに触れずに「使われていないコード」を調べるにはどうしたらよいか?
egmc
1
230
OPENLOGI Company Profile for engineer
hr01
1
62k
Babylon.js Japan Activities (2026/4)
limes2018
0
190
サイボウズ 開発本部採用ピッチ / Cybozu Engineer Recruit
cybozuinsideout
PRO
10
77k
解剖"React Native"
hacusk
0
120
AIにより大幅に強化された AWS Transform Customを触ってみる
0air
0
330
ふりかえりがなかった職能横断チームにふりかえりを導入してみて学んだこと 〜チームのふりかえりを「みんなで未来を考える場」にするプロローグ設計〜
masahiro1214shimokawa
0
230
20260410 - CNTUG meetup #72 - DiskImage Builder 介紹:以 Kubespray CI 打造 RockyLinux 10 Cloud Image 為例
tico88612
0
110
"まず試す"ためのDatabricks Apps活用法 / Databricks Apps for Early Experiments and Validation
nttcom
1
210
ADOTで始めるサーバレスアーキテクチャのオブザーバビリティ
alchemy1115
2
260
AWSで2番目にリリースされたサービスについてお話しします(諸説あります)
yama3133
0
130
Featured
See All Featured
The SEO identity crisis: Don't let AI make you average
varn
0
430
Crafting Experiences
bethany
1
110
GitHub's CSS Performance
jonrohan
1032
470k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
160
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
170
WCS-LA-2024
lcolladotor
0
520
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.1k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
170
Music & Morning Musume
bryan
47
7.1k
Darren the Foodie - Storyboard
khoart
PRO
3
3.1k
Transcript
The maybe monad as a replacement for nil
My Problem My Solution
Everyone Stand Up
None
a!/samphippen
My Problem
Type Checking is the Antithesis of Object Oriented Programming
Type
Types
What is a type?
What is a data type?
A type is a set of possible values and operations
Class
Terms are literally interchangeable in Ruby
Terms are literally interchangeable in Ruby Konstantin
Fixnum
✕
+
/
—
1.class # => Fixnum
You know what all these things do
1+1 # => 2
Array
count
each
In Ruby some types are interchangeable
Typeclass
A set of types and common operations
There is some expectation of what the operations will do
Duck typing
All number types in Ruby form a typeclass
Fixnum Float BigDecimal
Numeric Op Numeric = Numeric
Positive Numeric + Positive Numeric = Positive Numeric =
Also collections
Hash Set Array
Type Checking is the Antithesis of Object Oriented Programming
Type Checking is the Antithesis of Object Oriented Programming
Type Checking
This term has two meanings
Compile time type checking
public static final List<string> seriouslyiamsoboredwh ocares
Like in Java
Clearly we don’t do this in Ruby
So what do I mean?
ActiveRecord::Base #find_by
pony = Pony.find_by(:id => smth) pony.neigh
pony = Pony.find_by(:id => smth) if pony pony.neigh else puts
“No Pony can’t neigh” end
The problem here is two return types
nil Pony < AR::Base
We’re forced to add a type check
Also, I think this is the wrong type check
pony = Pony.find_by(:id => smth) if !pony.nil? pony.neigh else puts
“No Pony can’t neigh” end
A more explicit type check
But still wrong
pony = Pony.find_by(:id => smth) if !pony.nil? pony.neigh else puts
“No Pony can’t neigh” end
pony = Pony.find_by(:id => smth) if pony.respond_to?(:neigh) pony.neigh else puts
“No Pony can’t neigh” end
This type checking adds unnecessary complexity to our app
Type Checking is the Antithesis of Object Oriented Programming
Type Checking is the Antithesis of Object Oriented Programming
Antithesis
I am using it to mean “DOING IT WRONG”
Type Checking is the Antithesis of Object Oriented Programming
Type Checking is the Antithesis of Object Oriented Programming
Object oriented programming
Konstantin Haase says:
Data abstraction and control abstraction
Alan Kay says:
Everything is an object
Objects communicate by sending and receiving messages
def bees if :bar == a.foo else end end
def bees a.foo nil end
Tell don’t ask
Objects have their own memory (in terms of objects).
Data hiding
Every object is an instance of a class (which must
be an object).
The class holds the shared behavior for its instances (in
the form of objects in a program list)
To eval a program list, control is passed to the
first object and the remainder is treated as its message.
Type Checking is the Antithesis of Object Oriented Programming
Type Checking is the Antithesis of Object Oriented Programming
My Problem
My Problem
My Solution
Just always make your methods return things of a consistent
type class
Thanks!
No obviously there’s more
Third party APIs do this all the time
pony = Pony.find_by(:id => smth) if pony pony.neigh else puts
“No Pony can’t neigh” end
The problem here is two return types
As a client of this API I am forced to
add a type check
nil is such a common case
How do we fix it?
Null object pattern
I think this one is quite well known
class Pony def horse_power 0.5 end end
Pony.find_by( :key => value ) || NullPony.new
class NullPony def horse_power 0 end end
NullPony quacks the same as Pony
Solves the typing problem
Summing over ponies will only count Pony objects
0 might be the wrong default
Pony * NullPony = 0
Decided the default for horse_power when defining the class
Change is inevitable
Can’t predict how NullPony will be used in the future
Maybe Typeclass
Solves same problem
Allows for runtime defaults
#map(&blk) -> Maybe #value_or(a) -> a
class Just def initialize(value) @value = value end def map(&blk)
def value_or(x) Just.new(blk.call(@value)) @value end end end
class Nothing def map(&blk) self end def value_or(x) x end
end
A consistent interface for dealing with missing values
NoMethodError: undefined method `foo' for nil:NilClass
NoMethodError: undefined method `foo' for nil:NilClass
[Maybe, Nothing, Maybe]
call map on all of them
collapse with value_or
To Recap:
Null object can replace nils if you know the defaults
at class definition time
Maybe if you want defaults at run time
Your job is not to make Alan Kay happy
RSpec RSpec ! ! RSpec 3
tinyurl.com/ samfr2014
Let’s have some questions a!/samphippen
[email protected]