Thursday, October 06, 2016

Baruco 2013: Rules, by Sandi Metz

%ah I have seen specimens big many times
and on a and and on occasions at where I've spoken before of to
I always publicly admit I still haven't read her book but
today I want to tell you something
I haven't read a book it and
from I still feel bad about to die because
because I have yet to hear a person that says read that book ed
at 10:11 affect their life your professional life and perhaps their
spiritual life in some way because up the ID's in it sandy is an exceptional
and I'm really would like to put your hands together for rapid and Ms
broken doctor Nick told me not to tell me she creeps
because he said he would steal them and tell you and
I have to tell you that he stole my nap joke still
I'm and I know we're we're running a little behind schedule and my tacos
exactly 30 minutes show
on the user just a minute before I start to tell you that I feel completely
unsympathetic if you're used to having and map out a after lunch and now you're
because I am missing the welter the three-week by
torrent Spain which to me maybe for me with it an American Chris Horner took
the lead yesterday in a dramatic finish
today is the final day in their writing right now
22 preciate I'm here with you instead
in the front of the TV you
you're welcome chill rules
when a great title for attack rate we levels in a shock to me give you
five rules about how to write object-oriented code
following these rules will
improve your cold the rules are important
see should always follow them and they're completely
arbitrary so it's perfectly OK to break them
and was preparing the shotgun a bunch research about
rules and rule following in humans in why we formal rules even when
we would not otherwise even when no one is gonna bust us and we would not
otherwise be harmed by disobeying them and I found some
really interesting research at least was interesting to me about
room following which i think is even more important in the rules I'm gonna
give you today
and show this stock is in three parts it for some
just talk a little bit about wools what rules or
and how social scientists to find them and then we'll switch to the technical
part and I'm gonna give you five rules for writing code
gonna take you have a right onto drinker in at the end we're gonna go and do them
a little met awk about rules which you can give you an even more important
reason to have rules okay so first what social scientists think the rules
on they think the rules is falling in 13 categories
there are taboos too bizarre things that are
forbidden we can give them is unclean or
hearst in the usually involve breaking it to do usually involve some form of
punishment I'm incest is it to do and so is cannibalism
there's oddly enough tears
there are no universal taboos there's not any one single
prohibited thing that is uniform across all cultures
next after to boost their loss
malls are rules that govern behavior better
enforced by social institutions which are not always rational
their criminal laws were this
fate decides they're trying to maintain what they whatever they think I'm a
or in there also civil laws where are the state educates between two people
who are having a dispute
and finally after a loss their customs or what social scientists call norms
norms are group held beliefs about how members should behave in a certain
an example a group norm is that kissing scene that people do when they greet
this is a police station in France
arm where they're changing shifts this would be
absolutely abnormal in america but it is probably would probably be abnormal not
to do this in France
arm it's clear to me that I do not look Spanish
because everyone to greet me either give me a hug her shake my hand
in last night I had to ask the guys is we're leaving the dinner
if they would be given cheek is a former Spanish in a said yes
and so they taught me how so I got to practice that last night which was very
september's walls in norms
many of them were both arbitrary and unenforceable
into the really interesting question about rules is why
we obeyed him and social scientist the people who should be something they call
rules system phiri the traditionally give
eight possible reasons to explain why someone might follow a rule
in the research I'm gonna talk about in part 3 suggests and non
which again i think is the most important reason but here but for some
are here the first day
self-interest you might follow a rule
because you gain something to achieve a gain or to avoid a loss
we want to win the trophy and show we follow the rule
next you might fall rules because you want to mark your shelf as
part of a group the French police people might
a think themselves the really play group who greeted by kissing on the cheek
in we might the commercials is the group that never leaves trailing white space
their all kinds of ways for groups to identify themselves
and regardless of how inform all the rules are they delineate the group
and we use them to recognize each other all you have to do look
is look at the picture and you know it us they're marked
as identity they follow the rules now that she might for
you might fall will because it's given to you by a4e
there are usually religious authorities is a pope in Barcelona a couple years
but their secular thorny there's no law that says you're out but you go back to
the dugout regardless
group promote authorities and then they respect the rules that they create
fourth people follow rules because we we have a sensible order
people love or we follow rules that are otherwise meaningless because they make
the world scene
regular and dependable we know the right distance apart to stand at the bus stop
and it bothers us when people violate that rule even when it does not
otherwise harm us
next their social sanctions the most obvious being present
but some rules have such built-in sanctions
that we tend to follow them even when there's not a law prohibiting
shows we follow because with the common veal love ignorance
we follow these rules even when we are harmed by doing so because we don't
understand the consequences imagine that you have a rule
accept all free themes
this can turn out badly and family might follow our simple habit
we like to think of ourselves as self-aware but there are many ways in
which we are programmed by our experiences in our culture's
and the most deeply accepted rules are often the least question
so we have so there you go thats rules taboos laws and norms
and we followed them for all kinds of reasons even when it against our own
and chill against that overview rules I'm gonna give me five
these rules about how to write object-oriented code now these rules are
no rooms
they're not lost and even though sometimes we treat our rules as
taboos these are not chiseled in stone
and took me to the rules is actually a constraint
it to limitation on behavior
the first row my first warm writing object-oriented code is that a class
me canteen no more than a hundred lines occurred
the second row is that a method may contain no more than five
and the Thirroul is that you may pass no more than four parameters to a method
I have always regretted this role feels like maybe it ought to be
3 somewhere between three and four 4
if if you have to pass for parameters there's definitely an object in there
oh and the Chorley this role she cannot just keep with the hash
I am
don't even right to this first three rows a hundred lines in class five runs
in a method for primers path to mess it there
strictly about writing plain old really code
the next few rules are about writing real scared a controller action may pass
only one instance variable to view
and a controller action may know only too
other class names now some movie probably may have heard
me give this generals on the Rebbe rogues in with your ex back as early in
the spring and I misspoke
on that part cash and I should one here that a controller action could no one
what I meant that is was it could no one business question 1 presenter class
many people who heard that part cast adopted that mu
and I can tell you it it works perfectly fine and show just like maybe four
printer should be three maybe two classes should be one
the constraint could beat the constrained could only work if they're
tighter and the course I get one final ruling it a rule about breaking the
so you can break in here or you want as long as you can talk someone into
believing it a good idea
act so here they are five simple rules
they're incredibly easy to remember near easy to implement
the walls aren't complicated but following them has
enormous consequences imagine
the app that was created by these rules
the force you
to create small objects and the bias those small objects
toward poll rose toward Pauline old Ruby objects
they you can't write objects that are bound up inside the framework to use
the rules force e2 the object to you create to know a minimal number of other
things into object end up with
very few dependencies we don't know what the future holds
and but we'd like our apt to survive it and following these rules will help
they make changeable lapsed they're built up small objects
that require little context and have few dependencies
their simple straightforward well tested code
the King easily be reused in novel and unexpected circumstances
up sorry so the rules themselves are not that special
in some ways they are completely arbitrary
I cannot make a case that says 101 is substantially worse than a hundred
or that 99 is substantially better these are just
a warning in the same their market shares common here and go no further
this is the place where you have to ask the question
she I break this thing in half should I this is our what responsibility can I
chisel of
into another object the rules are means to an end
and that and is to reduce the cost of software
if you define costas time money or pay me
in what we want is to produce features at the lowest cost
in my shirt that the best way to do this
is to make small objects and I assert that the best way to do
VAP is to define watch small means to make a rule about small
and then follow that rule so the walls
are to achieve a goal in achieving the goal is for more important than the
we're not bound to these rules were bound to the goal
and we will drop any wall that letters achieve that goal
so I think I'm right right
I think small object to reduce your costs but I am
I am absolutely willing to admit that small objects have a price
there's a cost to their own this is a design choice
in like every design choice it has consequences following these rules
place is a bet that you'll be better off making many small objects
then you will be making a few large want its choosing between
object and procedures on
I thought it was interesting that match in this morning that
the scripts were 100 lines long and that's a procedure
right which is kind my limit on the quack the size of a class
on happy when I've been on
I its bad for ministry from my scripture actually more questions about that if
you wanna hear about it later
show should think about this imagine your application has
on a continuum on one side you can have a few really large
objects long step-by-step procedures that have all the code in
at the other end to the continuing there's an application made have many
interacting objects on the this
this very big the mister Wong procedures
they are really easy to understand you can look at that code and tell exactly
what's going on
however if it's hard to change it easy to break something else if you make a
change inside a long procedure
and it's very difficult to reuse party but you end up having a copy and paste
and you have to worry about which
beat that stuff relied on and show did the downside the upside is it seem
really easy to understand when you first look at the downside is that it's almost
impossible to change
if you were at never changes that's a good bet but must've rhd
me up slightly continue me have this notion about being poor small objects
and those small objects arm clearly more difficult to initially understand
looking at the source code a bunch of small classes does not tell you
much of the logic in that apt exists in the messages
and the message is don't actually happened to you reconstituted oh and put
it in memory
and so when you're looking at the source code for a bunch of small objects you
have to construct
imaginary middle model and supply the messages yourself
in order to understand what's going on
messages are at the core object-oriented design make
the creek seems that giving levels have been direction
and they let you different objects that plea roles and then use the roles as
so that you can substitute new object behind them messages are what gives you
change ability
and so we have this tension between an app that contained a buncha procedures
where you can look at the source code and be comforted
because you can understand what it's doing in another app that has a bunch of
small objects where you can look at you cannot immediately tell the operation
the whole by looking at the individual parts hub source code on
desk and I wanted to eat now I want it a two stories
when I was at this spring I had a conversation with a young man who
had read the book
he'd written a couple Bob doing abs but they were mostly collection to belong
and he was trying to follow the pattern love creating
are applications had a bunch of small object and he tried and tried asking a
question that I had a very difficult time understanding some spent a long
time in a house on it
banging heads trying to get it right in finally I understood his question to be
he said are used to write procedures
and now I'm creating a lot in lots of small objects
winning will alright understand my new app
in the same way that I understood the old
and when I finally understood that to be his question I had to look at him and
never you're never gonna understand
this new app in the same way that you understood the old
it's not as if having a budget procedures and having a much smaller and
lighter to parking pads that lead to the scene in point
they don't they go different polices
you you also what I told him was that you will never understand it like you
used to what will happen is that you will see
to care it will know it will quit bothering you afterwhile
and illustrate why Mitt a one more story
Caleb who's here somewhere who works for thought my
and Boston Mass in the US was part of a group of people he
I'll when I first discusses UAL's in the spring he took them into
about which is consulting company and they decided to adopt them on a project
they were working
and using them exclusively as a test as an experiment to see if they would help
and they spent a while on a new project
and it in I had a conversation with him last week
to Sri checkin to see more how they would evaluate that experience in
XP experience in with the tony was that they loved it
they the pic
even though the stream it had ended they work they continue to use the rules on
that project
and the people who had been on a team that left and went to other projects to
the rules with them they found them very useful as a way to rate code
but I specifically asked him about this issue about the tension between
understanding a few large objects foresees a bunch of small ones
and I was particularly worried about interested in the expensive
junior programmers how they dealt with that pool small object
and so when I have to question they turn the camera and pointed a young man named
and I S Paul haddonfield how does it feel working on this application that
bill a bunch of small things in what he said was that he loved it
because it made him feel safe
that he knew that he did not have to understand the entire application
to to make a change he just needed to understand his part
and he had no fear he had absolute confidence that he can if he understood
the park he was changing
he could make that change without breaking something in the larger hole
an axis power a small object it really worked for those guys
into using making this choice going more going more towards small object to cart
to change in perspective
and many others think of our applications israel's amps
and bridge has an opinion about how to organize code in that opinion is
tremendously helpful it's at the root up a real success
I for those of you who are old enough to remember their
there was a video many years ago about make a block in 10 minutes
that we all watch that brought me a mystery else and it was in a meeting
it was a feat of technology and getting at you may remember
in there was a controller that knew the name of one class
post ended use that class to create a shingle instance variable
at post to pass to interview thats
the rails pattern and net application
was a classic Rails app it was a rapper around a database that you do
crud operation directly replace update delete operations against a pilot data
the edges the framework the incoming message and the outgoing persistent were
stuck together
they were glued right in the middle there wasn't any domain logic in
ap in a this is the problem you have
you should use the reels pattern to solve it it exactly right
put all of your code in some classes application controller an Active Record
base that's the perfect solution to that problem
however some other have more complicated maps
they're not really rails aP's there after israel's
an application israel's at one is one that has a bunch business logic that
does not mapped directly to
and action in a controller or to a row in a database table
in when you have backed if I want to follow
the pattern appel reels expects me to behave
and I have a bunch a business logic that doesn't fit into dat
in order to use the rails pattern I have to push the edges in the frame apart
and create new objects in my own and put them in the middle
in the five rules for she did it
that's the way to do it she alleged the final part make small
polo small perot's with few dependencies
create your own our checks they have your business logic and put them in the
and show if I'm right should follow these rules
now because I meant for you and I because you wanted n if I with this
for now because they major centre order
but because it's in your own self-interest
I think they save money in your application but it turned out
but there's more to it than that rules have more value
the just saving money in your application in our part 3 the stock the
middle part
about one's it turns out that you're following is a thing we humans do to
send signals to one another
Anderson interesting research I'm I have a psychology degree so I'm a recovering
psychology should not rely to research
there's there's new research that that I think applies directly to
our business a braking software applications its by these two guys
I can pronounce want their names compile and that other guy
and here's the name in the study which it like every academic study is
impossible a partial let me just do that
this is their assertion that rule screen for coopers tight sonoma tell you about
the study has two parts:
in the first study they divided subsection A groups have 8 and they
wanted to figure out in every movie
which which were more ru following in which were more rule-breaking
and so they have them play this game the goal the goal here is to walk across
through all the stoplights you press this button to make them in person walk
a he what when when they walk the walk to the next red light
he started with 8 euros you lose 8 since a second where you wait
every red light last five seconds in your instructions are
the rule this is what they tell the subject the rule is to wait at each stop
light until it turns green
right to it takes %uh four seconds to walk from white to light
and then across the end in it also those lights
obviously wait five seconds so if you be the wall the most you can keep a very
your own is for if you ignore the role
you get to keep six you have more money left at the end if you ignore the role
and to some folks played a game like this they go to first why
they wait for five seconds it turns green the press lock button will pursue
we okay so rate you get the idea here
home then some other people
played a game like this they just put their farm on the walk button
it turns out the what martin works even if the white red
you won't know that unless you run this expand okay so this is a test they used
separate rule follower shimon breakers and I can hear all you thinking
that you would be a rule breaker because you're programmers and your game players
and you want to know if the walk but working you would press you would try to
let me give you a different
instruction which is this is our instruction did
to decide if your rule follower rule breaker go to first stoplight make this
change in the source code in the check into the source code repository
after removing all the trailing white space
and then press walk
you don't feel like such a rule breaker and how the
yes so did the test is context-dependent so imagine that you get put in a group
based on that card debt text that test a test that can take a picture for us
and so after they didn't with every individual person they took the
foremost wall following people in the foremost rule-breaking people in every
group in a separate them in the team's
and they had the teams played cooperative game
here to begin their search for players they play ten round meet round they
dropped ok inked
and show that total each player has a infinite pyro
have individual tokens in the students are worth $1 there's a shared pool
its capital 360 in their worth to you
in every round every player does sixty tokens and chill
here's what you obviously do in the first room you say well okay I'm just
everybody just ask for sixty there's enough right let's get the tokens that
are worth more
and sleeves 120 tokens
now between rounds the common pool this comment regenerate
by factor up half and show in the regeneration phase the goes from 120 up
to 180
now the Danish two more rules there have not yet told you about
one easy if at the Sherborn ever gets below 30
it doesn't regenerate and the other is
if any RAM people choose more tokens choose to meet up into the Sherborn get
the talking to the shirt will be distributed in proportion to the request
that you made
act and shit let's just play one more game 0 actually we should
so you can think of the shared poor in case you have not already thought about
let me tell you with the shameful stands proxy for it could be a field where
farmers are grazing cattle
it could be /c where we're fishing or it could be an application
on which many programmers work over a number of years
you can overrated for you reading efficiency
are you can hacking hack an app for a while
and they still be varying but if you use them for too long a time comes when
they're done
they're no good anybody and you if you're playing this game if you're
playing a game where you care about the health of your
ap in an infinite number Rams
and you trust your fellow players
when there's after choosing sixty tokens in the first round here say play around
you do this message there's a honey left well I can't get into the bottom 30 shot
to take those off
on this divide that before that cuz that's only fair
37.45 get around down so I'm gonna pick 37
and sure enough
here we go show is 180 ready if everybody takes 37 here on my camera
lets do this
area every week it's 37 we end up with %uh
32 the thing regenerate a
we get forty a at Chur so you can see that cooperators
people who want to show people to get maintained indefinitely rapidly devices
every player chooses ish in every round that the shameful rapidly decreases so
you only get a few in every round
but everyone is better off if you keep the pool together if the pool survives
and so you pick what your shares every round it totally works
alright to remember we did the pretest we separated have two teams are
followers in breakers they have been played this game
here's what happened the group rule followers
were four times more likely then rule breakers
to cooperate to keep the pool live
in addition it was clear they when they fail all that they feel because he can
get the math right
under pressure buried there anything I'm talkin to the rounding issue is a
problem like if you round up and everybody rounds up in you get cross at
thirty boundary
but they did to go now sits on the draw said they were making in the win
they need to do it in a and it had they been able to come and even able to
communicate they would have sustainable forever
the king to rule breakers
consume the common resource much instantly and by their choices
mathematically you can tell they're not even trying there
over harvesting dramatically it is it is during
each Wow their mental calculation looks like this
she wanting the measured in the study was something they call rested
prosit E track have a hard time saying I've been practicing all morning
and it's the 10-team someone to in the next round dues
what someone else did in the prior around and it turns out that we're
heighten the choices are highly reciprocal
and it is great if every mans mans
rate if if if if everyone's nice in
we're all behaving riche prickly the niceness hesitancy be maintained
however in each group where their players who have bad behavior
not only did they behave badly but then the ownership repeat the bad behavior
and they have this amazing race to the bottom
but so this is interesting but have more enters to us
is the repeated the study in the used mixed group big-name a group that had
some more followers and some will breakers
and these groups behave like break a group
and the authors theorized that it's because this quality of
ressa proctor Chrissy it is if
there's a round plain where someone does that and it just poke everyone else in
the eye and in the next round they did this
right okay buddy they
they go maybe he just like break to bridge the bridge should have won
I'm yet a break a group that's mostly will followers it has a single
rule breaker rapidly degrades to this and here's what the authors on the study
say about this
they say that in mixed groups this war always be true
that having one rule breaker on a team or in ever to buoy cause it to the
saucer sure
poo and by extension overgrazing feel
are overfished see
and I hesitate to take this analogy all the way to software development
but I sometimes wonder if I should
show they also say that the only way to avoid this problem
the only way to avoid the inevitability have mixed group
over her missing a resource is to screen for Ruby
breakers and preemptively exclude them
harsh I know still is usually to use one LG to give me words to think about the
problem not having everybody on board on the team
and I think I it will help me think about this and show
a reprise in light of that study what are the consequences for
up my rules why they still why she told small objects and pole rose
and away from dependencies but a willingness to follow rules
ET walls any ripon said the rules that are constructed to achieve a common goal
which sends a signal
have willingness to collaborate
this means that you're a person who would do what it takes to maintain an
application overtime in round after round after round no matter what the
future holds
an end to as the same ol screen for cooperative tights and these are just
the types
with whom we want to work wall's matter
technically and social their
restrictions right the restrictions their alarms to go off the train which
time stop and think
before she to make small objects incinerating big procedures
and the four she keep your business logic out on whatever framework you're
the rules on sections down we don't care action with the rules are what I want is
the good thing they give you we want cost-effective software
and all adopt any rule that gives me Dad
if you're an experienced program just make your own rules
I don't care just makin any walls the point is it my rules
its the belief in some rules and the willingness
and South disappoint to follow them
but if you're not if you don't yet have enough experience to make your own rules
follow these
do it will improve your software
you can break the rules only under the supervision
up a trustworthy person that has more experience
and how do you find that person well they have rules
and they're sitting all around you right now sending signals
by following them thank you
a.m. I know at first what you are saying about you know you can break the rules
be over
reasons archer and in your book you talk about
divide responsibilities into modules and rolls researcher
and is gonna similar to ID zein you know Jim
days hmm can rebook and he talks about putting
the modules inside the use case and of course that kinda brings your total
number of class lines
over two hundred and it's hard it's a Tess
his point is that that's kinda logic to be self-contained
so what's your kind review should be other the roles
separate from things that you can I'm kinda hard ass about this
okay May Camacho if especially that models only using one class
you're deciding some gold from either belongs in the classic count against you
on cam
okay sorry justice
should be smaller than try if you try to do if you try to solve the problem with
smaller pieces
music and against you I think you'll like this offer you get thank you
so mad this morning talked about how
it is I'm really to give the program a lot of freedom and
it seems like some programming languages
have sorta rules Ruby has freedom
that type language doesn't like change the type of something after you
initialize it
Ruby you can do that and I wonder
I don't like would be better to have a programming language that actually
enforce these rules
language level like why use language that gives you so much freedom
when you're gonna then place rules
freedom your own
him away how to be better I
home hate the idea that constraint give freedom abolished
famine that get me to hear my neck stand up whenever her nephew that for each
in I hate people given the rules and so I would
at i'ma deeply died in will dynamically typed x-small talker
and I want the freedom to do whatever I change whatever i think is best for my
application so I
I would never consider trying to enforce these limitations on people
I initially made up these rules because I was
helping out a group of people that had gone we me for on the other side so they
had 1000 morning controllers
1500 mind only models and you're trying to construct a show the rules that would
push hard on the other side the teeter totter I
I since found it folks found these rules useful
it's interesting to talk a lot more everyone when I talk to this guy so
sweetly talked about how much freedom they fell
by having these walls it freedom from thinking
from making whole class decisions because you just told them when they had
a shirt
thinking and should I would I would never try to enforce these rules on
but I am willing to entertain the idea that perhaps there is some free and
not because my personal experiences ever because pp tell me the region that way
my biases towards module I just use natural
my in my cuz there's almost nothing that doesn't need jewels should
it it feels like the right way to record not a lim