Prime Reacts: The Flaws of Inheritance - YouTube

Channel: unknown

all right are you ready for this let's
do this are you ready for the first one
then the flaws of inheritance by the way
so this is very this is let's say marker
let's see hold on ah the flaws of
all right
so real talk I think that this person
right here code Aesthetics is like one
of the best coding YouTubers right now
he has five videos up in a hundred and
fifty thousand Subs like the guy I don't
know what he does it's just awesome and
I saw one of his videos and I loved it
so I wanted to watch another one I just
wanted to see what he made here and so
let's watch this you may have heard the
saying prefer okay let's turn that down
let's let's let's try that again let's
just try that immediately again but turn
it down because I think we all just got
done experiencing what some people refer
to as an eargasm but other people refer
to as dying from loud sounds all right
here we go you may have heard the saying
prefer a composition over inheritance
the advice is a little vague so I'm
gonna break it down what is composition
inheritance and why would you prefer one
over the other
both composition and inheritance are
trying to solve the same problem you
have a piece of code that you're trying
to reuse
inheritance is when you have a class
that contains functionality you want to
reuse so you create a subclass extending
its functionality
if you if this guy sucks actually what
was I saying here is amazing do you see
where his squirrely braces are okay do
you see
where his squirrely braces are what is
this C sharp what are we looking at
right now
ridiculous okay who puts next line
squirrely braces okay this is absurd can
we can we
everyone go go subscribe to him and then
tell him that his squirrely brace
placement is awful
yeah code Aesthetics my ass more like
Athletics you've basically created a
copy of the class with a new name
and then you can inject new methods to
extend or override parts
whoopsies oh my goodness
um sorry I don't know what just happened
there I got super excited apparently
then you can inject new methods to
extend or override Parts you know when
you're sold this in college or you're
shown this in college or you're shown it
for the first time it seems like this
makes perfect sense every single time I
feel like this makes perfect sense like
this is the way you should solve things
like like on paper this feels really
we have a rudimentary image class here
it represents an RGB image and stores it
as a double array of pixels the image
class hides how the image is stored in
memory and provides a method for looking
up pixel values
we also have some stuff we can do to the
we have a resize what language is this
this is this is C sharp right am I am I
catching this correctly what I don't I
don't I don't understand this thing
right here
okay it is C Sharp I assume that's just
like doing okay I get it it's D sharp
hey type one in the chat if you love C
sharp come on let's see it let's go
let's go let's run it let's go make it
there you go I love all the twos let's
go let's go look at all those two
knew it I knew none of you liked it I
knew none of you liked it you guys just
think you do hey but real talk I think
the guy's doing uh I think this person
here code code Aesthetics is is using
Vim to write C sharp okay hey that's
method which resizes the image by scale
factor and we have methods to flip the
image horizontally or vertically
the library should support jpeg ping and
bitmap images
but we also want to reuse all of these
methods for the different types of
so to support loading and saving these
images we add two abstract methods save
and load
I feel like this is where everything's
going to fall apart
I can already feel like inside of my
this feels so right this feels like
exactly how you would do it
and then all of a sudden
it's just like she goes so wrong you're
like oh crap
um well the flip it vertically in ping
you need to do you know it just it all
then you just it's just it's just the
and then we create the subclass jpeg
image ping image and bitmap image
it's always the same crap it is always
the same crap look at this yeah this
looks beautiful this is this is great
honestly there's different versions of
load and Save
this is great
but also get all of the other methods
for free you don't like me calling it
a PNG is called a ping
an sng is called a sing
what's wrong with you guys
and a gif is called a gif everybody
knows this
a webp no that's actually a webp
okay no one actually calls it webp they
all they call it w-e-b-p
so when we load a JPEG image or a ping
image we can call resize on it and then
we save it the resize method is reused
for all of the image types
or save the overwritten version is
called instead
this works well but now we want to
create a version of an image that
doesn't come from a file at all but
instead has some methods that allow the
user to draw on the image
so we create a drawable image class and
inherit from our parent image class yeah
and this is where inheritance starts to
have issues
the downsides of inheritance is that
you've coupled yourself to the parent
the structure of the parent is thrust
upon the child
nothing like a good old-fashioned
for a thrusting forced to implement
these two load and save methods in order
to reuse our resize and flip code even
though they don't make sense for our
all we can do is have these two methods
throw an exception
to prevent this we need to remove this
method from our parent class and add a
new parent class in between called file
image that contained these two methods
my goodness this is like giving me
nightmares I can already feel one time I
made a game that was like a basic Galaga
Style game uh this was in C sharp a long
time ago with the XNA engine you may not
you probably most of you probably
weren't even alive when X9 came out and
oh my I at one point I think I had seven
layers of inheritance it is where I
realized I hate inheritance is is in the
welcome to Costco I love you thank you
vomitos oh my goodness vamanos just did
it he did the Subscribe then the follow
okay hey priorities
priorities of course what do you mean
you have a game object that has a a
position and an image associated with it
or you just have a position then you
have a drawable game object then you
have a movable game object that inherits
from drop because you know not all
things drawn actually move and then you
also may have an acceleration if you
wanted to add an acceleration so you
might have an acceleratable object I
don't know I don't know I was 19 years
old trying to figure out game
programming reading XNA books
from the internet
and guess what I ended up with seven
layers of inheritance okay I didn't know
what I was doing but I hated it I hated
it I hated it and I learned then and
there I hate inheritance but I didn't
know what to do better
but this also breaks anyone who
currently expects the image class to
contain those methods yeah when new
changes like this come we're forced to
edit all our classes a very expensive
this is the greatest downfall of
I find it similar to how the ideal
cleanest database schema often causes
problems when you need to scale
we've moved to nosql databases with tons
of dirty duplication inheritance breaks
I don't I don't agree with that last
I feel like I I've been following this
guy like me and him tracking one-on-one
right there so good and then he just
throws out my squeal or no squeal just
like you can't be just tossing out no
squeal when we're talking about
inheritance like that okay that's just
when you need to change the code change
is the enemy a perfect design and you
often paint yourself into a corner early
on with your inheritance design dude
this is why capital D design like people
that go into a room and design a system
first fully on paper boxes and arrows
all this kind of I tell you what
this is exactly why it doesn't work this
is like it's this it's this right there
is that you go in you make all these
decisions and then guess what ends up
happening something because your
co-worker's like yeah I need all the
video frames as well and you're just
I just met with you yesterday what do
you mean you need the video frames uh
I forgot are you today I open it up and
now I just need I just need it I need it
today I didn't need it yesterday and
you're just like me I'm done I I
actually just I'm done I cannot build my
product I have to throw away my entire
design my capital D design
I I really I'm all about the the triple
method so if I'm making software to like
what I consider a Perfection level I
make it three times if I'm making it to
what I consider like production quality
I make it twice uh first time I just go
in there and I just like dicks out for
harambe and I just go I I mean I'm just
code right you're just in there
you're just making stuff you're yelling
dicks out for harambe and boom you come
out with like a one file 600 lines you
don't know what happened fever dreams
sweat coming out the crack like right
like it's just like something happened
from here to here and then you take a
step back you understand the problem and
that's when and how and where you fix it
you know what I mean digs Out Boys big
dick energy can I get some Dr Pepper you
know what I mean you know see what the
I have to deal with am I doing a
good as men gold AI
you see the kind of I have to deal
with I get these messages all the time
okay the big dick energy boys you know
uh the big dick Energy number one
himself out here just trying to drink a
little bit of Dr Pepper
but anyways
real talk like
this is really the only constant you
should expect in anything you build is
change and that's why you should try to
make as minimal amount of coupling as
cat girls everybody knows cat girls big
boobs is way hotter
this is because inheritance naturally
asks you to bundle all common elements
into a parent class yes but the soonest
you find an exception to the commonality
it requires big angles can we just like
take a moment here again this guy just
incredible ability to talk about like to
to create an animation that's so
perfectly just displayed like all of our
thoughts together like look at that like
everybody totally understood like this
look how good that word naturally asks
you to bundle all common elements into a
parent class but the soonest you find an
exception to the commonality it requires
big changes
our alternative is to use composition
so what is composition
you've already been doing it
composition is the pattern you're doing
whenever you reuse code without
inheritance if we have two classes and
they want to reuse code they simply use
the code
let's change our image classes to be
composed instead
first we're going to remove our abstract
methods first off I'm actually really
excited about this I think this is
actually a pretty cool idea that's going
on right here
no one ever shows you like really
concrete ways to do things there's a lot
what's called uh
how how do you say this there's a lot of
uh whiteboard masturbation that goes on
and there's not usually a lot of
concrete stuff so I'm actually very
excited about this I think he's doing a
really good job right here and so
I really hope this this goes well
from image
that was David hassloff and thank you
very much I genuinely appreciate that 10
bangers doctorial
Syria I gave up the map I know it's a
different material I know Diablo 3 is
not spelled that way just let me just
let me have my moment okay just let me
have my moment
now this is no longer an abstract class
it's simply a class that represents an
image in memory
in our jpeg ping and bitmap classes
we no longer inherit image
but we'll keep our save and load methods
they'll just now be stand alone not
overriding anything
the methods were accessing a bunch of
stuff from the parent class so what do
we do about those well instead of
accessing them through this
will simply pass in the image in
question instead
I'm liking that so far operations on top
of an image is really nice
this is really really nice like this is
this is kind of what you want to see
already I'm enjoying this a lot a lot
more right now I'm I'm really enjoying
this this is definitely no hand wavy
you're definitely right stegan
so now image represents an image and
these other classes cleanly represent a
specific file format
I can't look at that I'm going to vomit
oh gosh for whatever reason smooth
scrolling makes me want to vomit
now if our new drawing requirement comes
in we create an image draw class that
takes an image to draw to and the
methods do their thing we're no longer
bundled to the file related stuff
because we didn't force all the common
elements into a parent class we don't
need to alter any of the other classes
to add or image draw a class
now the user no longer chooses the one
class that suits their needs they also
combine classes together for their
particular use case so here we're
loading a JPEG image drawing to it and
then saving it
another app could do something different
like load a bitmap image flip it resize
it and then save it out as a ping
inheritance is interesting because it
actually combines two capabilities the
ability to reuse code but also the
ability to build an abstraction
creating abstractions allow a piece of
code to reuse another piece of code but
to also not know which piece of code
it's using you define a contract that
both sides of the abstraction agree to
this gives the code the rough shape of
the other code but it doesn't know
exactly what it is
inheritance does this by allowing a
consumer to think it's taking a class
but it's actually given a subclass
then the code can operate like it always
does even if the system as a whole is
doing something very different if we go
back to when our image code using
inheritance our application used the
natural abstraction capability of
inheritance by storing references to the
parent class
when our app opens a file we just figure
out which subclass to create and then
store a reference to it through the
parent class
lesson polymorphism is great
interfacing is great
then when the user clicks the save
button our save clicked method will get
invoked and we'll just call the save
method and we're abstracted from whether
it's the jpeg ping or bitmap
but with composition you don't have
parent classes you're just using the
types you want
inheritance allows you to abstract
because the methods of the parent class
forms a contract a contract that says
that every child's class shall have at
least these methods
so for our new classes without
inheritance we still want to be able to
call or save and load methods without
caring about which class it is
this is where interfaces come in instead
of a full parent class with all its
variables and methods an interface
simply so I do think
oh let me let me let me hear what he has
to say describes the contract of what an
object can do
in this case we'll create an interface
called image file which I do think that
this is one of the big downfalls of
typescript which is they allow for
and methods in their interface I really
love interfaces
allow you just to have like a shape of
your data right a facade over what it
actually is and it's just fantastic to
have these but the moment you start
introducing properties I feel like
it just starts breaking down right uh
it just really starts becoming a huge
pain in the ass and I just really I just
really dislike that you know what I mean
uh and then I also on top of that I
think rust did it right which is if you
define an interface
and you have an interface method that
you you say you can use you can create a
second interface method and actually
provide a default implementation which
references the first interface method I
think that that's a really cool concept
where you can actually get some of the
benefits of an abstract class
thank you baby looking good
but it only is abstracted over that
individual trait like I think that that
is a really really really amazing thing
yeah def yeah Dev you didn't know about
this oh here check this out okay sorry
I'm going to derail this for a quick
second just so you guys can see this uh
faster than lime if you haven't seen him
uh great stuff he has a great blog he
has some really good stuff uh the his
Advent of code series and I think it's
day four I want to say it's day four
I've I've
yeah here we go um
so he builds out this whole trait
extension going on right so range
contains right contains range and
welcome to Costco there you go let's see
there you go right here
if you define this method you can follow
up with contains or is contained open
the Costco I love you in which you can
actually do this thing right here so I
think that this is really fantastic
right so you can allow for default
implementations if the implementation
only uses what is inside the trade
to me that is like
that's like where the big win comes in
this is where the big this is what I
used to to harass uh Rich Harrison with
I just love this like when I see this it
makes me feel really amazing by it like
I think that this is the truest best way
to have interfaces it is it's it's
genuinely the best I I I don't think
there's a better way to have them that I
can think of currently
yeah exactly so that's what makes it so
good is that notice that it can't refer
to anything that it doesn't know about
since contains range must be implemented
it can it can use that method because it
must be implemented right that is that's
radical right like that is radical I
love that you know what I mean that is
just so sweet
yeah how do you uh I just know I just
know fast in line he's a good guy
yeah it's such a good idea that even
Java does it I think Java did it right
then Java I mean hey thank you very much
I haven't used Java since 1.6 so I don't
know what happened since 1.6
um I started using groovy two point
whatever the hell it was and that was
awful I hate groovy
let's see how I'm friends what do you
think in uh typescript using interface
to describe behavior and types of
describe the data uh it's just too
problematic it's just too problematic
typescript interfaces are just naughty I
would just in general stay away from
for the most part uh thank you graku
welcome for the most part I just stay
away from all right so let's see where
this is going
represents the operations an image file
can do load and Save
now like before we save a reference to
one of our implementations but now
through the interface
and when the user clicks save on
interfaces are a much more lightweight
way to do this because our interfaces
are minimal
I hate the question mark operator like
this this I actually dislike this all
it just it makes me feel upset right
because it's hiding a problem I I don't
I don't my phases are a much more like
way to do this because our interfaces
are minimal parent classes share
everything by default making them more
difficult to change
but interfaces Define only the critical
parts of the contract and are easily
tacked onto existing glasses
now that we have a nice abstraction for
loading and saving files in our app we
can actually lift the creation of which
image file out of our image app class
we'll simply ask the user of the class
to pass in the interface instead
that way this class can just focus on
dealing with the UI commands and the
file class can be elsewhere that's much
better right this is much much better
there's a name for what we just did
there dependency injection
I'll do a whole video on dependency
dependency injection is really hard
the problem is dependency injection is
great until it's not
it's it's it's it's it's something that
oh I want to answer this so bad we'll
come back to this
um it's great until it's not great
it's great until it it's not great right
when you when you get someone into
really intense amounts of it
and then default versions of it it's
really hard to debug through your code
or to even understand where the code's
like I like dependency injection I think
it's a good technique to use I find
myself using it less and less uh just
because I always find
that I end up getting into a place
where it hurts like like you have to
pull up the doll and you ask me where
does it hurt and then I draw a circle
around the whole thing you know what I
it can it can get it can get ouchy
di is almost unavoidable exactly there's
some levels of di that makes sense
there's some levels of di that doesn't
make sense like one thing I find that
people do with the eyes like say you're
building a program to get data from a
and then do something with it now
for testability purposes you could go
full di on this and do all the la la la
or you could do some nice uh you could
you could override the Imports and get
all this stuff in and inject mock
classes and all that crap I hate my
classes they're always such a pain in
the ass to use
But ultimately in the end I find that
most my problems aren't getting data out
of a database right like these you start
building all these abstractions around
it it almost exclusively comes down to
me using an interface that I can pass
around and just make sure that that
thing's correct right like you start you
you know like di works until it doesn't
work and the moment it starts expanding
too much oh man it can just become
it just becomes such a nightmare I just
I remember being at
the tvy code base that Netflix went all
in on dependency injection and so
everything that you got was like a
dependency injected item so everything
was like a functor right so everything
so you got your you got your you
know what I mean like everything was a
functor and so like you just
like to find out what the hell built
your stuff was just impossible to figure
out what the hell even built it it was
very emotional injection but if you've
heard the term before and wondered what
it was that's it passing in an interface
for what you're going to use
I won't say that inheritance is as evil
as some would say but I will say that I
almost never use it in my code
composition isn't perfect you do end up
with a lot of boilerplate needing to
initialize all of your internal types
many implementations will contain the
same code repeated
and when there's also like a structural
problem when it comes to composition I
find that sometimes I end up with these
like folders where it's just like here's
a bunch of functions for playing with
events here's a bunch of functions for
doing that here's a bunch of functions
for doing this other thing and sometimes
I hate that you know I mean like
sometimes all of a sudden I'm just like
I know I have
something to pluck the time stamp
out of this event
but I forget
where it went you know what I mean like
then I have to go like use references
and go find it like it it's not all
beautiful it's not all beautiful used
code you often need to create a lot of
wrapper methods where you simply return
a call to an inner type yeah but
ultimately composition reduces the
surface area between objects which gives
you less friction as changes come in
inheritance might be useful if you're
working inside of an existing system
that had highly repetitive code where
you only needed to modify one thing
for example if you had the need for 100
classes to conform to some specific
interface which half of them need the
same boilerplate over and over again you
might say that that means that each
class has too much responsibility
and you'd be right but changing the
plug-in model would cost the team months
of work
you might not want to put your effort
into that just yet
if you do use inheritance design the
class to be inherited
I'd avoid protected variables with
direct access like we avoid making our
variables public
you know every time I've
done anything with protected stuff I
always find that it's just not the way I
wanted it to be
you know the more you can separate out
from each level the cleaner it really is
for overriding create an explicit
protected API that you're supposed to
override and access
Mark everything else as private final or
this prevents bugs when changing your
parent classes you notice he said those
orders backwards this is because of not
understanding what your child's classes
have done
that was great
I really like code Aesthetics if you if
you don't know code Aesthetics you
go subscribe to code Aesthetics he's a
really good job I think he does a really
really good job with the stuff I pretty
much agreed with everything he had to
say other than that that no squeal one
that he just tossed out right in the
middle yeah this is why uh we use no
squealed document stores and I'm just
like wow
calm down calm down
I love you this is good though
I don't have a strong opinion on
inversion of control I'd have to I
just like everything it's useful when
it's useful it sucks when it sucks I'm
not convinced
I don't like no squeal it just depends
like there's some things that I don't
like no squeal for you know what I mean
yeah but it only has like seven videos
so far uh how was he noticed uh this
quickly I it's just that's why that's
the thing is that when it comes to
YouTube and it comes to the algorithm it
comes to everything it's all about the
like does somebody watch your stuff
engage with it and enjoy it and they
have their own metrics to determine that
and they also have limited screens uh
real estate
hey guys uh they they also have just
limited screen real estate and they can
only promote so many videos at once so
when they do that they have to make sure
that if I'm gonna show this this square
has a seven percent chance of getting
clicked but only a one minute watch time
well that sucks because that's seven
percent times one minute right so that's
like very little seconds watched oh this
one only has a five percent but they're
watching all 10 minutes boom like that
one's a way better one to put in this
position and then not only that you also
then have to start doing like this
person watches videos that is like this
other person you kind of have to think
about it