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
Extremely Defensive Coding
Search
Penelope Phippen
October 08, 2015
0
88
Extremely Defensive Coding
Penelope Phippen
October 08, 2015
Tweet
Share
More Decks by Penelope Phippen
See All by Penelope Phippen
Introducing Rubyfmt
penelope_zone
0
540
How RSpec Works
penelope_zone
0
6.5k
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
84
What is processor (brighton ruby edition)
penelope_zone
0
94
What is processor?
penelope_zone
1
340
extremely defensive coding - rubyconf edition
penelope_zone
0
250
Agile, etc.
penelope_zone
2
210
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Rails Girls Zürich Keynote
gr2m
94
13k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Embracing the Ebb and Flow
colly
84
4.5k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
The Pragmatic Product Professional
lauravandoore
32
6.3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Transcript
Extremely Defensive Coding
a!/samphippen
I want to start with a story
“When is it OK to override the methods on object?”
I umm’d and ahh’d for a bit
I gave a half explanation and then asked them if
they understood
I eventually came to an answer to do with consistency
Do it when it makes your object more consistent with
Ruby, not less
e.g. an == on a data object
What makes a gem good?
Internals?
Internals?
Interface?
Interface? ✅
More convenient than if I did it myself
And it stays convenient
And it can be used by everyone on a team
And it works with a wide range of Ruby codebases
I don’t want much do I?
RSpec
RSpec Definitely always convenient
RSpec Definitely always convenient
User story
As an RSpec user
When I stub an object
I want the original method to be on that object
after the example
So that my objects aren’t broken by my test suite
As an RSpec user
When I stub an object
I want the original method to be on that object
after the example
So that my objects aren’t broken by my test suite
How does that work?
allow(cat).to receive(:meow)
Takes the meow method off cat
Saves it
Executes test
Puts the method back on the original object
How do you save a method?
None
Method object
Put it somewhere else
Put it back at end of test
The end
The end
Defensive coding
RSpec::Support .method_handle_for
Simply invoking the method method is not good enough
Some scenes may have been altered/accelerated for your viewing pleasure
Let’s have some questions !!/samphippen
[email protected]
Users Lie
def method ‘get’ end
Users can redefine anything at any time in Ruby
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Instance method object Method without a target object
Comes from a class/module not an instance
Grab the Kernel implementation of #method
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Users can and will redefine core methods at any time
Instead we use Kernel’s implementation
Nobody screws with Kernel
Ruby interpreters lie
Some objects do not have Kernel in their inheritance chain
None
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Rebinding module methods
c
c
RSpec does not support Rubinius
None
We tried, we really really tried
So anyway, dealing with module methods
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Let’s have some questions !!/samphippen
[email protected]
Ruby interpreters behave differently.
Let’s have some questions !!/samphippen
[email protected]
Sometimes users don’t lie
None
None
None
None
`method': undefined method `foo' for class `Foo' (NameError)
This is a catch 22
Solution: trust but verify
None
None
None
Let’s put it all together
None
Wrapping up
This is why I love RSpec
Please file bugs
Your gem should be defensive
Users lie Redefinitions come from anywhere, expect them
Ruby interpreters lie Your code will run on non-MRI, old
MRI, etc
Sometimes users don’t lie Users are weird, trust but verify
What makes a gem good?
Internals?
That explanation made no sense
Internals?
Interface?
allow(cat).to receive(:meow)
You get the complexity of RSpec working with any object
for free
You probably didn’t even know it was there until I
just told you
Interface? ✅
Gems
Gems are like super objects
Done badly they cause an even more extreme mess
Done correctly they hide huge complexity behind well defined barriers
Defending against users helps your gem be convenient
Write defensively, and your users will never know what’s inside
the box
Remember the story?
This talk is born out of Ruby’s power
We need our code to defend today, against the mistakes
of our tomorrow
None
Let’s have some questions a!/samphippen
[email protected]