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