WEBVTT
99:59:59.999 --> 99:59:59.999
Hey, I am Sandi Metz and I am the
99:59:59.999 --> 99:59:59.999
last but one speaker and that means
99:59:59.999 --> 99:59:59.999
you have almost made it.
[audience laughter]
99:59:59.999 --> 99:59:59.999
You get a break, there's another keynote
99:59:59.999 --> 99:59:59.999
if you stay for that –
99:59:59.999 --> 99:59:59.999
it's really bright up here, I'm going to have
99:59:59.999 --> 99:59:59.999
to look at you some. So, yeah,
99:59:59.999 --> 99:59:59.999
you've almost arrived. Even those of you who
99:59:59.999 --> 99:59:59.999
are new have almost survived a total RailsConf
99:59:59.999 --> 99:59:59.999
and so I think we should – let's do this –
99:59:59.999 --> 99:59:59.999
sponsors pay the bills, they do, we're glad. [clapping]
99:59:59.999 --> 99:59:59.999
Thank 'em.
99:59:59.999 --> 99:59:59.999
But even more than that
99:59:59.999 --> 99:59:59.999
"human people" organize this conference
99:59:59.999 --> 99:59:59.999
and if you see – this is your job before you
99:59:59.999 --> 99:59:59.999
leave today – find someone in one of those
99:59:59.999 --> 99:59:59.999
red shirts, and say thank you.
99:59:59.999 --> 99:59:59.999
Alright? Let's thank 'em here.
99:59:59.999 --> 99:59:59.999
Thank 'em in person.
99:59:59.999 --> 99:59:59.999
Going to start my clock, I have about –
99:59:59.999 --> 99:59:59.999
I really do have about 40 minutes.
99:59:59.999 --> 99:59:59.999
I don't have 500 slides of code this year,
99:59:59.999 --> 99:59:59.999
though, so… it's not going to be that bad.
99:59:59.999 --> 99:59:59.999
Yeah, let's just go. So
99:59:59.999 --> 99:59:59.999
I'm Sandi Metz and I'm really happy to be here
99:59:59.999 --> 99:59:59.999
and I have a talk. This years talk is
99:59:59.999 --> 99:59:59.999
about code smells.
99:59:59.999 --> 99:59:59.999
This is a term invented by this guy. So
99:59:59.999 --> 99:59:59.999
it's really, uh, we did not plan that
99:59:59.999 --> 99:59:59.999
I would follow Katrina
99:59:59.999 --> 99:59:59.999
but it's the perfect talk to follow her
99:59:59.999 --> 99:59:59.999
talk. She just mentioned this guy's name.
99:59:59.999 --> 99:59:59.999
This guy's name is Kent Beck. This guy's
99:59:59.999 --> 99:59:59.999
Martin Fowler, he's the man who wrote
99:59:59.999 --> 99:59:59.999
this book, which is the book
99:59:59.999 --> 99:59:59.999
that teaches us to do refactoring.
99:59:59.999 --> 99:59:59.999
They collaborated together on the
99:59:59.999 --> 99:59:59.999
third chapter of that book, and
99:59:59.999 --> 99:59:59.999
it's like naming things wins and
99:59:59.999 --> 99:59:59.999
the name "code smell", the reason why
99:59:59.999 --> 99:59:59.999
you know that term today is because
99:59:59.999 --> 99:59:59.999
of what these guys did back in the 1990s.
99:59:59.999 --> 99:59:59.999
Now,
99:59:59.999 --> 99:59:59.999
I wrote a book a couple of years ago
99:59:59.999 --> 99:59:59.999
– before that, I wrote code for 35 years,
99:59:59.999 --> 99:59:59.999
I went to my desk every day and
99:59:59.999 --> 99:59:59.999
wrote code – and now, I don't do that.
99:59:59.999 --> 99:59:59.999
I teach. I teach classes in
99:59:59.999 --> 99:59:59.999
object-oriented design.
99:59:59.999 --> 99:59:59.999
And in my classes,
99:59:59.999 --> 99:59:59.999
I have occasion to ask people
99:59:59.999 --> 99:59:59.999
if they've heard of code smells
99:59:59.999 --> 99:59:59.999
and just like I suspect all of you
99:59:59.999 --> 99:59:59.999
everyone in my class always says
99:59:59.999 --> 99:59:59.999
"Oh yeah, we know what code smells
99:59:59.999 --> 99:59:59.999
are." And then I ask them
99:59:59.999 --> 99:59:59.999
to list five…
99:59:59.999 --> 99:59:59.999
and no one can.
99:59:59.999 --> 99:59:59.999
Alright, okay, now I can hear
99:59:59.999 --> 99:59:59.999
you trying, but really most of you cannot.
99:59:59.999 --> 99:59:59.999
I know that, okay.
99:59:59.999 --> 99:59:59.999
And so, we all think
99:59:59.999 --> 99:59:59.999
we know about this term
99:59:59.999 --> 99:59:59.999
but it doesn't mean "I don't like your
99:59:59.999 --> 99:59:59.999
code and I can't tell you why."
99:59:59.999 --> 99:59:59.999
That is not what a code smell is.
99:59:59.999 --> 99:59:59.999
There is some opinion involved
99:59:59.999 --> 99:59:59.999
but really, it's not just that I
99:59:59.999 --> 99:59:59.999
disagree with how you wrote your code,
99:59:59.999 --> 99:59:59.999
the standard is higher. These smells –
99:59:59.999 --> 99:59:59.999
these 22 different smells all have
99:59:59.999 --> 99:59:59.999
very precise meanings…
99:59:59.999 --> 99:59:59.999
and the power, the magic if you will
99:59:59.999 --> 99:59:59.999
of this list is they've given things
99:59:59.999 --> 99:59:59.999
names. Once you've given a complex idea
99:59:59.999 --> 99:59:59.999
a name, if we could just
99:59:59.999 --> 99:59:59.999
learn what that name stood for
99:59:59.999 --> 99:59:59.999
it means we could just talk to each other
99:59:59.999 --> 99:59:59.999
in an unambiguous way without
99:59:59.999 --> 99:59:59.999
having miscommunications. And so
99:59:59.999 --> 99:59:59.999
one of the things they talk about –
99:59:59.999 --> 99:59:59.999
very often when people talk about
99:59:59.999 --> 99:59:59.999
code smells, they prefix "code smell"
99:59:59.999 --> 99:59:59.999
with the word "bad".
99:59:59.999 --> 99:59:59.999
They say "bad smells".
99:59:59.999 --> 99:59:59.999
But really, the definition of a code
99:59:59.999 --> 99:59:59.999
smell is that it might indicate a problem.
99:59:59.999 --> 99:59:59.999
You don't have an obligation
99:59:59.999 --> 99:59:59.999
to filter out all the smells,
99:59:59.999 --> 99:59:59.999
and it's actually important that you not.
99:59:59.999 --> 99:59:59.999
It's also worth getting familiar
99:59:59.999 --> 99:59:59.999
with code smells because they have
99:59:59.999 --> 99:59:59.999
such great names.
99:59:59.999 --> 99:59:59.999
Look at this list.
99:59:59.999 --> 99:59:59.999
"Feature Envy" – that's cool.
99:59:59.999 --> 99:59:59.999
I like this one, the one that
99:59:59.999 --> 99:59:59.999
needs a code of conduct:
99:59:59.999 --> 99:59:59.999
"Innappropriate Intimacy".
99:59:59.999 --> 99:59:59.999
"Shotgun Surgery", that's another
99:59:59.999 --> 99:59:59.999
favourite.
99:59:59.999 --> 99:59:59.999
And so, the problem with this list
99:59:59.999 --> 99:59:59.999
– first of all, okay, I'll confess –
99:59:59.999 --> 99:59:59.999
I had that book for a really long time
99:59:59.999 --> 99:59:59.999
before I read it. I had a really
99:59:59.999 --> 99:59:59.999
hard time with it.
99:59:59.999 --> 99:59:59.999
It's one of those books that's
99:59:59.999 --> 99:59:59.999
like a recipe book and I've heard
99:59:59.999 --> 99:59:59.999
people who – it would be like reading
99:59:59.999 --> 99:59:59.999
a cookbook if you did not eat.
99:59:59.999 --> 99:59:59.999
And so I really need the story
99:59:59.999 --> 99:59:59.999
arc and I had very hard time
99:59:59.999 --> 99:59:59.999
following along, like, persisting
99:59:59.999 --> 99:59:59.999
with a bunch of recipes that were just
99:59:59.999 --> 99:59:59.999
like etc., etc., etc.
99:59:59.999 --> 99:59:59.999
But it turns out
99:59:59.999 --> 99:59:59.999
it was really worthwhile
99:59:59.999 --> 99:59:59.999
and I finally did it and actually
99:59:59.999 --> 99:59:59.999
Katrina made me. I have to say it.
99:59:59.999 --> 99:59:59.999
Katrina made me.
99:59:59.999 --> 99:59:59.999
So there's 22 things on this list.
99:59:59.999 --> 99:59:59.999
I had a Stack Overflow at 22.
99:59:59.999 --> 99:59:59.999
It turns out there's a guy who's name
99:59:59.999 --> 99:59:59.999
I can't pronounce – "Mäntylä" – it'll
99:59:59.999 --> 99:59:59.999
be in the credits, he wrote a paper
99:59:59.999 --> 99:59:59.999
where he grouped 'em. He grouped
99:59:59.999 --> 99:59:59.999
'em in five different categories,
99:59:59.999 --> 99:59:59.999
and through the magic of keynote,
99:59:59.999 --> 99:59:59.999
I can do that.
99:59:59.999 --> 99:59:59.999
This is the only reason we make
99:59:59.999 --> 99:59:59.999
talks, so we can use the effects.
99:59:59.999 --> 99:59:59.999
So here, let's just talk about
99:59:59.999 --> 99:59:59.999
of things that just do not need
99:59:59.999 --> 99:59:59.999
to be that big.
99:59:59.999 --> 99:59:59.999
Long methods and large classes
99:59:59.999 --> 99:59:59.999
are probably self-explanatory.
99:59:59.999 --> 99:59:59.999
Data Clumps is where you have
99:59:59.999 --> 99:59:59.999
two or more pieces of data that
99:59:59.999 --> 99:59:59.999
always appear together; pass 'em
99:59:59.999 --> 99:59:59.999
around and in together.
99:59:59.999 --> 99:59:59.999
Long parameter list is obvious.
99:59:59.999 --> 99:59:59.999
It might only occur once, but if
99:59:59.999 --> 99:59:59.999
it's long enough, there's probably an
99:59:59.999 --> 99:59:59.999
object in there somewhere.
99:59:59.999 --> 99:59:59.999
And this other wonderfully named
99:59:59.999 --> 99:59:59.999
thing called "Primitive Obsession", we saw
99:59:59.999 --> 99:59:59.999
an example of that in the talk Katrina
99:59:59.999 --> 99:59:59.999
just gave. This is like when you have
99:59:59.999 --> 99:59:59.999
an instance of a base class
99:59:59.999 --> 99:59:59.999
like a String, or a Number, or a Hash
99:59:59.999 --> 99:59:59.999
or an Array, and you pass it
99:59:59.999 --> 99:59:59.999
around to a bunch of objects
99:59:59.999 --> 99:59:59.999
and they look at it and decide on what
99:59:59.999 --> 99:59:59.999
to do based on something that
99:59:59.999 --> 99:59:59.999
they know about it.
99:59:59.999 --> 99:59:59.999
So you say, "I got a number,
99:59:59.999 --> 99:59:59.999
it's six so I'll do thing X" or
99:59:59.999 --> 99:59:59.999
"it's eight, I'll do something else."
99:59:59.999 --> 99:59:59.999
If only the thing you got was
99:59:59.999 --> 99:59:59.999
smarter, you could just send
99:59:59.999 --> 99:59:59.999
it messages, so Primitive Obsession
99:59:59.999 --> 99:59:59.999
is when the objects are too dumb
99:59:59.999 --> 99:59:59.999
that you're passing around.
99:59:59.999 --> 99:59:59.999
These things are grouped into
99:59:59.999 --> 99:59:59.999
a category called "Bloaters".
99:59:59.999 --> 99:59:59.999
They make code bigger than it needs to be
99:59:59.999 --> 99:59:59.999
in the places where they use them.
99:59:59.999 --> 99:59:59.999
The next group, this one,
99:59:59.999 --> 99:59:59.999
these are ideas that are available in
99:59:59.999 --> 99:59:59.999
object-oriented programming that you
99:59:59.999 --> 99:59:59.999
can misuse.
99:59:59.999 --> 99:59:59.999
Switch statements, you know they're
99:59:59.999 --> 99:59:59.999
conditionals in normal people talk.
99:59:59.999 --> 99:59:59.999
"Refused Bequest" is when it's an
99:59:59.999 --> 99:59:59.999
inheritance problem. You have a subclass
99:59:59.999 --> 99:59:59.999
that overrides a method that it
99:59:59.999 --> 99:59:59.999
inherits from a superclass, and
99:59:59.999 --> 99:59:59.999
throws an exception and says
99:59:59.999 --> 99:59:59.999
like "I don't implement that thing."
99:59:59.999 --> 99:59:59.999
"I refuse the bequest."
99:59:59.999 --> 99:59:59.999
"Alternative Classes with Alternative
99:59:59.999 --> 99:59:59.999
Interfaces" is pretty obvious.
99:59:59.999 --> 99:59:59.999
The other thing is the Temporary Field –
99:59:59.999 --> 99:59:59.999
it's interesting that Temporary Field is
99:59:59.999 --> 99:59:59.999
on this list, right? Temporary Fields can
99:59:59.999 --> 99:59:59.999
be really handy, but sometimes
99:59:59.999 --> 99:59:59.999
they mean that you should have
99:59:59.999 --> 99:59:59.999
made a method with that name, right?
99:59:59.999 --> 99:59:59.999
Why are you giving it a name?
99:59:59.999 --> 99:59:59.999
What is it about the code that you have
99:59:59.999 --> 99:59:59.999
now that feels like it needs that name?
99:59:59.999 --> 99:59:59.999
And so these things are all grouped
99:59:59.999 --> 99:59:59.999
in a category that he called "Tool
99:59:59.999 --> 99:59:59.999
Abusers". I work on a lot of
99:59:59.999 --> 99:59:59.999
bikes, I have a garage full of bikes
99:59:59.999 --> 99:59:59.999
and I'm an amateur mechanic
99:59:59.999 --> 99:59:59.999
which means I have amateur tools
99:59:59.999 --> 99:59:59.999
which sometimes involves a very short
99:59:59.999 --> 99:59:59.999
wrench, a very long pipe, and a
99:59:59.999 --> 99:59:59.999
hammer.
99:59:59.999 --> 99:59:59.999
I can tell you that it almost always
99:59:59.999 --> 99:59:59.999
turns out badly if you abuse your tools.
99:59:59.999 --> 99:59:59.999
Alright, next. This group
99:59:59.999 --> 99:59:59.999
this is stuff that makes change hard.
99:59:59.999 --> 99:59:59.999
So "Divergent Change", "Shotgun
99:59:59.999 --> 99:59:59.999
Surgery" – which we've talked about –
99:59:59.999 --> 99:59:59.999
"Parallel Inheritance Heirarchies"
99:59:59.999 --> 99:59:59.999
which is pretty obvious – sometimes
99:59:59.999 --> 99:59:59.999
you have a couple of heirarchies
99:59:59.999 --> 99:59:59.999
that each have two sides, and
99:59:59.999 --> 99:59:59.999
every time you change something
99:59:59.999 --> 99:59:59.999
you gotta go add or move
99:59:59.999 --> 99:59:59.999
in both sides of the heirarchy.
99:59:59.999 --> 99:59:59.999
These are the kind of things
99:59:59.999 --> 99:59:59.999
that keep you from wanting
99:59:59.999 --> 99:59:59.999
to change code or make code
99:59:59.999 --> 99:59:59.999
hard to change. Now, notice that
99:59:59.999 --> 99:59:59.999
almost everything that I'm talking
99:59:59.999 --> 99:59:59.999
about… if nothing ever changes,
99:59:59.999 --> 99:59:59.999
it's probably okay. Like, code that's
99:59:59.999 --> 99:59:59.999
– really embarrasing code –
99:59:59.999 --> 99:59:59.999
it's fine to keep that really embarrasing
99:59:59.999 --> 99:59:59.999
code, you should be brave about
99:59:59.999 --> 99:59:59.999
your ugly, embarrasing code
99:59:59.999 --> 99:59:59.999
because it is not costing you money
99:59:59.999 --> 99:59:59.999
if it's not changing. And so,
99:59:59.999 --> 99:59:59.999
just because these smells exist,
99:59:59.999 --> 99:59:59.999
you know, sometimes you should
99:59:59.999 --> 99:59:59.999
just, like, own 'em. Be proud.
99:59:59.999 --> 99:59:59.999
Walk away.
99:59:59.999 --> 99:59:59.999
Don't let people make fun of you
99:59:59.999 --> 99:59:59.999
for having bad code.
99:59:59.999 --> 99:59:59.999
This next category…
99:59:59.999 --> 99:59:59.999
"Lazy Class", "Speculative Generality" –
99:59:59.999 --> 99:59:59.999
okay, on top of the effects
99:59:59.999 --> 99:59:59.999
finding the pictures is also fun –
99:59:59.999 --> 99:59:59.999
classes that don't do enough is a
99:59:59.999 --> 99:59:59.999
lazy class, it doesn't justify its
99:59:59.999 --> 99:59:59.999
existence. I'm going to skip Speculative
99:59:59.999 --> 99:59:59.999
Generality for a minute. Data Class,
99:59:59.999 --> 99:59:59.999
you know, we're object-oriented
99:59:59.999 --> 99:59:59.999
programmers, classes oughta have data
99:59:59.999 --> 99:59:59.999
and behaviour. Duplicated Code is
99:59:59.999 --> 99:59:59.999
pretty obvious. Let me go back to
99:59:59.999 --> 99:59:59.999
Speculative Generality.
99:59:59.999 --> 99:59:59.999
I'm going to ask you to raise your hands
99:59:59.999 --> 99:59:59.999
again. Who in here has ever
99:59:59.999 --> 99:59:59.999
written some code for a feature you
99:59:59.999 --> 99:59:59.999
thought might arrive in the future.
99:59:59.999 --> 99:59:59.999
Keep your hands up for a minute.
99:59:59.999 --> 99:59:59.999
Alright, I'm going to out myself
99:59:59.999 --> 99:59:59.999
with you here.
99:59:59.999 --> 99:59:59.999
Who in here has ever, after many
99:59:59.999 --> 99:59:59.999
months of working around that code
99:59:59.999 --> 99:59:59.999
ripped it out and thrown it
99:59:59.999 --> 99:59:59.999
away? chuckles Yeah, okay.
99:59:59.999 --> 99:59:59.999
We are bad guessers, and
99:59:59.999 --> 99:59:59.999
you know, I love OO,
99:59:59.999 --> 99:59:59.999
I love object-oriented design,
99:59:59.999 --> 99:59:59.999
it's a thing that really interests me,
99:59:59.999 --> 99:59:59.999
but this thing of Speculative Generality,
99:59:59.999 --> 99:59:59.999
where we say, I'm going to do something
99:59:59.999 --> 99:59:59.999
really cool in my code for some feature
99:59:59.999 --> 99:59:59.999
I think we might need later…
99:59:59.999 --> 99:59:59.999
this is why people say bad things
99:59:59.999 --> 99:59:59.999
about OO. Right? This is what
99:59:59.999 --> 99:59:59.999
they blame us for. It's primarily
99:59:59.999 --> 99:59:59.999
things in that category.
99:59:59.999 --> 99:59:59.999
You have to be right. The few times
99:59:59.999 --> 99:59:59.999
that you are right have to really
99:59:59.999 --> 99:59:59.999
be big wins that way the enormous
99:59:59.999 --> 99:59:59.999
cost of being wrong – code is read
99:59:59.999 --> 99:59:59.999
many more times than it is written.
99:59:59.999 --> 99:59:59.999
The reason why we cost money
99:59:59.999 --> 99:59:59.999
is the time spent reading code.
99:59:59.999 --> 99:59:59.999
And if you add generality, you
99:59:59.999 --> 99:59:59.999
increase the level of abstraction
99:59:59.999 --> 99:59:59.999
of code. Very often that means
99:59:59.999 --> 99:59:59.999
adding levels of indirection
99:59:59.999 --> 99:59:59.999
which humans are terrible at
99:59:59.999 --> 99:59:59.999
and every time you look at that code
99:59:59.999 --> 99:59:59.999
it means its harder to understand.
99:59:59.999 --> 99:59:59.999
So we should really try to
99:59:59.999 --> 99:59:59.999
restrain ourselves, and not speculate
99:59:59.999 --> 99:59:59.999
about the future. When the new
99:59:59.999 --> 99:59:59.999
requirements come in, they'll tell us
99:59:59.999 --> 99:59:59.999
how we wish we'd written the code
99:59:59.999 --> 99:59:59.999
and we can do it then.
99:59:59.999 --> 99:59:59.999
Dispensables. Sorry, here.
99:59:59.999 --> 99:59:59.999
Ah, see I did that thing with the clicker!
99:59:59.999 --> 99:59:59.999
Dispensables, okay, you've all
99:59:59.999 --> 99:59:59.999
seen that now so we'll move on.
99:59:59.999 --> 99:59:59.999
So the last category here is this group.
99:59:59.999 --> 99:59:59.999
"Feature Envy", "Inappropriate Intimacy",
99:59:59.999 --> 99:59:59.999
"Message Chains", and "Middle Man".
99:59:59.999 --> 99:59:59.999
Feature Envy is when I have an object
99:59:59.999 --> 99:59:59.999
uh, Joe down here is an object,
99:59:59.999 --> 99:59:59.999
Joe's an object that I know about
99:59:59.999 --> 99:59:59.999
and I send him way more messages
99:59:59.999 --> 99:59:59.999
than I send myself. Could be
99:59:59.999 --> 99:59:59.999
that I'm more tightly coupled to
99:59:59.999 --> 99:59:59.999
Joe than I know, right?
99:59:59.999 --> 99:59:59.999
Inappropriate Intimacy would be
99:59:59.999 --> 99:59:59.999
when Joe had a bunch of private
99:59:59.999 --> 99:59:59.999
methods and I reached in and
99:59:59.999 --> 99:59:59.999
got them, okay, that would be bad.
99:59:59.999 --> 99:59:59.999
laughs I really am sorta pushing
99:59:59.999 --> 99:59:59.999
the code of conduct, aren't I?
99:59:59.999 --> 99:59:59.999
I hope no-one was made uncomfortable
99:59:59.999 --> 99:59:59.999
by that. Message Chains – there's
99:59:59.999 --> 99:59:59.999
the Law of Demeter, those violations
99:59:59.999 --> 99:59:59.999
right? You got dots. You send
99:59:59.999 --> 99:59:59.999
messages to something you know about
99:59:59.999 --> 99:59:59.999
and you get a response and you send
99:59:59.999 --> 99:59:59.999
a message to that. If the types
99:59:59.999 --> 99:59:59.999
change across those dot changes
99:59:59.999 --> 99:59:59.999
that's a Message Chain.
99:59:59.999 --> 99:59:59.999
Middle Man is if you have
99:59:59.999 --> 99:59:59.999
an object that you send messages to
99:59:59.999 --> 99:59:59.999
and it's sole purpose in life is to
99:59:59.999 --> 99:59:59.999
forward those messages to somebody else
99:59:59.999 --> 99:59:59.999
maybe you don't need that object.
99:59:59.999 --> 99:59:59.999
These things are grouped together
99:59:59.999 --> 99:59:59.999
in a group called "Couplers"
99:59:59.999 --> 99:59:59.999
because the effect of this
99:59:59.999 --> 99:59:59.999
is that it binds the objects together
99:59:59.999 --> 99:59:59.999
such that even if they're
99:59:59.999 --> 99:59:59.999
beautiful, even if you've tested
99:59:59.999 --> 99:59:59.999
'em, even if they're little
99:59:59.999 --> 99:59:59.999
works of art, you can't ever
99:59:59.999 --> 99:59:59.999
reach in and get one out
99:59:59.999 --> 99:59:59.999
and use it in another context.
99:59:59.999 --> 99:59:59.999
They come as a bundle, all or nothing.
99:59:59.999 --> 99:59:59.999
And so there you go.
99:59:59.999 --> 99:59:59.999
10 minutes and 34 seconds
99:59:59.999 --> 99:59:59.999
everything you need to know about
99:59:59.999 --> 99:59:59.999
code smells.
99:59:59.999 --> 99:59:59.999
Okay, but we're not done.
99:59:59.999 --> 99:59:59.999
Now I'm going to talk about refactoring
99:59:59.999 --> 99:59:59.999
but not very much
99:59:59.999 --> 99:59:59.999
because you just heard a talk
99:59:59.999 --> 99:59:59.999
on refactoring.
99:59:59.999 --> 99:59:59.999
But there's a thing here
99:59:59.999 --> 99:59:59.999
that people don't know,
99:59:59.999 --> 99:59:59.999
that those guys discovered in the 90s,
99:59:59.999 --> 99:59:59.999
that I want you to go away today
99:59:59.999 --> 99:59:59.999
understanding, and it's this:
99:59:59.999 --> 99:59:59.999
refactorings, just like code smells have
99:59:59.999 --> 99:59:59.999
names and they mean very specific
99:59:59.999 --> 99:59:59.999
things, refactorings have names
99:59:59.999 --> 99:59:59.999
they're very specific, and they
99:59:59.999 --> 99:59:59.999
come with recipes. Not hand-wavy
99:59:59.999 --> 99:59:59.999
recipes, very, very specific, concrete
99:59:59.999 --> 99:59:59.999
recipes. Here's one.
99:59:59.999 --> 99:59:59.999
This is page 149 of Martin Fowler's
99:59:59.999 --> 99:59:59.999
book, and it looks like a recipe, right?
99:59:59.999 --> 99:59:59.999
It has numbers down the side.
99:59:59.999 --> 99:59:59.999
There's little optional clauses here
99:59:59.999 --> 99:59:59.999
for situations that might be different in
99:59:59.999 --> 99:59:59.999
your case. You notice that it refers
99:59:59.999 --> 99:59:59.999
to other recipes, here where the
99:59:59.999 --> 99:59:59.999
things are in capital letters, that's
99:59:59.999 --> 99:59:59.999
another whole recipe by itself.
99:59:59.999 --> 99:59:59.999
It's recipes and recipes
99:59:59.999 --> 99:59:59.999
within recipes.
99:59:59.999 --> 99:59:59.999
All of the refactorings work in the
99:59:59.999 --> 99:59:59.999
same way. This is not something –
99:59:59.999 --> 99:59:59.999
we don't wave our hands and say
99:59:59.999 --> 99:59:59.999
"go refactor" – refactoring has a
99:59:59.999 --> 99:59:59.999
very specific definition, it's to
99:59:59.999 --> 99:59:59.999
rearrange code without changing its
99:59:59.999 --> 99:59:59.999
behaviour and all the ways
99:59:59.999 --> 99:59:59.999
in which you can rearrange code are
99:59:59.999 --> 99:59:59.999
already written down, with instructions
99:59:59.999 --> 99:59:59.999
by people who really thought a lot
99:59:59.999 --> 99:59:59.999
about this. And so now you know
99:59:59.999 --> 99:59:59.999
code smells have names, and they're
99:59:59.999 --> 99:59:59.999
real things, and refactorings have names
99:59:59.999 --> 99:59:59.999
and they're real things, and they
99:59:59.999 --> 99:59:59.999
come with recipes. I can give you
99:59:59.999 --> 99:59:59.999
one last bit of news. Every code smell
99:59:59.999 --> 99:59:59.999
maps to the Curative Refactoring Recipe.
99:59:59.999 --> 99:59:59.999
Ponder that for a minute.
99:59:59.999 --> 99:59:59.999
What does that mean?
99:59:59.999 --> 99:59:59.999
Here's a cheat sheet. This one's
99:59:59.999 --> 99:59:59.999
provided by the guys at Industrial
99:59:59.999 --> 99:59:59.999
Logic. Notice at the top that the code
99:59:59.999 --> 99:59:59.999
smell they're talking about is Data
99:59:59.999 --> 99:59:59.999
Clumps. This is a little tiny definition
99:59:59.999 --> 99:59:59.999
of a Data Clump. "[F 81]" is a
99:59:59.999 --> 99:59:59.999
reference to page 81 in Martin
99:59:59.999 --> 99:59:59.999
Fowler's book. The three things
99:59:59.999 --> 99:59:59.999
on the bottom are the
99:59:59.999 --> 99:59:59.999
refactoring recipes that are curative
99:59:59.999 --> 99:59:59.999
for that code smell.
99:59:59.999 --> 99:59:59.999
So this slide, I just blew it up
99:59:59.999 --> 99:59:59.999
so you could see it,
99:59:59.999 --> 99:59:59.999
I just extracted it from this PDF
99:59:59.999 --> 99:59:59.999
which is a couple pages long. It
99:59:59.999 --> 99:59:59.999
cross-references all the code smells
99:59:59.999 --> 99:59:59.999
and refactorings in Martin Fowler's
99:59:59.999 --> 99:59:59.999
book and also a book by
99:59:59.999 --> 99:59:59.999
another guy named Joshua Kerievsky
99:59:59.999 --> 99:59:59.999
called Refactoring to Patterns.
99:59:59.999 --> 99:59:59.999
It turns out that this is all
99:59:59.999 --> 99:59:59.999
you need to know. The problem
99:59:59.999 --> 99:59:59.999
is solved. You do not have to
99:59:59.999 --> 99:59:59.999
reinvent this wheel.
99:59:59.999 --> 99:59:59.999
All you need to know is a few things.
99:59:59.999 --> 99:59:59.999
And many of you –
99:59:59.999 --> 99:59:59.999
at least, my experience –
99:59:59.999 --> 99:59:59.999
the reason I wanted to give this talk
99:59:59.999 --> 99:59:59.999
from my experience teaching is
99:59:59.999 --> 99:59:59.999
somehow a generation has passed
99:59:59.999 --> 99:59:59.999
since all these books were written, and
99:59:59.999 --> 99:59:59.999
that people new to programming
99:59:59.999 --> 99:59:59.999
in the last 15 years –
99:59:59.999 --> 99:59:59.999
I'm a woman of a certain age,
99:59:59.999 --> 99:59:59.999
you can tell –
99:59:59.999 --> 99:59:59.999
if you're new in the last 15 years
99:59:59.999 --> 99:59:59.999
you may not have this information.
99:59:59.999 --> 99:59:59.999
I'm going to show you some code now.
99:59:59.999 --> 99:59:59.999
I'm going to use the last 15-20 minutes
99:59:59.999 --> 99:59:59.999
looking at code, and I'm going to
99:59:59.999 --> 99:59:59.999
show you the practical effect of
99:59:59.999 --> 99:59:59.999
recognizing code smells and doing
99:59:59.999 --> 99:59:59.999
refactorings.
99:59:59.999 --> 99:59:59.999
My class Sale is a subclass of
99:59:59.999 --> 99:59:59.999
Persistence. You can think of that as
99:59:59.999 --> 99:59:59.999
ActiveRecord. If you're in the back it
99:59:59.999 --> 99:59:59.999
won't hurt my feelings if you get up
99:59:59.999 --> 99:59:59.999
and come forward – that's the font
99:59:59.999 --> 99:59:59.999
size of my code.
99:59:59.999 --> 99:59:59.999
I didn't know I was gonna be in the
99:59:59.999 --> 99:59:59.999
keynote, man, it just happened.
99:59:59.999 --> 99:59:59.999
Let's say I have my class Foo that has
99:59:59.999 --> 99:59:59.999
a sales_total method, takes some params,
99:59:59.999 --> 99:59:59.999
and maybe this a controller-like thing
99:59:59.999 --> 99:59:59.999
or something that a controller calls.
99:59:59.999 --> 99:59:59.999
It knows the name of the Sale class and
99:59:59.999 --> 99:59:59.999
it knows some other things, right?
99:59:59.999 --> 99:59:59.999
It knows that Sale understands where
99:59:59.999 --> 99:59:59.999
and it knows that the thing that comes
99:59:59.999 --> 99:59:59.999
back as a response from sending the where
99:59:59.999 --> 99:59:59.999
message knows sum.