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
Always default to false - Paul Grayson
Search
Las Vegas Ruby Group
January 29, 2014
0
67
Always default to false - Paul Grayson
Las Vegas Ruby Group
January 29, 2014
Tweet
Share
More Decks by Las Vegas Ruby Group
See All by Las Vegas Ruby Group
Ruby ISO Standard - David Grayson
lvrug
0
120
Windows Automation - Howard Feldman
lvrug
0
64
Separating Your Application from Rails - Brian Hughes
lvrug
0
110
SWIG and Ruby - David Grayson
lvrug
0
72
Practical Object-Oriented Design in Ruby - Charles Jackson
lvrug
3
120
The Hamster Gem - Ryan Mulligan
lvrug
1
86
Varnish+Redis - Russ Smith
lvrug
1
99
Lambdas and Pops - Jan Hettich
lvrug
0
72
Making Good Use of Fonts - Russ Smith
lvrug
1
83
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
53
7.8k
Balancing Empowerment & Direction
lara
3
610
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
13k
Raft: Consensus for Rubyists
vanstee
140
7.1k
It's Worth the Effort
3n
187
28k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
Code Review Best Practice
trishagee
70
19k
Building Applications with DynamoDB
mza
96
6.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
A designer walks into a library…
pauljervisheath
207
24k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
How GitHub (no longer) Works
holman
315
140k
Transcript
Always default to false Paul Grayson Las Vegas Ruby User
Group 2014-01-29
Be opinionated! • There is a right way • Consistency
aids memory true false 1 exception 0 default
The default is already false or null in many contexts
Ruby: @var, if x, methods, [], etc. -> nil SQL: NULL Javascript: undefined C#/Java: bool/boolean defaults false Originates in digital circuits? - default is OFF
Options should be opt in ☐ YES, please add me
to your mailing list ✓
Fields, variables, conditionals # DO THIS: if on_mailing_list send_email end
# NOT THIS: if !on_mailing_list withhold_email end How would you even write that method? Body does not run by default
Be careful with true default <% if !out_of_stock %> <div
id="order_form"> ... </div> <% end %> Must remember to not put anything essential in here.
Emphasize connections def f if x exceptional_stuff else default_stuff end
more_stuff end The normal path Condition and code to handle it
true default complicates connections def f if !x default_stuff else
exceptional_stuff end more_stuff end The normal path Condition and code to handle it
The “guard clause” pattern def f return y if x
normal_stuff ... end The normal path Condition and code to handle it
Summary • Default false for all fields • Test for
unusual conditions (when possible) • Beware of conditionals with true default true false 1 exception 0 default