1 99:59:59,999 --> 99:59:59,999 Hey, I am Sandi Metz and I am the 2 99:59:59,999 --> 99:59:59,999 last but one speaker and that means 3 99:59:59,999 --> 99:59:59,999 you have almost made it. [audience laughter] 4 99:59:59,999 --> 99:59:59,999 You get a break, there's another keynote 5 99:59:59,999 --> 99:59:59,999 if you stay for that – 6 99:59:59,999 --> 99:59:59,999 it's really bright up here, I'm going to have 7 99:59:59,999 --> 99:59:59,999 to look at you some. So, yeah, 8 99:59:59,999 --> 99:59:59,999 you've almost arrived. Even those of you who 9 99:59:59,999 --> 99:59:59,999 are new have almost survived a total RailsConf 10 99:59:59,999 --> 99:59:59,999 and so I think we should – let's do this – 11 99:59:59,999 --> 99:59:59,999 sponsors pay the bills, they do, we're glad. [clapping] 12 99:59:59,999 --> 99:59:59,999 Thank 'em. 13 99:59:59,999 --> 99:59:59,999 But even more than that 14 99:59:59,999 --> 99:59:59,999 "human people" organize this conference 15 99:59:59,999 --> 99:59:59,999 and if you see – this is your job before you 16 99:59:59,999 --> 99:59:59,999 leave today – find someone in one of those 17 99:59:59,999 --> 99:59:59,999 red shirts, and say thank you. 18 99:59:59,999 --> 99:59:59,999 Alright? Let's thank 'em here. 19 99:59:59,999 --> 99:59:59,999 Thank 'em in person. 20 99:59:59,999 --> 99:59:59,999 Going to start my clock, I have about – 21 99:59:59,999 --> 99:59:59,999 I really do have about 40 minutes. 22 99:59:59,999 --> 99:59:59,999 I don't have 500 slides of code this year, 23 99:59:59,999 --> 99:59:59,999 though, so… it's not going to be that bad. 24 99:59:59,999 --> 99:59:59,999 Yeah, let's just go. So 25 99:59:59,999 --> 99:59:59,999 I'm Sandi Metz and I'm really happy to be here 26 99:59:59,999 --> 99:59:59,999 and I have a talk. This years talk is 27 99:59:59,999 --> 99:59:59,999 about code smells. 28 99:59:59,999 --> 99:59:59,999 This is a term invented by this guy. So 29 99:59:59,999 --> 99:59:59,999 it's really, uh, we did not plan that 30 99:59:59,999 --> 99:59:59,999 I would follow Katrina 31 99:59:59,999 --> 99:59:59,999 but it's the perfect talk to follow her 32 99:59:59,999 --> 99:59:59,999 talk. She just mentioned this guy's name. 33 99:59:59,999 --> 99:59:59,999 This guy's name is Kent Beck. This guy's 34 99:59:59,999 --> 99:59:59,999 Martin Fowler, he's the man who wrote 35 99:59:59,999 --> 99:59:59,999 this book, which is the book 36 99:59:59,999 --> 99:59:59,999 that teaches us to do refactoring. 37 99:59:59,999 --> 99:59:59,999 They collaborated together on the 38 99:59:59,999 --> 99:59:59,999 third chapter of that book, and 39 99:59:59,999 --> 99:59:59,999 it's like naming things wins and 40 99:59:59,999 --> 99:59:59,999 the name "code smell", the reason why 41 99:59:59,999 --> 99:59:59,999 you know that term today is because 42 99:59:59,999 --> 99:59:59,999 of what these guys did back in the 1990s. 43 99:59:59,999 --> 99:59:59,999 Now, 44 99:59:59,999 --> 99:59:59,999 I wrote a book a couple of years ago 45 99:59:59,999 --> 99:59:59,999 – before that, I wrote code for 35 years, 46 99:59:59,999 --> 99:59:59,999 I went to my desk every day and 47 99:59:59,999 --> 99:59:59,999 wrote code – and now, I don't do that. 48 99:59:59,999 --> 99:59:59,999 I teach. I teach classes in 49 99:59:59,999 --> 99:59:59,999 object-oriented design. 50 99:59:59,999 --> 99:59:59,999 And in my classes, 51 99:59:59,999 --> 99:59:59,999 I have occasion to ask people 52 99:59:59,999 --> 99:59:59,999 if they've heard of code smells 53 99:59:59,999 --> 99:59:59,999 and just like I suspect all of you 54 99:59:59,999 --> 99:59:59,999 everyone in my class always says 55 99:59:59,999 --> 99:59:59,999 "Oh yeah, we know what code smells 56 99:59:59,999 --> 99:59:59,999 are." And then I ask them 57 99:59:59,999 --> 99:59:59,999 to list five… 58 99:59:59,999 --> 99:59:59,999 and no one can. 59 99:59:59,999 --> 99:59:59,999 Alright, okay, now I can hear 60 99:59:59,999 --> 99:59:59,999 you trying, but really most of you cannot. 61 99:59:59,999 --> 99:59:59,999 I know that, okay. 62 99:59:59,999 --> 99:59:59,999 And so, we all think 63 99:59:59,999 --> 99:59:59,999 we know about this term 64 99:59:59,999 --> 99:59:59,999 but it doesn't mean "I don't like your 65 99:59:59,999 --> 99:59:59,999 code and I can't tell you why." 66 99:59:59,999 --> 99:59:59,999 That is not what a code smell is. 67 99:59:59,999 --> 99:59:59,999 There is some opinion involved 68 99:59:59,999 --> 99:59:59,999 but really, it's not just that I 69 99:59:59,999 --> 99:59:59,999 disagree with how you wrote your code, 70 99:59:59,999 --> 99:59:59,999 the standard is higher. These smells – 71 99:59:59,999 --> 99:59:59,999 these 22 different smells all have 72 99:59:59,999 --> 99:59:59,999 very precise meanings… 73 99:59:59,999 --> 99:59:59,999 and the power, the magic if you will 74 99:59:59,999 --> 99:59:59,999 of this list is they've given things 75 99:59:59,999 --> 99:59:59,999 names. Once you've given a complex idea 76 99:59:59,999 --> 99:59:59,999 a name, if we could just 77 99:59:59,999 --> 99:59:59,999 learn what that name stood for 78 99:59:59,999 --> 99:59:59,999 it means we could just talk to each other 79 99:59:59,999 --> 99:59:59,999 in an unambiguous way without 80 99:59:59,999 --> 99:59:59,999 having miscommunications. And so 81 99:59:59,999 --> 99:59:59,999 one of the things they talk about – 82 99:59:59,999 --> 99:59:59,999 very often when people talk about 83 99:59:59,999 --> 99:59:59,999 code smells, they prefix "code smell" 84 99:59:59,999 --> 99:59:59,999 with the word "bad". 85 99:59:59,999 --> 99:59:59,999 They say "bad smells". 86 99:59:59,999 --> 99:59:59,999 But really, the definition of a code 87 99:59:59,999 --> 99:59:59,999 smell is that it might indicate a problem. 88 99:59:59,999 --> 99:59:59,999 You don't have an obligation 89 99:59:59,999 --> 99:59:59,999 to filter out all the smells, 90 99:59:59,999 --> 99:59:59,999 and it's actually important that you not. 91 99:59:59,999 --> 99:59:59,999 It's also worth getting familiar 92 99:59:59,999 --> 99:59:59,999 with code smells because they have 93 99:59:59,999 --> 99:59:59,999 such great names. 94 99:59:59,999 --> 99:59:59,999 Look at this list. 95 99:59:59,999 --> 99:59:59,999 "Feature Envy" – that's cool. 96 99:59:59,999 --> 99:59:59,999 I like this one, the one that 97 99:59:59,999 --> 99:59:59,999 needs a code of conduct: 98 99:59:59,999 --> 99:59:59,999 "Innappropriate Intimacy". 99 99:59:59,999 --> 99:59:59,999 "Shotgun Surgery", that's another 100 99:59:59,999 --> 99:59:59,999 favourite. 101 99:59:59,999 --> 99:59:59,999 And so, the problem with this list 102 99:59:59,999 --> 99:59:59,999 – first of all, okay, I'll confess – 103 99:59:59,999 --> 99:59:59,999 I had that book for a really long time 104 99:59:59,999 --> 99:59:59,999 before I read it. I had a really 105 99:59:59,999 --> 99:59:59,999 hard time with it. 106 99:59:59,999 --> 99:59:59,999 It's one of those books that's 107 99:59:59,999 --> 99:59:59,999 like a recipe book and I've heard 108 99:59:59,999 --> 99:59:59,999 people who – it would be like reading 109 99:59:59,999 --> 99:59:59,999 a cookbook if you did not eat. 110 99:59:59,999 --> 99:59:59,999 And so I really need the story 111 99:59:59,999 --> 99:59:59,999 arc and I had very hard time 112 99:59:59,999 --> 99:59:59,999 following along, like, persisting 113 99:59:59,999 --> 99:59:59,999 with a bunch of recipes that were just 114 99:59:59,999 --> 99:59:59,999 like etc., etc., etc. 115 99:59:59,999 --> 99:59:59,999 But it turns out 116 99:59:59,999 --> 99:59:59,999 it was really worthwhile 117 99:59:59,999 --> 99:59:59,999 and I finally did it and actually 118 99:59:59,999 --> 99:59:59,999 Katrina made me. I have to say it. 119 99:59:59,999 --> 99:59:59,999 Katrina made me. 120 99:59:59,999 --> 99:59:59,999 So there's 22 things on this list. 121 99:59:59,999 --> 99:59:59,999 I had a Stack Overflow at 22. 122 99:59:59,999 --> 99:59:59,999 It turns out there's a guy who's name 123 99:59:59,999 --> 99:59:59,999 I can't pronounce – "Mäntylä" – it'll 124 99:59:59,999 --> 99:59:59,999 be in the credits, he wrote a paper 125 99:59:59,999 --> 99:59:59,999 where he grouped 'em. He grouped 126 99:59:59,999 --> 99:59:59,999 'em in five different categories, 127 99:59:59,999 --> 99:59:59,999 and through the magic of keynote, 128 99:59:59,999 --> 99:59:59,999 I can do that. 129 99:59:59,999 --> 99:59:59,999 This is the only reason we make 130 99:59:59,999 --> 99:59:59,999 talks, so we can use the effects. 131 99:59:59,999 --> 99:59:59,999 So here, let's just talk about 132 99:59:59,999 --> 99:59:59,999 of things that just do not need 133 99:59:59,999 --> 99:59:59,999 to be that big. 134 99:59:59,999 --> 99:59:59,999 Long methods and large classes 135 99:59:59,999 --> 99:59:59,999 are probably self-explanatory. 136 99:59:59,999 --> 99:59:59,999 Data Clumps is where you have 137 99:59:59,999 --> 99:59:59,999 two or more pieces of data that 138 99:59:59,999 --> 99:59:59,999 always appear together; pass 'em 139 99:59:59,999 --> 99:59:59,999 around and in together. 140 99:59:59,999 --> 99:59:59,999 Long parameter list is obvious. 141 99:59:59,999 --> 99:59:59,999 It might only occur once, but if 142 99:59:59,999 --> 99:59:59,999 it's long enough, there's probably an 143 99:59:59,999 --> 99:59:59,999 object in there somewhere. 144 99:59:59,999 --> 99:59:59,999 And this other wonderfully named 145 99:59:59,999 --> 99:59:59,999 thing called "Primitive Obsession", we saw 146 99:59:59,999 --> 99:59:59,999 an example of that in the talk Katrina 147 99:59:59,999 --> 99:59:59,999 just gave. This is like when you have 148 99:59:59,999 --> 99:59:59,999 an instance of a base class 149 99:59:59,999 --> 99:59:59,999 like a String, or a Number, or a Hash 150 99:59:59,999 --> 99:59:59,999 or an Array, and you pass it 151 99:59:59,999 --> 99:59:59,999 around to a bunch of objects 152 99:59:59,999 --> 99:59:59,999 and they look at it and decide on what 153 99:59:59,999 --> 99:59:59,999 to do based on something that 154 99:59:59,999 --> 99:59:59,999 they know about it. 155 99:59:59,999 --> 99:59:59,999 So you say, "I got a number, 156 99:59:59,999 --> 99:59:59,999 it's six so I'll do thing X" or 157 99:59:59,999 --> 99:59:59,999 "it's eight, I'll do something else." 158 99:59:59,999 --> 99:59:59,999 If only the thing you got was 159 99:59:59,999 --> 99:59:59,999 smarter, you could just send 160 99:59:59,999 --> 99:59:59,999 it messages, so Primitive Obsession 161 99:59:59,999 --> 99:59:59,999 is when the objects are too dumb 162 99:59:59,999 --> 99:59:59,999 that you're passing around. 163 99:59:59,999 --> 99:59:59,999 These things are grouped into 164 99:59:59,999 --> 99:59:59,999 a category called "Bloaters". 165 99:59:59,999 --> 99:59:59,999 They make code bigger than it needs to be 166 99:59:59,999 --> 99:59:59,999 in the places where they use them. 167 99:59:59,999 --> 99:59:59,999 The next group, this one, 168 99:59:59,999 --> 99:59:59,999 these are ideas that are available in 169 99:59:59,999 --> 99:59:59,999 object-oriented programming that you 170 99:59:59,999 --> 99:59:59,999 can misuse. 171 99:59:59,999 --> 99:59:59,999 Switch statements, you know they're 172 99:59:59,999 --> 99:59:59,999 conditionals in normal people talk. 173 99:59:59,999 --> 99:59:59,999 "Refused Bequest" is when it's an 174 99:59:59,999 --> 99:59:59,999 inheritance problem. You have a subclass 175 99:59:59,999 --> 99:59:59,999 that overrides a method that it 176 99:59:59,999 --> 99:59:59,999 inherits from a superclass, and 177 99:59:59,999 --> 99:59:59,999 throws an exception and says 178 99:59:59,999 --> 99:59:59,999 like "I don't implement that thing." 179 99:59:59,999 --> 99:59:59,999 "I refuse the bequest." 180 99:59:59,999 --> 99:59:59,999 "Alternative Classes with Alternative 181 99:59:59,999 --> 99:59:59,999 Interfaces" is pretty obvious. 182 99:59:59,999 --> 99:59:59,999 The other thing is the Temporary Field – 183 99:59:59,999 --> 99:59:59,999 it's interesting that Temporary Field is 184 99:59:59,999 --> 99:59:59,999 on this list, right? Temporary Fields can 185 99:59:59,999 --> 99:59:59,999 be really handy, but sometimes 186 99:59:59,999 --> 99:59:59,999 they mean that you should have 187 99:59:59,999 --> 99:59:59,999 made a method with that name, right? 188 99:59:59,999 --> 99:59:59,999 Why are you giving it a name? 189 99:59:59,999 --> 99:59:59,999 What is it about the code that you have 190 99:59:59,999 --> 99:59:59,999 now that feels like it needs that name? 191 99:59:59,999 --> 99:59:59,999 And so these things are all grouped 192 99:59:59,999 --> 99:59:59,999 in a category that he called "Tool 193 99:59:59,999 --> 99:59:59,999 Abusers". I work on a lot of 194 99:59:59,999 --> 99:59:59,999 bikes, I have a garage full of bikes 195 99:59:59,999 --> 99:59:59,999 and I'm an amateur mechanic 196 99:59:59,999 --> 99:59:59,999 which means I have amateur tools 197 99:59:59,999 --> 99:59:59,999 which sometimes involves a very short 198 99:59:59,999 --> 99:59:59,999 wrench, a very long pipe, and a 199 99:59:59,999 --> 99:59:59,999 hammer. 200 99:59:59,999 --> 99:59:59,999 I can tell you that it almost always 201 99:59:59,999 --> 99:59:59,999 turns out badly if you abuse your tools. 202 99:59:59,999 --> 99:59:59,999 Alright, next. This group 203 99:59:59,999 --> 99:59:59,999 this is stuff that makes change hard. 204 99:59:59,999 --> 99:59:59,999 So "Divergent Change", "Shotgun 205 99:59:59,999 --> 99:59:59,999 Surgery" – which we've talked about – 206 99:59:59,999 --> 99:59:59,999 "Parallel Inheritance Heirarchies" 207 99:59:59,999 --> 99:59:59,999 which is pretty obvious – sometimes 208 99:59:59,999 --> 99:59:59,999 you have a couple of heirarchies 209 99:59:59,999 --> 99:59:59,999 that each have two sides, and 210 99:59:59,999 --> 99:59:59,999 every time you change something 211 99:59:59,999 --> 99:59:59,999 you gotta go add or move 212 99:59:59,999 --> 99:59:59,999 in both sides of the heirarchy. 213 99:59:59,999 --> 99:59:59,999 These are the kind of things 214 99:59:59,999 --> 99:59:59,999 that keep you from wanting 215 99:59:59,999 --> 99:59:59,999 to change code or make code 216 99:59:59,999 --> 99:59:59,999 hard to change. Now, notice that 217 99:59:59,999 --> 99:59:59,999 almost everything that I'm talking 218 99:59:59,999 --> 99:59:59,999 about… if nothing ever changes, 219 99:59:59,999 --> 99:59:59,999 it's probably okay. Like, code that's 220 99:59:59,999 --> 99:59:59,999 – really embarrasing code – 221 99:59:59,999 --> 99:59:59,999 it's fine to keep that really embarrasing 222 99:59:59,999 --> 99:59:59,999 code, you should be brave about 223 99:59:59,999 --> 99:59:59,999 your ugly, embarrasing code 224 99:59:59,999 --> 99:59:59,999 because it is not costing you money 225 99:59:59,999 --> 99:59:59,999 if it's not changing. And so, 226 99:59:59,999 --> 99:59:59,999 just because these smells exist, 227 99:59:59,999 --> 99:59:59,999 you know, sometimes you should 228 99:59:59,999 --> 99:59:59,999 just, like, own 'em. Be proud. 229 99:59:59,999 --> 99:59:59,999 Walk away. 230 99:59:59,999 --> 99:59:59,999 Don't let people make fun of you 231 99:59:59,999 --> 99:59:59,999 for having bad code. 232 99:59:59,999 --> 99:59:59,999 This next category… 233 99:59:59,999 --> 99:59:59,999 "Lazy Class", "Speculative Generality" – 234 99:59:59,999 --> 99:59:59,999 okay, on top of the effects 235 99:59:59,999 --> 99:59:59,999 finding the pictures is also fun – 236 99:59:59,999 --> 99:59:59,999 classes that don't do enough is a 237 99:59:59,999 --> 99:59:59,999 lazy class, it doesn't justify its 238 99:59:59,999 --> 99:59:59,999 existence. I'm going to skip Speculative 239 99:59:59,999 --> 99:59:59,999 Generality for a minute. Data Class, 240 99:59:59,999 --> 99:59:59,999 you know, we're object-oriented 241 99:59:59,999 --> 99:59:59,999 programmers, classes oughta have data 242 99:59:59,999 --> 99:59:59,999 and behaviour. Duplicated Code is 243 99:59:59,999 --> 99:59:59,999 pretty obvious. Let me go back to 244 99:59:59,999 --> 99:59:59,999 Speculative Generality. 245 99:59:59,999 --> 99:59:59,999 I'm going to ask you to raise your hands 246 99:59:59,999 --> 99:59:59,999 again. Who in here has ever 247 99:59:59,999 --> 99:59:59,999 written some code for a feature you 248 99:59:59,999 --> 99:59:59,999 thought might arrive in the future. 249 99:59:59,999 --> 99:59:59,999 Keep your hands up for a minute. 250 99:59:59,999 --> 99:59:59,999 Alright, I'm going to out myself 251 99:59:59,999 --> 99:59:59,999 with you here. 252 99:59:59,999 --> 99:59:59,999 Who in here has ever, after many 253 99:59:59,999 --> 99:59:59,999 months of working around that code 254 99:59:59,999 --> 99:59:59,999 ripped it out and thrown it 255 99:59:59,999 --> 99:59:59,999 away? chuckles Yeah, okay. 256 99:59:59,999 --> 99:59:59,999 We are bad guessers, and 257 99:59:59,999 --> 99:59:59,999 you know, I love OO, 258 99:59:59,999 --> 99:59:59,999 I love object-oriented design, 259 99:59:59,999 --> 99:59:59,999 it's a thing that really interests me, 260 99:59:59,999 --> 99:59:59,999 but this thing of Speculative Generality, 261 99:59:59,999 --> 99:59:59,999 where we say, I'm going to do something 262 99:59:59,999 --> 99:59:59,999 really cool in my code for some feature 263 99:59:59,999 --> 99:59:59,999 I think we might need later… 264 99:59:59,999 --> 99:59:59,999 this is why people say bad things 265 99:59:59,999 --> 99:59:59,999 about OO. Right? This is what 266 99:59:59,999 --> 99:59:59,999 they blame us for. It's primarily 267 99:59:59,999 --> 99:59:59,999 things in that category. 268 99:59:59,999 --> 99:59:59,999 You have to be right. The few times 269 99:59:59,999 --> 99:59:59,999 that you are right have to really 270 99:59:59,999 --> 99:59:59,999 be big wins that way the enormous 271 99:59:59,999 --> 99:59:59,999 cost of being wrong – code is read 272 99:59:59,999 --> 99:59:59,999 many more times than it is written. 273 99:59:59,999 --> 99:59:59,999 The reason why we cost money 274 99:59:59,999 --> 99:59:59,999 is the time spent reading code. 275 99:59:59,999 --> 99:59:59,999 And if you add generality, you 276 99:59:59,999 --> 99:59:59,999 increase the level of abstraction 277 99:59:59,999 --> 99:59:59,999 of code. Very often that means 278 99:59:59,999 --> 99:59:59,999 adding levels of indirection 279 99:59:59,999 --> 99:59:59,999 which humans are terrible at 280 99:59:59,999 --> 99:59:59,999 and every time you look at that code 281 99:59:59,999 --> 99:59:59,999 it means its harder to understand. 282 99:59:59,999 --> 99:59:59,999 So we should really try to 283 99:59:59,999 --> 99:59:59,999 restrain ourselves, and not speculate 284 99:59:59,999 --> 99:59:59,999 about the future. When the new 285 99:59:59,999 --> 99:59:59,999 requirements come in, they'll tell us 286 99:59:59,999 --> 99:59:59,999 how we wish we'd written the code 287 99:59:59,999 --> 99:59:59,999 and we can do it then. 288 99:59:59,999 --> 99:59:59,999 Dispensables. Sorry, here. 289 99:59:59,999 --> 99:59:59,999 Ah, see I did that thing with the clicker! 290 99:59:59,999 --> 99:59:59,999 Dispensables, okay, you've all 291 99:59:59,999 --> 99:59:59,999 seen that now so we'll move on. 292 99:59:59,999 --> 99:59:59,999 So the last category here is this group. 293 99:59:59,999 --> 99:59:59,999 "Feature Envy", "Inappropriate Intimacy", 294 99:59:59,999 --> 99:59:59,999 "Message Chains", and "Middle Man". 295 99:59:59,999 --> 99:59:59,999 Feature Envy is when I have an object 296 99:59:59,999 --> 99:59:59,999 uh, Joe down here is an object, 297 99:59:59,999 --> 99:59:59,999 Joe's an object that I know about 298 99:59:59,999 --> 99:59:59,999 and I send him way more messages 299 99:59:59,999 --> 99:59:59,999 than I send myself. Could be 300 99:59:59,999 --> 99:59:59,999 that I'm more tightly coupled to 301 99:59:59,999 --> 99:59:59,999 Joe than I know, right? 302 99:59:59,999 --> 99:59:59,999 Inappropriate Intimacy would be 303 99:59:59,999 --> 99:59:59,999 when Joe had a bunch of private 304 99:59:59,999 --> 99:59:59,999 methods and I reached in and 305 99:59:59,999 --> 99:59:59,999 got them, okay, that would be bad. 306 99:59:59,999 --> 99:59:59,999 laughs I really am sorta pushing 307 99:59:59,999 --> 99:59:59,999 the code of conduct, aren't I? 308 99:59:59,999 --> 99:59:59,999 I hope no-one was made uncomfortable 309 99:59:59,999 --> 99:59:59,999 by that. Message Chains – there's 310 99:59:59,999 --> 99:59:59,999 the Law of Demeter, those violations 311 99:59:59,999 --> 99:59:59,999 right? You got dots. You send 312 99:59:59,999 --> 99:59:59,999 messages to something you know about 313 99:59:59,999 --> 99:59:59,999 and you get a response and you send 314 99:59:59,999 --> 99:59:59,999 a message to that. If the types 315 99:59:59,999 --> 99:59:59,999 change across those dot changes 316 99:59:59,999 --> 99:59:59,999 that's a Message Chain. 317 99:59:59,999 --> 99:59:59,999 Middle Man is if you have 318 99:59:59,999 --> 99:59:59,999 an object that you send messages to 319 99:59:59,999 --> 99:59:59,999 and it's sole purpose in life is to 320 99:59:59,999 --> 99:59:59,999 forward those messages to somebody else 321 99:59:59,999 --> 99:59:59,999 maybe you don't need that object. 322 99:59:59,999 --> 99:59:59,999 These things are grouped together 323 99:59:59,999 --> 99:59:59,999 in a group called "Couplers" 324 99:59:59,999 --> 99:59:59,999 because the effect of this 325 99:59:59,999 --> 99:59:59,999 is that it binds the objects together 326 99:59:59,999 --> 99:59:59,999 such that even if they're 327 99:59:59,999 --> 99:59:59,999 beautiful, even if you've tested 328 99:59:59,999 --> 99:59:59,999 'em, even if they're little 329 99:59:59,999 --> 99:59:59,999 works of art, you can't ever 330 99:59:59,999 --> 99:59:59,999 reach in and get one out 331 99:59:59,999 --> 99:59:59,999 and use it in another context. 332 99:59:59,999 --> 99:59:59,999 They come as a bundle, all or nothing. 333 99:59:59,999 --> 99:59:59,999 And so there you go. 334 99:59:59,999 --> 99:59:59,999 10 minutes and 34 seconds 335 99:59:59,999 --> 99:59:59,999 everything you need to know about 336 99:59:59,999 --> 99:59:59,999 code smells. 337 99:59:59,999 --> 99:59:59,999 Okay, but we're not done. 338 99:59:59,999 --> 99:59:59,999 Now I'm going to talk about refactoring 339 99:59:59,999 --> 99:59:59,999 but not very much 340 99:59:59,999 --> 99:59:59,999 because you just heard a talk 341 99:59:59,999 --> 99:59:59,999 on refactoring. 342 99:59:59,999 --> 99:59:59,999 But there's a thing here 343 99:59:59,999 --> 99:59:59,999 that people don't know, 344 99:59:59,999 --> 99:59:59,999 that those guys discovered in the 90s, 345 99:59:59,999 --> 99:59:59,999 that I want you to go away today 346 99:59:59,999 --> 99:59:59,999 understanding, and it's this: 347 99:59:59,999 --> 99:59:59,999 refactorings, just like code smells have 348 99:59:59,999 --> 99:59:59,999 names and they mean very specific 349 99:59:59,999 --> 99:59:59,999 things, refactorings have names 350 99:59:59,999 --> 99:59:59,999 they're very specific, and they 351 99:59:59,999 --> 99:59:59,999 come with recipes. Not hand-wavy 352 99:59:59,999 --> 99:59:59,999 recipes, very, very specific, concrete 353 99:59:59,999 --> 99:59:59,999 recipes. Here's one. 354 99:59:59,999 --> 99:59:59,999 This is page 149 of Martin Fowler's 355 99:59:59,999 --> 99:59:59,999 book, and it looks like a recipe, right? 356 99:59:59,999 --> 99:59:59,999 It has numbers down the side. 357 99:59:59,999 --> 99:59:59,999 There's little optional clauses here 358 99:59:59,999 --> 99:59:59,999 for situations that might be different in 359 99:59:59,999 --> 99:59:59,999 your case. You notice that it refers 360 99:59:59,999 --> 99:59:59,999 to other recipes, here where the 361 99:59:59,999 --> 99:59:59,999 things are in capital letters, that's 362 99:59:59,999 --> 99:59:59,999 another whole recipe by itself. 363 99:59:59,999 --> 99:59:59,999 It's recipes and recipes 364 99:59:59,999 --> 99:59:59,999 within recipes. 365 99:59:59,999 --> 99:59:59,999 All of the refactorings work in the 366 99:59:59,999 --> 99:59:59,999 same way. This is not something – 367 99:59:59,999 --> 99:59:59,999 we don't wave our hands and say 368 99:59:59,999 --> 99:59:59,999 "go refactor" – refactoring has a 369 99:59:59,999 --> 99:59:59,999 very specific definition, it's to 370 99:59:59,999 --> 99:59:59,999 rearrange code without changing its 371 99:59:59,999 --> 99:59:59,999 behaviour and all the ways 372 99:59:59,999 --> 99:59:59,999 in which you can rearrange code are 373 99:59:59,999 --> 99:59:59,999 already written down, with instructions 374 99:59:59,999 --> 99:59:59,999 by people who really thought a lot 375 99:59:59,999 --> 99:59:59,999 about this. And so now you know 376 99:59:59,999 --> 99:59:59,999 code smells have names, and they're 377 99:59:59,999 --> 99:59:59,999 real things, and refactorings have names 378 99:59:59,999 --> 99:59:59,999 and they're real things, and they 379 99:59:59,999 --> 99:59:59,999 come with recipes. I can give you 380 99:59:59,999 --> 99:59:59,999 one last bit of news. Every code smell 381 99:59:59,999 --> 99:59:59,999 maps to the Curative Refactoring Recipe. 382 99:59:59,999 --> 99:59:59,999 Ponder that for a minute. 383 99:59:59,999 --> 99:59:59,999 What does that mean? 384 99:59:59,999 --> 99:59:59,999 Here's a cheat sheet. This one's 385 99:59:59,999 --> 99:59:59,999 provided by the guys at Industrial 386 99:59:59,999 --> 99:59:59,999 Logic. Notice at the top that the code 387 99:59:59,999 --> 99:59:59,999 smell they're talking about is Data 388 99:59:59,999 --> 99:59:59,999 Clumps. This is a little tiny definition 389 99:59:59,999 --> 99:59:59,999 of a Data Clump. "[F 81]" is a 390 99:59:59,999 --> 99:59:59,999 reference to page 81 in Martin 391 99:59:59,999 --> 99:59:59,999 Fowler's book. The three things 392 99:59:59,999 --> 99:59:59,999 on the bottom are the 393 99:59:59,999 --> 99:59:59,999 refactoring recipes that are curative 394 99:59:59,999 --> 99:59:59,999 for that code smell. 395 99:59:59,999 --> 99:59:59,999 So this slide, I just blew it up 396 99:59:59,999 --> 99:59:59,999 so you could see it, 397 99:59:59,999 --> 99:59:59,999 I just extracted it from this PDF 398 99:59:59,999 --> 99:59:59,999 which is a couple pages long. It 399 99:59:59,999 --> 99:59:59,999 cross-references all the code smells 400 99:59:59,999 --> 99:59:59,999 and refactorings in Martin Fowler's 401 99:59:59,999 --> 99:59:59,999 book and also a book by 402 99:59:59,999 --> 99:59:59,999 another guy named Joshua Kerievsky 403 99:59:59,999 --> 99:59:59,999 called Refactoring to Patterns. 404 99:59:59,999 --> 99:59:59,999 It turns out that this is all 405 99:59:59,999 --> 99:59:59,999 you need to know. The problem 406 99:59:59,999 --> 99:59:59,999 is solved. You do not have to 407 99:59:59,999 --> 99:59:59,999 reinvent this wheel. 408 99:59:59,999 --> 99:59:59,999 All you need to know is a few things. 409 99:59:59,999 --> 99:59:59,999 And many of you – 410 99:59:59,999 --> 99:59:59,999 at least, my experience – 411 99:59:59,999 --> 99:59:59,999 the reason I wanted to give this talk 412 99:59:59,999 --> 99:59:59,999 from my experience teaching is 413 99:59:59,999 --> 99:59:59,999 somehow a generation has passed 414 99:59:59,999 --> 99:59:59,999 since all these books were written, and 415 99:59:59,999 --> 99:59:59,999 that people new to programming 416 99:59:59,999 --> 99:59:59,999 in the last 15 years – 417 99:59:59,999 --> 99:59:59,999 I'm a woman of a certain age, 418 99:59:59,999 --> 99:59:59,999 you can tell – 419 99:59:59,999 --> 99:59:59,999 if you're new in the last 15 years 420 99:59:59,999 --> 99:59:59,999 you may not have this information. 421 99:59:59,999 --> 99:59:59,999 I'm going to show you some code now. 422 99:59:59,999 --> 99:59:59,999 I'm going to use the last 15-20 minutes 423 99:59:59,999 --> 99:59:59,999 looking at code, and I'm going to 424 99:59:59,999 --> 99:59:59,999 show you the practical effect of 425 99:59:59,999 --> 99:59:59,999 recognizing code smells and doing 426 99:59:59,999 --> 99:59:59,999 refactorings. 427 99:59:59,999 --> 99:59:59,999 My class Sale is a subclass of 428 99:59:59,999 --> 99:59:59,999 Persistence. You can think of that as 429 99:59:59,999 --> 99:59:59,999 ActiveRecord. If you're in the back it 430 99:59:59,999 --> 99:59:59,999 won't hurt my feelings if you get up 431 99:59:59,999 --> 99:59:59,999 and come forward – that's the font 432 99:59:59,999 --> 99:59:59,999 size of my code. 433 99:59:59,999 --> 99:59:59,999 I didn't know I was gonna be in the 434 99:59:59,999 --> 99:59:59,999 keynote, man, it just happened. 435 99:59:59,999 --> 99:59:59,999 Let's say I have my class Foo that has 436 99:59:59,999 --> 99:59:59,999 a sales_total method, takes some params, 437 99:59:59,999 --> 99:59:59,999 and maybe this a controller-like thing 438 99:59:59,999 --> 99:59:59,999 or something that a controller calls. 439 99:59:59,999 --> 99:59:59,999 It knows the name of the Sale class and 440 99:59:59,999 --> 99:59:59,999 it knows some other things, right? 441 99:59:59,999 --> 99:59:59,999 It knows that Sale understands where 442 99:59:59,999 --> 99:59:59,999 and it knows that the thing that comes 443 99:59:59,999 --> 99:59:59,999 back as a response from sending the where 444 99:59:59,999 --> 99:59:59,999 message knows sum.