00:00:15.679
welcome to the penultimate time slot of the conference how's everybody
00:00:21.119
doing yeah good I'm I'm I'm pleased to say I'm finally over my altitude sickness so I should be able to cope
00:00:27.599
with this today um great and uh and I'm not sick in bed with 104 fever which is great
00:00:33.600
because the last time I was supposed to speak at Ruby conin Denver that's what happened so praise be okay so welcome
00:00:41.520
everybody we're getting going this is uh this is thinking in object so who am I I
00:00:47.440
am Josh cesser I'm from San Francisco um I did the startup thing there I'm CTO of
00:00:52.800
a very small startup called cognitive Health Innovations uh we do mental health applications I'm also on this
00:00:59.719
podast called Ruby Rogues um who listens to Ruby Rogues here okay who who listens
00:01:06.240
regularly who listens religiously who listen who listens
00:01:11.880
atheistically yeah okay great um I'm also an organizer of Golden Gate Ruby
00:01:17.400
conference yeah okay who's been to Golden Gate Ruby who's been to goo oh cool great um so I've been doing Ruby
00:01:25.479
about seven years and I've been doing object-oriented programming uh probably longer than some of you have been alive
00:01:32.320
uh I was doing small talk and oop before it was cool so that makes me an object
00:01:39.320
hipster um no hold your heckling till the end of the talk please so uh let's see so thinking in
00:01:46.200
objects this is me thinking about programming in objects and you can tell I'm thinking because I'm wearing the
00:01:51.360
thinking cap so uh you may not have a fabulous thinking cap so I'm here to
00:01:57.360
tell you how you can think in objects better and write better Ruby programs and be
00:02:03.439
happier so uh I was very lucky to get to work with Alan K for a while and Alan
00:02:09.200
Kay he's the guy who invented the term object-oriented programming he's the
00:02:14.680
creator of the Small Talk programming language and he uh invented the whole
00:02:20.440
concept of the personal computer he called it the dynabook so smart guy and he's often quoted as saying something he
00:02:27.800
actually said which is that perspec Ive is worth 80 IQ points now 80 points of
00:02:33.840
IQ is a big difference it's like to genius I mean really so so let's look at
00:02:40.360
an example so don't shout it out but raise your hand if you recognize what's
00:02:46.519
on the next slide and you're only going to have a couple seconds okay now who recognizes
00:02:55.400
it so same thing just a difference in perspective there we
00:03:01.159
go this talk is about the conceptual Foundation of object-oriented programming it's I want to give you a
00:03:08.159
vocabulary for thinking about oop so that you can write better code and be
00:03:13.319
happier while you're doing it there's not this isn't a how to talk there's not going to be a lot of how to do stuff but
00:03:20.519
there will be plenty of definitions so let's go on with the
00:03:25.599
definitions so there's a lot of different kinds of object-oriented programming languages uh that you know
00:03:33.319
if there were only one kind there would only be one language right that so the but they have even though they have a
00:03:38.760
lot of differences they have a lot of things in common otherwise they wouldn't be oop languages and so so I'm going to
00:03:45.920
go over what those things are and then we'll get into them so objects are
00:03:51.480
things that have identity behavior and state and then once you have objects you
00:03:58.640
get an objectoriented program Language by objects with encapsulation polymorphism and
00:04:06.760
inheritance okay so that's basically my talk are there any
00:04:13.840
questions okay okay I I have a little more time to fill so uh we'll continue
00:04:19.400
on right so the punchline is it's all about messages when alen K and the small
00:04:26.800
talk team at the learning research group were putting together Small Talk they wanted to put together something that
00:04:33.680
let them build computers that work the way cells in your body worked and cells in your body cells are actually very
00:04:40.120
powerful computational entities what goes on in a cell is very difficult for us to simulate
00:04:45.440
computationally and the way cells interact a you know a cell one cell in
00:04:51.039
my finger can't tell what's going on inside another cell and it can't make it do something the the way they they
00:04:57.800
interact is one cell will send a chemical message to the other cell and then that and then that
00:05:04.560
cell receives the message and does something in response that it knows how to do and when all goes well you know you
00:05:12.000
have a happy life the object oriented programming is built so that we can have
00:05:18.600
computer software that operates the same way which is really the only way that they could think of how to build really
00:05:24.160
large sophisticated complicated programs that worked okay but we're not talking about
00:05:29.960
small talk we're talking about Ruby which is something people have to remind me now and then so what about
00:05:36.600
Ruby okay who who's seen a slide like this before a picture like this before this is like something that's the night
00:05:44.639
of drunken hell from a grad student right it's like you know who puts these things together this is the history of
00:05:51.680
the lineage of programming languages and how they all interact and uh yeah so
00:05:56.759
it's really hard to see because it's so small to fit everything on there but in the pink circle over there is Ruby and if you look really close you can see
00:06:03.120
that Ruby comes from Python and sather and Eiffel and there's
00:06:09.360
like small talk way back in there but uh so you know I think the only thing these
00:06:15.599
charts all have in common is they're all wrong okay so I I flew out here on on
00:06:20.919
Wednesday I usually hate flying I used to love it when I was a kid but then the seats got smaller as I got bigger and so
00:06:27.919
now I I kind of dread flying but I got a really nice treat on the flight out here because I got the best person in the world sitting next to me on the
00:06:34.680
flight so um I got the opportunity to show a couple of my slides to matz and
00:06:40.440
got and got his comment on it and he said well yeah I got the deaf keyword from python
00:06:47.400
but so and there's a couple things from Eiffel and things like that but uh but
00:06:52.599
really he agreed with me with the slide that I'm showing you next which is uh the real lineage of small uh Ruby and
00:07:00.960
that's and so Ruby is like small talk and lisp got together and have a love child and then went off to work and
00:07:08.039
hired Pearl to be the nanny to raise it so you have object-oriented
00:07:14.879
programming functional programming and system scripting and they all fit together you know beautifully in this
00:07:21.639
really rich flexible language it gives you a lot of expressive less a lot of power flexibility but it can also lead
00:07:28.919
to a lot of confusion and complications as programming Styles Collide so oop is a different kind of
00:07:37.080
programming style from procedural functional Etc I've seen a lot of code that was
00:07:43.360
nominally Ruby but was actually Java or C or PHP I mean okay who's who's seen
00:07:49.520
that who's written that okay thank you
00:07:55.680
um okay so that that's enough intro onward for
00:08:01.039
definitions the uh so this is what I showed before um objects are identity behavior and state so let's start
00:08:07.000
drilling into those things now identity is the quality of existential
00:08:13.919
uniqueness right just because two things have the same value doesn't mean they're the same thing I have a very good friend
00:08:20.479
who's a twin and he is not his brother the um okay so I'm gonna I'm gonna try a
00:08:26.720
little analogy here who here knows what the internet is okay okay who here is a web
00:08:33.320
programmer okay good number of people the when you open your browser and you take a look at a web page like
00:08:40.440
GitHub there's some communication that happens between your computer and the GitHub
00:08:45.720
server the way that those things find each other to talk to each other is they use the domain name system to look up
00:08:54.320
their addresses so we have you know we can look up the address for github.com and that comes back as a 32-bit number
00:09:00.600
we have a dotted quad notation here and that's a unique address for that server there's no other server anywhere on the
00:09:06.040
planet that has that same address and that mean so that means it has a unique identity now we're going to ignore
00:09:11.399
things like firewalls and load balancers and reverse caching proxies because they don't really matter for the point I'm
00:09:17.920
trying to make here and you can simulate all that stuff in object ored programming as well so skipping right
00:09:23.920
along the uh so we we have this unique address we have the same kind of thing in Ruby and Ruby uh we have an object
00:09:31.720
say we have a a string a string Fu and we can ask that object for its object ID
00:09:37.079
which gives us back a unique number that uniquely identifies that object we can have another string Fu and we ask it for
00:09:44.760
its object ID and it's a different number in in MRI and mat's Ruby those
00:09:49.839
numbers have to be different because different objects because those numbers represent the addresses in memory you
00:09:56.079
know so it's like a pointer in C so they're always going to be so we have two strings they have the
00:10:03.399
same value they're spelled the same everything about them is the same they you know s dou equals T but if you use
00:10:11.079
equal question mark that compares the identity of the objects and they're different and we can do a little
00:10:16.240
experiment and we can say okay we have s s and t and we shovel D onto the end of s to get food T is still
00:10:24.040
F okay so that that's about it for identity what about Behavior we have a string we can ask it for its methods
00:10:30.959
methods are the behaviors that the object knows how to do and so we can ask
00:10:36.200
we can ask food for it me its methods and we can see it can compare itself and tell how long it is and reverse itself
00:10:41.680
in a number of other things and you can create your own behaviors by defining new methods so here we have a numeric
00:10:48.160
type and we're telling it how to figure out if it's even or if it's odd okay so that you know we've we've
00:10:55.240
now covered identity and behavior and you know so that's where we're at but we
00:11:02.839
haven't built an object yet but what we have is something that's still pretty interesting we have things that I'm G
00:11:10.600
I'm going to call uniques here and these are these are different from Singleton these are things that do not have any
00:11:17.040
internal State there's nothing inside them they're atoms you can't cut you can't chop them
00:11:24.320
down in any smaller pieces but they still have you know there's still interesting things
00:11:29.920
and they have some distinct properties so and we deal with those things every day when we program in Ruby so we have
00:11:35.760
nil and true and false and use those all the time the we also have symbols which
00:11:42.920
have Spellings which make them different and they're certainly not Singletons because there's many different instances
00:11:48.279
of class symbol but once you have a symbol you never look inside it the you
00:11:54.160
know the way that it's spelled never matters it's just its identity that is the interesting thing about about it you
00:12:00.279
can also create your own uniques okay so who who got to see Ben arnstein's talk the other day about
00:12:05.639
refactoring that was a great talk if you haven't seen it you should go watch the video um so Ben showed the null object
00:12:12.959
pattern to say okay here's how you create an object that represents somebody using the system who isn't
00:12:18.120
logged in and he created a whole class around that and so so you you only need
00:12:24.040
one of these things and and you're never going to have you aren't going to have like 800 of these unique objects
00:12:30.360
representing the same person right the or different people because they all behave the same they're not logged in
00:12:35.519
they can only do one thing the so here's how you create one of those things without having to define a new class you
00:12:40.880
just say I'm going to make an instance of object there's no instance variables in it it can't really do much of
00:12:47.560
anything so I'll start putting some methods on it and you can use the you know Define a method on guest and you
00:12:54.440
just give it a name so it doesn't have to look in an instance variable to find out what its name is because it's always going to be the same so we now so we
00:13:01.000
have an object that just because it has a unique identity and you've put some behavior on it it's interesting and can
00:13:07.519
do a lot for you okay but it would be really hard to write programs that did useful things if
00:13:15.279
all we had was uniques so we need some state state is the data that's contained
00:13:21.399
within an object that lets it fulfill what it has to do to do the
00:13:26.959
behaviors so it's all so data is always in service of
00:13:35.480
behavior so here we have we have Fu we shovel a d onto the end and then we can
00:13:41.800
ask how long it is so the object has to maintain some data for what are its
00:13:47.000
contents so when you ask it how long it is it knows but objects you know like cells
00:13:54.399
are like boxes you can't look inside it so even though State exist EXs from the
00:14:00.639
outside it looks just the same as Behavior another way to to look at that is we call that uniform
00:14:07.560
access okay we have say we have a person class and we want to ask it for its name
00:14:15.519
we have a we can create that as an accessor and just say okay we have a first name that's equivalent to defining
00:14:22.079
a method with an instance variable in it that returns that the first name value
00:14:29.680
but what if we aren't storing the names as a first name and a last name some people with middle name suffixes
00:14:35.160
prefixes whatever you um what if we store it just as a full
00:14:41.399
name and the way we get back the first name is by splitting off the first word if we have different classes of
00:14:47.759
users they can store their names in different ways and as long as they return them this you know when you ask
00:14:53.440
for a first name you're good the uh one way of um so one so
00:15:00.199
uniform access means that asking for data looks just like invoking an
00:15:05.519
operation on the object something that doesn't do this is Javascript JavaScript does not have uniform access if you ask
00:15:12.519
for the value of a property it looks different from ask from calling an accessor function first line is a
00:15:19.040
property second line is a function that means that if you want to change how names are stored you have to go and hit
00:15:26.079
every call site that accesses that property and turn it into a and turn it into a call in a function so it's one
00:15:33.720
that's the single thing that I hate most about JavaScript okay and back to our analogy
00:15:39.000
about the uh talking over the internet you know talking to the to a web server
00:15:44.120
if we make a get request on index we don't know if enginex is just
00:15:50.839
returning an HTML file that's stored on dis or if there's a rails application
00:15:56.120
where an action in a controller runs and renders an ER template and Returns the HTML or if there's something that's
00:16:02.480
being served out of men cached or varnish you all we know is that we send a a get request and we get back a
00:16:08.720
response with h with some HTML in the body okay the name for this property is
00:16:15.279
encapsulation and I've already said pretty much everything I need to say about it so here's a picture of a
00:16:21.680
cauter okay our next property is polymorphism uh in Ruby we call that duck typing because if it looks like a
00:16:28.360
duck and quack like a dock it'll probably cook up nicely in an orange sauce the the other way that I like to
00:16:35.160
think about this is protocols so everybody works with protocols if you do web programming you have you know TCP
00:16:41.440
that lets you send packets over the internet you have HTTP that lets you communic lets browsers talk to web
00:16:47.319
servers and a protocol is just a set of rules for nads to communicate with each
00:16:52.800
other another way to think of that is it's an API and objects that have compatible
00:16:59.480
apis can be substituted for each other so that's the essence of polymorphism I think that's a pretty
00:17:05.520
easy concept to grasp so I'm not going to dwell on it too much okay so let's review for a moment
00:17:11.160
where we're at we have objects and we've covered encapsulation and
00:17:16.600
polymorphism that gives us what is called an object based language I didn't
00:17:22.280
make up the term but I like it and I think it expresses something pretty interesting that the secret sauce to an
00:17:28.480
object based language is dynamic binding or late binding and what that means is
00:17:34.080
that the code that executes when you invoke an operation on on an object is
00:17:39.360
determined at runtime not at compile time and and this is what lets us write
00:17:45.120
extensible Frameworks like rails because the code in rails that calls into your
00:17:51.120
application was written long before you ever wrote the code in your application so there's no way that it
00:17:57.799
could know about your code at run time or at compile time
00:18:03.280
so but moving up from an object based language to get to objectoriented we get inheritance but oh
00:18:10.799
wait one more thing about um about object based languages there are a fair number of object based languages out there we tend not to program in them for
00:18:17.520
general purpose programming because they're not very fun to program in for General case stuff I think they tend to
00:18:23.400
get used for things like simulation and other special purpose applications they're not so great for for what we do
00:18:30.360
because they're not as efficient because you don't have inheritance inheritance is great because
00:18:38.200
it makes you much more efficient in writing code you get to share code and that's that's what inheritance is
00:18:43.480
basically about you know we all know about dry right you know don't repeat yourself
00:18:48.559
Dr and then we have and as opposed to wet for right every time uh
00:18:56.120
the the um so I haven't talked about classes much here but classes are a way
00:19:01.679
that different instances can share their implementation so so we have a bunch of
00:19:07.000
of user objects they all share the code through a construct called a class and then classes can share their
00:19:13.360
implementations by saying okay I have a super class and then I want something that's a little more specialized than that so it'll be just like that but I'll
00:19:19.600
add some differences I think I think everyone's probably familiar with that concept but there are two types of of
00:19:26.200
inheritance to think about one is inheritance of implementation and the other is inheritance of type given that
00:19:33.520
Ruby is dynamically typed inheritance of type doesn't really help us all that
00:19:39.039
much if you're working in a language like C++ or Java where you have to worry
00:19:44.320
about a static type system type inheritance becomes much more important but the point of type inheritance is
00:19:51.320
largely to make the compiler happy so that your code can be better performant
00:19:56.559
it can be can have better performance um there's a lot that compilers can do
00:20:01.960
using the information and type systems to make the code run fast um but Ruby
00:20:07.360
approaches things differently so type inheritance isn't as useful for
00:20:13.400
us um the thing that I want to stress about inheritance is that deep
00:20:19.440
inheritance hierarchies are dangerous you should try and avoid them uh Sarah
00:20:25.080
May said this morning that good code has few dependencies and she showed a few examples of that that you know including
00:20:32.159
the law of Deiter and how that works the um the thing to keep in mind with
00:20:37.320
inheritance is that if you have a deep inheritance hierarchy your subass is dependent not only on your immediate
00:20:43.120
super class but all of your ancestry because any one of those things that changes any of your ANC ancestors
00:20:50.760
that changes can directly affect you so you're coupled to all of your super classes all the way up the line so it's
00:20:57.320
best to avoid that um and you want to use I prefer to use composition so let me let me take a
00:21:03.960
slight detour here and I want to talk about delegation so how many people have heard
00:21:09.760
the term delegation and how many people are confused about what it means okay so delegation gets used to
00:21:17.679
mean two different things depending on which Camp you're in or what language you you work in usually most people who
00:21:23.799
work in Ruby use delegation to mean uh something that happens in position so
00:21:30.120
rather than inheriting from an array you create a new class that contains an array within it and then it compo it
00:21:37.080
composes itself with the array and uh can take on some behavior of the array
00:21:42.600
by using the array within it the there's a great library in Ruby called
00:21:48.880
forwardable that makes that really easy to do rails has some built-in stuff for doing that within active record models
00:21:54.159
as well um the other type of Delegation is used for with people who talk about
00:22:00.760
prototypical object languages JavaScript uh is one of these languages so is self and in those cases delegation
00:22:09.640
actually means inheritance so in 1987 at upsa there was a really great paper
00:22:14.760
presented by ly Andrea Stein called delegation is inheritance and she proved mathematically that prototypes and
00:22:22.240
delegation was exactly the same as classes and inheritance and settled a war that had been brewing in the the
00:22:28.559
object-oriented world for years um and then the next year a paper came out by a gang of people uh called Treaty of
00:22:34.279
Orlando that made some really interesting uh
00:22:40.240
um you know basically covered the whole landscape of different kinds of object languages and what their properties were
00:22:45.799
and if you're into like nerding out on object languages that's a great paper to
00:22:50.960
read um but that's not the kind of Delegation that that we usually talk about in Ruby but I just wanted to
00:22:56.960
clarify that Okay so we've gone through oop we have objects encapsulation
00:23:02.320
polymorphism and inheritance great but as I said the punch line is it's all about messages me if you can think about
00:23:09.760
objects interacting by sending messages that's when things start to get interesting so I'm I'm G to show you a
00:23:15.880
bit about how how this works and make some recommendations excuse me
00:23:31.440
so the there's a mistake that gets made by pretty much everyone who uses Ruby um
00:23:36.880
including people on the Ruby core team and that's that when you invoke an operation an object they call it calling
00:23:42.960
a method which is wrong so the the real way to think about
00:23:50.880
that is you're sending a message why aren't you calling a method well if you're doing delegation the when
00:23:58.159
you when you invoke an operation on an object the the real thing you're doing might not even be in that object it
00:24:05.200
might be in a different object that it's delegating the behavior to or even more nasty there might not be a method there
00:24:11.840
at all you might have a method missing Handler that figures out what to do when it gets that you know when it gets that
00:24:18.240
call on it so the best way to think about that is that you're sending a
00:24:24.360
message so however there is actually a way in Ruby that you can call a method directly I do not recommend this but I
00:24:31.279
will show you how it's done we have a yeah yeah laugh Eric so
00:24:37.480
we have a we have a person class and person has a name and then we have an employee subass and then and when you
00:24:43.799
print out the employee directory you want to know not just what their name is but their title so the name gets
00:24:49.399
reported as name comma job title but when you send that person that employee a letter you probably don't
00:24:55.399
want to put you know Josh cesser comma CTO on the front of the envelope so you want to address it to just the bare name
00:25:01.760
which means you want to get the name as as answered by the person class so here you can uh if you look in that recipient
00:25:09.039
name method the first thing we do is we pull the method out of the person class
00:25:14.080
we bind it to the recipient object and then we can call it and that's how you call a method in Ruby but don't ever do
00:25:22.279
it or James Edward gray II will come to your house and hurt you
00:25:29.159
so so that so so let's all take the pledge from now on when you invoke an imp an operation on an object we call it
00:25:37.240
sending a message thank you very much okay the next thing is use objects
00:25:45.520
Ruby is an objectoriented language so use the objects that there's there's an
00:25:50.960
amazingly Rich set of core objects and you know fundamental data types in Ruby
00:25:57.760
and that and that's part of what makes Ruby great to use
00:26:03.240
but uh what is it what do How does it go um a difference in quantity eventually
00:26:09.720
becomes a difference in quality it's the kind of thing that you can get away with doing it a little bit but when it starts
00:26:15.000
to dominate your program design you're not doing objectoriented programming anymore I've seen programs written in
00:26:21.320
Ruby that worked by passing around hashes and one of the keys of the hash was type and that type was a name for
00:26:29.520
the kind of of hash it was and each method that received that hash would do
00:26:35.399
a case statement switching on that type and running a different piece of code so so they basically reimplemented
00:26:42.000
polymorphism that ran several orders of magnitude slower than the way the Ruby VM knows how to do it um that you know
00:26:49.640
did people see kui's talk where he talked about optimizing the virtual machine internals and and me method
00:26:56.399
dispatch you you you see how much effort VM implementers put into optimizing
00:27:01.559
these operations you are not going to do better than that so let the let the VM do it for
00:27:07.600
you okay so we have we have these data types we have blocks and lamb dubs we have hashes and arrays we have symbols
00:27:13.440
there's a lot of really cool stuff that's built into Ruby and you can build programs out of those things exclusively
00:27:18.520
but they will not be objectoriented you want to use objects so so my friend Corey Haynes said
00:27:24.640
reification unites Behavior with data and he uses the term behavior attractors
00:27:31.440
when you when you draw a circle around all of your data and behavior it's really easy to know where to put more it
00:27:37.240
goes inside the circle if that that example I talked about with the hashes and the typ
00:27:43.720
keys the behavior in the in the program for how to interact with those data types was scattered all over the program
00:27:51.000
so if you change something about that data structure you had to go find all of the code throughout the entire system
00:27:58.640
and look and see if that would be affected that's that's one of the primary advantages of object ared programming is everything is contained
00:28:05.200
within the cell membrane you know where it is you know what you have to touch
00:28:12.200
okay so so here's here's a a few different variations of how you can do something in Ruby so in active record we
00:28:18.679
have validations you have a you have a person and they have and they might have a
00:28:23.919
phone number and you want to make sure that they do have a phone number so you can say validates presence of phone
00:28:29.159
I made the names short so they would fit on the slide here ordinarily I'm more wordy in my code okay so but maybe the
00:28:35.960
user would have could have more than one phone you want to make sure that they have either a home phone or a cell phone
00:28:42.559
so you can use a block to do that that's pretty easy um but maybe there's like a whole
00:28:49.440
bunch of different ways that they could have a phone like they could have an emergency contact person who has a phone number and you probably don't want to
00:28:55.039
shove all of that logic into a block right there in one line so you create a method and then you use validate use the
00:29:00.640
validate has a phone method to do that for you but then you say okay well I don't just have users I have companies
00:29:07.200
that have phone numbers so I want to extract that code from that class and put it in its own class so I now have a has a phone object that does that
00:29:13.559
validation for me there's this whole progression of how you can scale from
00:29:19.799
using very simple procedural type stuff into using more object-oriented patterns that will help you and make your life
00:29:25.760
easier so you don't have to go go crazy and you know use a hammer everywhere but
00:29:32.640
you want to make sure you don't get stuck using a screwdriver all the time instead as well well talk about a strained metaphor
00:29:40.279
okay moving right along polymorphism polymorphism is uh probably the thing I
00:29:45.519
like most about object-oriented languages it's uh it it just it provides
00:29:51.880
a lot of flexibility and it's really how we think about stuff in the world it's just we don't interact with everything
00:29:58.039
everything as if it's the same thing right different things are different um so the so po so as I said before method
00:30:05.240
lookup as fast you know kichi talk showed that they put a lot of effort into optimizing these things you should
00:30:10.480
rely on polymorphism because it not only is it going to make things faster it's
00:30:16.360
also going to help you organize your code a lot better that's the thing that lets you
00:30:23.039
put everything within the in that Circle so that it all is in the same class so
00:30:28.200
let let me show you an example last week uh Ryan big right you know hand big round of applause for Ryan big he's done
00:30:34.279
a lot of stuff for like documenting Ruby and writing writing some good code he
00:30:40.399
he's a smart guy and he knows Ruby but he got himself in trouble so he tweeted last week oh my God I can't deal with
00:30:46.159
this code I'm like and I and he tweeted a gist to something and I looked I said man that person needs to be taught a lesson he's like yeah it was
00:30:53.720
me so I took a look at it I saw what I could do so so let let's take a look at what happened here so this is the code
00:31:00.039
that he wrote and the only thing wrong with this code is that it's not objectoriented this is procedural code
00:31:06.840
and so I I did a little highlighting here so we can see a little more what's going on here can can people actually see the code okay so okay so let let me
00:31:15.200
talk through it but there's three levels of conditional nesting here and that's like you know smells really bad the the
00:31:22.559
first thing that you check is is uh you know is there a product
00:31:28.360
and then okay do we have an admin and are we looking for deleted
00:31:34.200
records and so but there's definitely some cleanup that you can do here I took a look at this for for like two minutes
00:31:39.960
and it became pretty clear what was going on if you look in the first half of the of the method you know between if
00:31:45.600
product and else the blue else um all of the messages there are being sent to the
00:31:51.799
product and if there's no product you're talking to the variant class and then
00:31:56.919
within each of those cases there's very similar structures where you're saying okay if I'm an admin
00:32:02.240
and looking for deleted then I get to see all of the things otherwise I just
00:32:07.279
see the stuff that isn't deleted so I looked at that I said okay that's I can do a couple refactorings
00:32:14.320
there and this is what I ended up with so I wanted to make the make it
00:32:20.480
polymorphic so it was the same message being sent to different classes of things so I just inserted the aliases
00:32:28.120
for those messages into the variant class and then within the scope method I
00:32:34.399
collapsed the that first conditional if you go back here and look we got if product else something else I said okay
00:32:41.200
great we're either using product or variant because in the second Clause all the messages went to variant and then
00:32:47.159
there's only two cases left you either want to show the Deleted things or you don't so this is the kind of thing that
00:32:53.480
you can do when you learn how to think correctly in objects and you're using
00:32:58.519
polymorphism you're using classes
00:33:04.039
so we have objects identity behavior and state we have object Ranger programming
00:33:10.760
objects encapsulation polymorphism inheritance and the real Secret Sauce is
00:33:16.440
messages so one more thing my Ruby Rogues podcast is is a
00:33:22.960
finalist for the podcast Awards and voting is going on now it's going be going on through November 15th you can
00:33:29.519
vote once a day each of you so vote early vote often thank you very much