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
Types and Refactoring
Search
Penelope Phippen
July 21, 2014
Technology
3
180
Types and Refactoring
A talk about types and refactoring I gave at Brighton Ruby 2014.
Penelope Phippen
July 21, 2014
Tweet
Share
More Decks by Penelope Phippen
See All by Penelope Phippen
Introducing Rubyfmt
penelope_zone
0
500
How RSpec Works
penelope_zone
0
6.3k
Quick and easy browser testing using RSpec and Rails 5.1
penelope_zone
1
75
Teaching RSpec to play nice with Rails
penelope_zone
2
120
Little machines that eat strings
penelope_zone
1
79
What is processor (brighton ruby edition)
penelope_zone
0
92
What is processor?
penelope_zone
1
340
extremely defensive coding - rubyconf edition
penelope_zone
0
230
Agile, etc.
penelope_zone
2
200
Other Decks in Technology
See All in Technology
元インフラエンジニアに成る / Human Resources to Human Relations
bobtani
3
810
巨大なテーブルのテーブル定義を無停止で安全に誰でも変更できるようにする / Table-definitions-for-huge-tables-can-be-modified-by-anyone-safely-and-non-disruptively
freee
1
740
Reducing Cross-Zone Egress at Spotify with Custom gRPC Load Balancing Recap
koh_naga
0
120
Janus
bkuhlmann
1
490
長期運用プロジェクトでのMySQLからTiDB移行の検証
colopl
2
670
ユーザーストーリーのレビューを自動化したみたの
bun913
1
330
コンテナセキュリティの基本と脅威への対策
kyohmizu
3
700
クラウドサインにおけるプロダクトマネージャーの役割と開発プロセス / 20240410_cloudsign-PdM
bengo4com
1
680
DevOpsメトリクスとアウトカムの接続にトライ!開発プロセスを通して計測できるメトリクスの活用方法
ham0215
1
200
アクセシビリティを考慮したUI/CSSフレームワーク・ライブラリ選定
yajihum
0
160
ここが嬉しいABAC ここが辛いよABAC #再解説+補足編
masahirokawahara
0
220
**強い**エンジニアのなり方 - フィードバックサイクルを勝ち取る / grow one day each day
soudai
61
18k
Featured
See All Featured
What's new in Ruby 2.0
geeforr
337
31k
Music & Morning Musume
bryan
41
5.6k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
220
21k
Designing with Data
zakiwarfel
95
4.8k
Fashionably flexible responsive web design (full day workshop)
malarkey
397
65k
BBQ
matthewcrist
80
8.7k
Documentation Writing (for coders)
carmenintech
59
3.9k
Six Lessons from altMBA
skipperchong
20
3k
Optimizing for Happiness
mojombo
370
69k
Into the Great Unknown - MozCon
thekraken
10
980
Embracing the Ebb and Flow
colly
79
4.1k
Transcript
Types & Refactoring
a!/samphippen
Types
I think type systems are great!
I think dynamic languages are great!
I think thinking about types in dynamic languages is great!
RUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUUUUUUUUUUUUUU UUUUUUUUUUUUUBYYYYYYYYYYYYYY YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
No type systems here
NoMethodError: undefined method `foo' for nil:NilClass
No explicit type systems here
What is a type?
Concrete implementation of some data and behaviour
Class
1.class # => FixNum
(1.0).class # => Float
What is a typeclass?
A set of methods satisfied by many types
Interface
(1+1.0).class # => Float (1.0+1).class # => Float
But also things like
Positive Int + Positive Int # => Positive Int
some_collection.count
String Hash Array
Form a typeclass (or interface) under count with no args
Methods that return consistent interfaces are great
The problem
We’re not forced to return a single interface
If interface is inconsistent, code must check types further down
For example returning nil
nil is not type classed the same as your object!
pony = Pony.find_by(:key => value) if pony pony.neigh else puts
“pony was nil” end
The if is a subtle form of type checking.
Type checking is the antithesis of OO
Can we get a consistent interface?
Refactoring
Null object
class Pony def horse_power 0.5 end end
Pony.find_by( :key => value ) || NullPony.new
class NullPony def horse_power 0 end end
Decides default for horse_power at define time
May or may not be what you want
Maybe
Steal ideas from functional programming!
Decide defaults at run time
Maybe is a type class
#map(&blk) -> maybe #value_or(x) -> Object
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
Map until done with transforms
use maybe.value_or(“something”)
Recap
Methods that return nil are almost always inconsistently typed
Use a null object to replace a nil if you
know defaults at define time
Use maybe if you want to set defaults at run
time
RSpec RSpec ! ! RSpec 3
tinyurl.com/ sambr2014
Let’s have some questions a!/samphippen
[email protected]