 So I'm Sandy Metz. I wrote a book a couple of years ago sort of by accident. I was I was made to write it And then after I wrote it people started reading it It was and it was a little it was very confusing to me actually when I was in the process of writing it I couldn't get anybody to read it People would ask me for sample chapters, and then they would go away And I would never hear from them again They would they would actually quit making eye contact with me when I when I saw them out in public And so I had no reason to believe that it would ever get read and then and then when I got published People did read it and it was a huge surprise to me them and I Started getting asked to speak at conferences and teach and that that made my schedule so busy that I had to quit my job And I didn't have a way to make a living and So I finally got so many requests to start teaching courses that I made up a course I made up a little short object-oriented design course and I started traveling and teaching that course When I Realized that my job like like I wrote code every day for 35 years I went to my desk. I had a day job just like you I wrote code And and then suddenly after the book was published I made this transition so that my job became to think about writing code to think about what made code good excuse me to think about How to write good code and then also to think about how to explain how to write good code how to teach people how to write good code and I found that I was When I made the course I picked what I thought were Discrete lessons in object-oriented design. It's a three-day class and I thought all the lessons were different and Then when I started teaching it, I've taught it over and over again for the last year and I finally After much exposure to the material. I finally realized that I wasn't teaching a bunch of different lessons I was just teaching one simple thing one tiny simple idea about object-oriented design and That all things are really this one thing And so today's talk is an outgrowth of my experience in the last year of teaching that course I'm gonna tell you it so I'm gonna tell you everything I've learned in the last 18 months in 30 minutes or so and it's gonna take me about 500 slides It's mostly code You're gonna love it. You haven't seen enough code today. It's a lot of it's a lot of code. You'll enjoy it so I Had to decide I was a very oh When I keep if anything really funny comes up on the captions yell at me and make me turn around and look at it Okay, I'm not gonna do the Aaron thing. Where's Aaron so so When the book came out it would it was really obvious to me that I had a different idea about objects and many of The people in the Ruby community that my understanding of objects was different and I I was curious about why that was I was trying to figure out like why is it that this it seemed okay? Let me say this. You know how the things you know seem obvious to you Everything in that book seemed obvious to me Seem like it would be boring to people and when I discovered that it wasn't I wanted to understand what it was about my experience That made me think in a different way about oh, oh, and I decided it was because I'm infected by small talk I wrote I've been writing Ruby code since 2005 and I have not yet written Ruby for as many years as I wrote small talk I wrote small talk apps for my day job for more than 12 years and so there's You know how the languages that you know shape the way you can think about problems So the way I think about oh is shaped by small talk and I'm gonna show you I'm gonna start off This talks in four parts. I'm gonna in this first part I'm gonna show you a thing about small talk that would shape the way you you think about oh, oh had you come through small talk and So and all the code I'm gonna show you is a Ruby. So don't be alarmed. We're only gonna look at Ruby code So, you know that right everybody's used to that. We know how that works You may or may not be familiar with this method the send method If you look at it, you can guess what it does right send is gonna invoke the whatever this passes an argument that symbol It's gonna invoke that message on the object and it does the same thing these things to do the same thing And so you've probably done this in Ruby And you're not surprised to see that one plus one gives you back to but you can also do this So have a look at that syntax for a minute So this is I'm gonna send the message Indicated by that symbol plus and I'm gonna pass an argument one and it does the same thing So now what is that? Well, you know, it's a fix and I'm in fix nums. No things and Among the list of the things they know is this And so it turns out in an OO language like Ruby This is a truth at the bottom of all things Plus is a method and it's invoked by sending a message That other syntax the syntax where you can say where you can write it so that it looks like math is Just syntactic sugar It's something that Matt's gave you so that you would feel more comfortable, but about how the code looked He did because he's a nice guy and he's sitting right here And so you might notice here that there's also this method And so I shouldn't have to convince you now that I'm sending the message equal equal to one passing one as an argument And that gives back true And well, what it what is true? Well, it's a singleton instance of true class It's a variable now. I'm gonna make up some things. Some of this stuff is useful lies But just believe me. Okay, so let's let's agree that true is a global variable that's set when Ruby boots up And it gets an instance of true class and it puts it in that variable so that you can refer to it and Use it to compare to things and so if true is a class what it knows things just like everything else It's they're all the same now again Some of these things are a bit more nuanced than what I'm saying But let's let's accept the useful lie for the purposes of this talk When I came to Ruby and I saw this I Was very comfortable with it. Sure. True is an instance of true class Just like false is an instance of false class and nil is an instance of nil class. I totally expected my small talk experience Made me expect the language to behave that way But there was something to me about Ruby that when I came to Ruby that really really confused me and it's that there was a special syntax For dealing with that object. There was a special syntax for dealing with true and dealing with false And to show you let me show you that syntax. So in small talk small This is the entire list of small talk keywords All of the keywords in small talk no matter how many times you count them There were still only be six Here's Ruby at least as of when this slide was produced And if you look at this you'll notice that special syntax For dealing with Booleans and now you take this keyword for granted so that it it's almost impossible for me to explain how it works Cory's gonna do a talk later that drives how it works, but I'm gonna attempt I'm gonna explain to you how this keyword works first it evaluates that expression and Then based on the result of it It's gonna evaluate the code in the block before the keyword else Or the code in the block after the keyword else Now it sounds ridiculous to even say that because you're so used to seeing it and So that's what's going on, but of course it's it's not quite right because in Ruby we have truthy Right really what we say is if it's truthy Evaluate the top part the part the first part before the else otherwise evaluate the part after the else and this is a type check This is absolutely a type check. It says if you're some object whose type. I know I will do one thing Otherwise, I will do another and I and we don't do type checks in OO And so I found the syntax very very surprising It's and it's not and I found it unnecessary. We don't need this syntax in order to do conditionals And so that's a challenge for you think about that for a minute. How can you do conditionals? In a language where you just have objects and messages if you don't have special syntax Have you ever even considered that? If you know don't tell So let's write some code Well here. Let me say this. This is what I want to do. I write OO. I want to send messages to objects and so If you when when this statement exists when this syntax exists I contend to you that it enables you it enables a procedural mindset and if you came from an OO language Strict OO language, maybe you don't get trapped in that mindset But if you came the other way like many of us did if you came from Python or JavaScript If you're a self-taught programmer like I was you could easily get in a situation where you came from a language Where you wrote piles of long conditionals and they they port directly into Ruby Because you can see this keyword. It implies to you that it that you can that it's a good thing to do What I want to do here is prove to you that it's not necessary. So let's write Message sending syntax to handle Booleans in Ruby You're gonna love this first. Let's break open true class And I'm gonna implement a new API. It's got two methods in it. One is if true Now this takes advantage of the fact that there's all the methods get an implicit block So the if true method in true class is gonna yield to the block. It's gonna evaluate the block now We're gonna return self. Don't worry about that right now. It'll be clear in a minute why that's true The other half of this API is the if false method it true class ignores the block in the if false method How does false behave? What do you would have what is the implementation of this API in false class? Well, it's just the opposite So true yields on true false yields on false And so if you write this code if you crack open these base classes and monkey patch them in this way Now you can do this So that's gonna evaluate to true if I send it the if true message remember that it yields to the block you see that output If I send if false to something that's true remember that does nothing so nothing happens The falses are they work just the opposite right if you send if true to a false it ignores the block If you send if false to a false that evaluates the block So That kind of does it but of course it's not quite right because this is true and false not truthy But it's really easy once we're here. It's really easy to get all the way up to truth I can just compound my sense by promoting this up to object and duplicating this in nil and Now if you had this code Okay, really let me put that credit back you who to cats a blog post from 2009 blesses hard I tried a couple of implementations of this, but I didn't like anything. I did better than what he did So here now it's not just true, but I can put anything in there everything is true except nil and false and nil is also nil just like false is now false and So that's all it takes and now instead of using this syntax I could do that and Now this this explains why we return self it so we can chain them So I can get the true and the false branch in there together and the same with false All right, you don't need a special syntax to deal with Booleans. You can just send messages to objects Now having shown you this I Am not suggesting that we do it I'm not I don't want to do this. We don't need to do it in Ruby Don't go make a gym. Don't tweet that. I said it don't create a gym that does this We don't we don't need that what I want to do is change how you think about objects I want you to imagine what the world would look the world of objects would look like to you if you did if you Couldn't write conditionals if you were raised up in a language that really pushed you into sending messages to objects What would you do if there were no if statements and the fact that I was taught oh by a language that didn't have an if statement meant that I am I'm extremely condition averse I Hate conditions. I'm very condition averse in a condition that I really hate is this one All right This is the there's an animal class. It has a factory method called find Which takes an ID and looks up an object if the object is there you get a you get an animal back There's an if there's an animal with a key pig you get it back if you pass it key It doesn't know you get no back Maybe someone gives you an array of stuff like this and you innocently Find over it and then you start you get this back and you start talking to them and then come boom I want to kind of want to wait here a minute and see what that looks like on that. Yeah, there we go Okay So so before we go on and I want to concede that sometimes nil is nothing Sometimes nil is nothing right and when it's nothing what you want to do here is you want to throw it away You want to compact it out of the array, but if you're sending it a message Yeah, you know, I'm a little dyslexic and all day long I felt like I was the one typing those captions Yeah, so you want to so here we want to throw it away But if you're sending it a message when you're trying to talk to these things there's something and in this case In in this case we want to fix it so it doesn't blow up on nil and it's really easy to imagine There's a thing that we often do here Especially if you're a Rails programmer and you got that user object that's sometimes nil and sometimes a user and you want to say guest On the screen right you want to say welcome guest or welcome whoever it is. What do you do you do this? Right here you put this is a long form of it You put some kind of conditional right there where the center of the messages that gets back the object that doesn't conform to the Right API So if you do that that works and it's nice because I can say guest or no animal there But of course we're Ruby programmers and that's not pretty enough. We would never really do that, right? We're gonna maybe use truthy That makes it look nicer, but then of course I lose my ability to insert my own value there I'll get the empty string back But of course if you're a rails programmer you got try You got try right you're gonna say try here now. I'm not hating on try I use try myself, but but let's be honest Let's be honest about what try does here the problem that try solves, right? This is try is really this which is really a check on nil which is really a type check on nil class And if you write it down really if you make if you form it as a conditional instead of the turner operator It's really it's actually even worse because I want that I want I want to supply some behavior in there and that looks like that and that's what I just told you I hated in the first half All right, but it's even worse here because I'm not really saying an object who's type I know I'm saying if I know who you are if you're a certain kind of object I'll supply some behavior Otherwise, I'm going to send you a message That's what this if statement says and the problem with conditions Is that they just get worse as soon as in this if you make the sender have that condition pretty soon you have this This everywhere, okay, I have a policy not to swear on stage And that means that none of the best words are available to me here This is just crazy right this is a maintenance nightmare and the day you have to change the value of that you end up with a code smell They call shotgun surgery right to change a single concept. You have to go make edits in a bunch of different places So we hate that this is a terrible thing. It's very non. Oh Instead of being conditioned a verse what I am is message centric I just want to send a message to an object and the problem I have here is that the objects on the list that I get back conformed to different API's and We've grown to accept this with nil Like you'll let people give you a list of animals back that occasionally contains a nil And I and I know that if like they give you animals and every now and then they gave you a carrot Right if it was a carrot or a truck you'd be up in arms about it But somehow we accept nil as if it's a thing that means something even when we're sending it messages So here what I want to do is just that I just want to be able to send that and so I'm gonna I'm just gonna write the code. I wish I had What I need and so animal works fine right now animal nose dame that totally works what I need Nil doesn't conform to the right API. I want this Instead of nil I need to get something back that I can send name to that will return that value And I'm just I'm just gonna give it a name. That's the object I wish I had and once I can conceive of writing that I Can so so this brings me really to the first big idea here. I would rather I Would rather know about a new object and duplicate behavior And so if I can if I can think of creating that object then I can use bar bar and I can use the short circuit operator there and Plug it in right here. I can substitute it in for nil and if I do that then this turns into that Now what has happened? I? Added a new dependency. I can see that I absolutely did And look I'm hating on conditions and I still have one I Didn't remove that but the code has gotten better in one way And it said I no longer own the behavior that behavior is moved somewhere else And that means I don't I don't need all this code all the code in the condition goes away Which means I can do that and thankfully now I can put it up there I can put it all in one line and when I talk to it I get exactly the right answers everything just works and So this technique where you swap in where you substitute a smarter object in for no it has it actually has a name It's the nil object pattern There's a guy named Joshua Kariyevsky who wrote a book called Refactoring to patterns and he quotes another guy who I've never heard of Bruce Anderson I don't know who Bruce Anderson is but Bruce Anderson calls this the active nothing The nil object pattern. It's a beautiful term right the active nothing and so Using the nil object pattern it lets you depend on knowing the name of the class Rather than the contents of a string and in just a minute ago. I said I'd rather depend on knowing an object Rather than behavior, but I'd rather depend I'd rather know an object rather than duplicate the behavior But there's a corollary to this and it's that I don't want to know very many objects And so but once you get to the point where you can create that class well here So the problem now, what have I changed that I've swapped my problem of knowing the string for knowing the Know in the class right but now that you're here. It's really easy to fix this problem I have this untrustworthy external API unreliable it Returns lists that contain things that conform to different APIs and I can just wrap it in my own class I can grab that message in for it and I can put the statement right here. I like to call this So if animal find is an interface right it's code assume it's code. You can't change it belongs to someone else Guaranteed animal find is your outer face So you wrap the interface of another system with an outer face at the boundary of your app and then all your code Can call your method And so you can't really get it's a kind of a lie that you can get rid of the conditional Somebody has to know but you can put it in just one place in your app so that you can change it And if you do that you get this back And then you can talk to them all and it just works So this is a dramatic improvement and it it absolutely decreases and decreases the complexity of the code the null object pattern So It took me a long time in my programming life to realize that the null object pattern is just a single concrete instance of a much larger abstraction and That's what the rest of this talk is about now if I lose you I'm not saying that I will But if if the rest of this talk If you don't get it all don't worry because the no logic pattern is a big deal and just getting this part of it Will improve your code So but but see once you understand that a logic pattern it opens the door to seeking this larger Abstraction and in order to illustrate this I'm gonna need a new example. So let me just introduce that example. I don't know Do you know if you grew up here in Spain or Barcelona? Do you know the house that Jack built? Okay, no So it's a thing that we learn in America kids learn it's sort of a nursery rhyme And it's it's a cute what we what they call cumulative tail So this was how line one goes you say this is the house that Jack built and then the next line adds a new part This is the mall that lay in the house that Jack built and then there'll be another part This is the rat that ate the mall that lay in the house that Jack built and it there's 12 parts and The 12th line looks like this viewer to recite the whole tail and so it's a memorization cumulative kind of thing it's nice because it's algorithmic and it they It means that we can teach programming lessons without having to resort to like bank loans Because which are really boring actually I took this recipe that you're well, I'm not gonna go there There's a recipe in Kara Efsky's book that I that used bank loans that I had to switch for the song All right if it would if I task you for creating the ruby code to produce this tail without and While you without duplicating any strings you might do something like this. Here's what it would look like Maybe you create a ray and you put all the bits in it Put that in a method Maybe you make a phrases method that takes a number number is the line that I want to output So in this case You may know the last method on array like if you send the last two array you get the last thing back You may not know that array takes an argument so I can get the last three things back So here if I pass three I would get those last three rat that ate malt the land house that Jack built You know what join does I need? Some way to get a whole line this puts the this is on the front and the period on the end I Need some way to recite the whole tail So I'll loop for as many times as I have bits and then run the cumulative the accumulation and then I'll put it on a class This code works. This absolutely works if I get a I can print line one or I can print line two or I can print line three or I can print the whole thing and I can recite the tail and So all well and good and so let's imagine you write this code and you give it to your customers because I don't know Why they want they want it right they have a website that does songs and they like it so much that the next thing they do Actually, let me ask you what happens as soon as you deliver a feature that works Yeah, they ask you for something else right and so here the next thing they ask you for is something I'm gonna call random house and so You have to keep house. This does not replace house This is in addition to house and here's how random house works What they want is they want you to take this list and one time before you start producing the tail They want you to randomize it They want you to shake it up and so it like for example, this is just one way it could have been randomized Notice here that this one ends with a rat that ate that made an awful and a milk the cat that killed All right, and so this version if you printed out the random version of this tail the line one would be This is the rat that ate this is made an awful on the cat that killed me And the whole thing looks like this Okay, so You guys all have way better English than I have Spanish So I wouldn't be surprised at all if you've noticed that some parts of this tail are not safe for work if you randomize it and so Every time you randomize it something new and scary happens. That's all I can say. It's not my fault It's not my fault. So so here So let's imagine that you're trying to do random house and I told you I want you to write random house But you cannot use an if statement can't break house Can't use an if statement so you might consider using inheritance And so let's write let's do a little inheritance here right here it is I can some class house I can override data I Can call shuffles the method on array if you don't know that it randomizes the contents of an array And I have to cache it because I want to just shuffle it once and produce a whole tail over and over again This absolutely works and it takes you know, it's like six lines of code five lines of code Inherence is really really easy All right, if you look at this somewhere in there the priest is married man. Let's kiss on the horse and So that took you know two minutes So what's the next thing it happens? Of course, right they want something else and so there's another new feature request. This is for echo house Again, you got to keep house got to keep random house. Here's how echo house works. It repeats the bit So the house that Jack built the house that Jack built the multi-land the house that Jack built and like that and So I'm going to do a tiny bit of refactoring before we do this because there's a method in here that has more than one responsibility And if I refact do a little refactoring it'll make my next task easier So this part right here. I'm going to isolate into a method of its own You notice it it has two responsibilities It goes into the array and get some things and then it joins them on a space So I'm just gonna names are hard So I'm going to call that parts and then I'll call parts up here and then we don't care what phrase does any more And so now right now if you pass through you get that that's how house behaves If I could only change it to return this instead Echo house would work That's the only bit I have to change and so now and you're you of course not allowed to use it conditional Can't add in a statement and I submit to you that now that you've gone down the inheritance path You're just gonna do more inheritance. Of course. We are and It's so easy It is so easy to pass this requirement look house. I get override parts now. We're gonna look at this in a minute. So Zip the zip method on a rate is not is not compressed. It's zipper and So what zip does is it takes the thing on the left and the thing on the right and combines them Now some of you may know that join doesn't care that this is a two-dimensional way But I can't I just couldn't stand it and so I flatten it Because it just bothers me. I know I just couldn't help myself Okay, and so that produces this array which indeed Makes echo house work There we go. All right, so here's the code we have now. We got house. I got random house house overrides data random house overrides parts So they asked for another new requirement. What is it? Can anybody guess? Yeah, random echo house Here we go and and so now I contend that you're screwed Because I'm not gonna let you duplicate any code How are you gonna do it? We don't even care about that anymore. That is not the problem. So If you continue down the path of inheritance you have no choice but to duplicate code You can over you can subclass off random house and here at data duplicate parts Or you can just flip-flop it Right here at parts duplicate data Don't tell me that modules is a solution to this problem. I Can hear you thinking that right modules are for cross-cutting concerns Modules is a form of inheritance and I didn't have another hundred slides to show you how badly modules will go wrong But I can promise you it will you should write that code if you think it's better than the solution about the show If you go you can prove to yourself that it's not gonna work, you know Persistible logable There's all kinds of great uses for modules But this is not a modules problem then when you go down the inheritance path and inheritance fails you which is what it did here Then don't just automatically turn to modules to try to as a way to try to share code Often when I go places I go places and they want me to look at their code Right, they have some heinous body of code and very often everyone has this problem Let me say that if you have this right people have subclass objects that are core to their domain to add tiny bits of specialization and then their business Isn't kind enough to fail right their business succeeds and and eventually those chickens come home to roost and so here Often what I see when I go places It is so wrong to subclass off either side of that hierarchy that what they do in cities They duplicate all the code All right, they do this they subclass right off house and they copy everything into it and I'm sympathetic to this right this is Arguably more honest. It's a more true representation of what's going on than either one All right Inheritances failed us now in just in case you're more a visual More visual person. Let's let's look at what happened here within here And so I had house I got random house and echo house it feels like they're that big Right, it feels like they're tiny relative to house. They change they specialize in one small part But really they each contain the entire surface area of house and all of the stuff They didn't specialize that happens here and here and so here we don't even care about house That's not the problem if I want to inherit off random house here. I get that but I can't get that See that one more time wait keynote effects. Got to love this All right, and you can you can do it to the other side too. It's equally bad All right, what we want is some kind of multiple inheritance kind of thing But it this is not how you should use modules here. We don't none of this is the right thing So inheritance is meant for specialization it is not meant for sharing code and When the proportion of what you're specializing is very small relative to the body of code that you're sharing the chance that you'll you'll come to harm using Herons if things continue to change the chance is very high And so here's the question you should ask yourself when you're trying to figure out whether you've done the right thing using Inheritance what you want to ask this is the question right is a that's the relationship things should have is random house a house And when you look at this here, this gives me a chance to use my laser pointer. Look it says house Of course it is right doesn't it feel like random house is a house But the problem is the names are bad the names are wrong Naming is hard and we fooled ourselves with the names instead of asking this question instead of assuming our names are right Let's do this instead What's different? Now It's kind of hard to look at this code and tell exactly what changed And so let me get so here's a really interesting thing. This is this is a paradox you can make it more obvious how things are different By making them more alike So I'm gonna try to figure out what it is about what's different about the code that I had by making it more like So let's just do that. I'm gonna make the data methods alike All right, so I'll just I'm gonna put that in a constant I'll declare a method called data So here's what I have now and so I'm just gonna nibble away at it making changes to make them more alike I could do that right. That's not necessary, but it works and You know, you never do this in real life, but that that works and now I'm down to there I really isolated the last difference And so now when you ask what changed you have to figure out what that is and give it a name And that name can be really hard. I was lucky enough to teach with Audie Grimm And he suggested this technique I when I when I teach I have a rule that you can only ask questions I can only ask questions I can't tell people things and so it can be really tedious for the students when I'm trying to find a Question that'll make them discover the idea and Audie did this. He said pretend it's a spreadsheet With where everything is in a different cell and name the heading name the column headings And so here. Well, that's clearly a class and that okay. I suck at names. So let's just call that data The question is what's this? What is this and we call the class random house and people will get here and they think was it feels like randomness But it's clearly not random is an instance of a thing in this category. All right. It's not random it's order and If this column is order Then that's an algorithm. It's not nothing. It's a thing just as real as this All right, and now that you know, it's orders what's varying it is clearly not a house Or is not a house. It's a role And it's really so let's write some let's write some players of that role It's really obvious what the random order is right. It's gonna take it away. It's gonna shuffle it give it back I'm gonna call this default order. What does it do? What's the algorithm here? Yeah, what's Something right All right, it doesn't change the order. That's the algorithm if you're a functional programmer You might call this the identity function. I find that language very confusing because I'm not a functional programmer But I'm trying not to name it a new thing like that's language. You're familiar with that's exactly what we're doing here And so how would you use it? Well subclass we don't need that subclass anymore. So let's just I'm gonna change data to be an adder here and now I'm gonna Remove the responsibility for ordering from house and the way we're gonna do that is I'm gonna inject this is then the Keyword argument syntax in case you're not familiar with it. I'm gonna inject an order And then I'm gonna give it an opportunity to order the data and now the code just works Now so what's happened here? Well, I have more code To do exactly the same thing I used to be able to do and this is why people complain about object-oriented design Right, they look at this and they think they can't see further but now watch this and that just works. All right, this is composition I'm gonna inject an object to play the role of the thing that varies now that you get that this is really easy Right, how does that go housework? I just got two right one that plays one that does each other of them I'm gonna inject a new one I'm a conservative fearful programmer. So I I formatted Way down at the last possible place. So that's where it is And so now again, I'm injecting two objects to default objects to identity functions If you will and I now I still have more even more new code for the same original behavior But now I can inject that and do this And so I got two roles each of which has two players they could have an infinite number of players I Can add I can add or new orders till the cows come home and new formators here And you can just plug them in and this works and so if we were gonna recite it I can have the original one I can have random I can have echo house or I can have random echo house and I no longer have more code actually I have less code and there's not a bit of duplication here I have this it's this idea of having pluggable behavior and here's the picture This is the one we started with instead of subclassing off it What we've done is we pulled the objects aside. We've isolated the thing that varies and the key here is That that when you have a variant you're varying from something and that something might not look like much But it is a variant that identity function is a variant What you have to give it a name in this case we call them order and format you define a role for them We have order and format or roles and then you have to inject the players at runtime You can watch that again too and so this is composition and dependency injection and this is what it means to do object-oriented design To be abstraction seeking so understanding these techniques being able to see these abstractions Vastly improves your code. It makes everything easier And so in summary first if you're talking to nil it's something Don't check if it's nil and supply the behavior you wish it had use the null object pattern Create active nothings to replace those nils in your code to replace the conditionals in your code Next, you know, I like inheritance I use inheritance if I was tasked with solving random house and echo house I might well do it with inheritance But the day the random echo house requirement comes in you have to run from inheritance and use composition as a solution It's not for sharing behavior and when you when you when you find that you have a failure in sharing behavior It's because you're trying to you also always have two specializations There's no such thing as one specialization if there's one there's always two and so when that happens You're gonna isolate the thing that varies give that concept a name define roles for it and use dependency injection to inject those players This is incredibly easy once you see it But it can be very hard to make the transition from using inheritance to using composition You have to believe in nothing you have to recognize those nothing When you got started in OO It was really easy even if you don't have any formal training It's really easy to understand that you can model objects You can model people you can model that the person next to you or the chair that you're sitting in that It's very clear that you can do that and then it doesn't it didn't take much Longer in your OO career before you start realizing that you can model business processes like procedures and algorithms those make sense Back in the beginning this talk I started off the first objects I talked about were fixed nums and booleans and You can't reach out in the world and pick up a six or a true And yet those objects are real inside my app Those objects are as real as the chair and I need them in order to write OO code and As abstract as numbers are it goes even deeper. It's it's it's an interesting It's a fascinating idea to me that we had numbers for many years before we had the idea of zero Zero is nothing But in many of the number symbol sets when people started counting things There was no symbol for zero and what that meant was that they always had to have a conditional The rules for numbers and the rules for nothing were different And so they always had to say you know you can add these things up or you can have none of them When when they created zero what they did was they they made a special set of rules for zero Which gave them what we would call polymorphism Where that they can put the same symbol in every set and they can send the same messages to all the objects and remove the conditionals So and your applications are full of ideas like zero. They're abstractions that reveal their presence by the absence of code I Don't want to write a condition I just want to send a message to an object and this requires that you find those objects and the objects might be hidden Between the lines of your code these hidden concepts look like nothing But they're never nothing they can't be nothing because nothing is always something Thank you, I'm Sandy Metz