The Only Time You Should Use Polymorphism

Sdílet
Vložit
  • čas přidán 26. 06. 2024
  • The only time you should use subtype polymorphism is when you have behavioural variation in the subtypes. I teach two university courses on object oriented programming and I see this problem a lot. Let me explain. RECOMMENDED READING:
    🛠️ geni.us/k8KhT3 (Refactoring)
    ⭐️ geni.us/zzlx (Dependency Injection: Principles, Practices, and Patterns)
    💎 geni.us/hNDE (Elegant Objects)
    🏛️ geni.us/IBhtLnh (Clean Architecture)
    🧩 geni.us/nlbA6 (Head First: Design Patterns)
    Read my FREE book on Object Oriented Programming:
    🚀 theobjectorientedway.com
    Watch next:
    • Always Use Interfaces
    CONTENTS:
    00:00 Thesis
    01:16 Example 1
    03:28 Example 2
    06:25 Example 3
    13:26 Summary

Komentáře • 672

  • @user-lb6zo3bj9k
    @user-lb6zo3bj9k Před 3 měsíci +302

    Finally he remembered his password

  • @Adam-326
    @Adam-326 Před 3 měsíci +446

    He randomly came back after three years! Tak! Good to see you again.

    • @rafael.aloizio1769
      @rafael.aloizio1769 Před 3 měsíci +11

      Yessssss I'm glad he is back man, I've learned so many things important on my career because of his videos

    • @danielszlicht2633
      @danielszlicht2633 Před 3 měsíci

      Love him, jest super

    • @pulkitprajapat7862
      @pulkitprajapat7862 Před 3 měsíci +5

      OMG I can't believe this, I asked him last week on Twitter when he is coming back..and here he is. 🐐

    • @DerRabeKirill
      @DerRabeKirill Před 3 měsíci +5

      What are the chances he’ll stay with us for some time now 😅

    • @pabloescobarparis6586
      @pabloescobarparis6586 Před 3 měsíci +4

      @@DerRabeKirilldepends if project manager leaves him alone

  • @Pawemie
    @Pawemie Před 3 měsíci +42

    I like to think, that you got so annoyed by someone at work using subtype polymorphism the wrong way, that it brought you back here ;)

    • @ChristopherOkhravi
      @ChristopherOkhravi  Před 3 měsíci +22

      I teach at a university so I read a lot of student code 😉

    • @tony2351992
      @tony2351992 Před 3 měsíci +4

      in which university do you teach I need to attend there, you're a CS legend
      @@ChristopherOkhravi

    • @BANANAS2011
      @BANANAS2011 Před 2 měsíci

      I wish more people in your team do things in wrong way that you make more videos to teach others 😂

  • @arminium56
    @arminium56 Před 3 měsíci +39

    Man, your notification popping up is the best thing happened in months❤, please don't disappear again :)

  • @EricFreedmanMusic
    @EricFreedmanMusic Před 3 měsíci +51

    literally just watched your design patten playlist, thinking why hasn't he posted for 3 years. perfect timing

  • @williamcory4689
    @williamcory4689 Před 3 měsíci +18

    Welcome back! Got a huge amount of value out of this channel in my early days of becoming a dev

  • @andreaskonomis3
    @andreaskonomis3 Před 3 měsíci +39

    Have been watching your Design Patterns series and the code walks videos and was always thinking if I ever wanted to have a youtube channel it would have been of this style. If only he was back. Wish granted, welcome back, so happy to see notifications from you! :DDDD

  • @sam_musey
    @sam_musey Před 3 měsíci +4

    I came to the comments section to share my excitement about your return.
    But, wow, I didn't expect there to be so many of us!

  • @culturemanoftheages
    @culturemanoftheages Před 3 měsíci +6

    I've never been more engaged with software engineering CZcams content. Your presentation skills are excellent.

  • @davidntumba2447
    @davidntumba2447 Před 3 měsíci +19

    I didnt even fully read the title, I just saw the thumbnail and clicked. Welcome back good sir.

  • @TazG2000
    @TazG2000 Před 3 měsíci +2

    I would add a point that "variations in behavior" can also come from _outside_ the class - a class can just be a data type, without being responsible for all the ways the data will be used. Imagine an Attack subtype, EnergyAttack, which has an additional property EnergyType. Now Scratch is a base Attack instance, and Thunderbolt is an EnergyAttack (with EnergyType = Lightning). Looking at just the class definitions, you could say this is a data-only variation, but it makes sense because the change is in the data structure (an additional property), not just values. These subclasses may or may not implement variations in behavior themselves, but _allow for_ external variations by being different types: We may have many general functions that work with the Attack type, as well as some functions that accept EnergyAttack specifically and work with the EnergyType property.

  • @dhruvachandra8210
    @dhruvachandra8210 Před 3 měsíci +46

    Suddenly you appeared out of thin air, please be consistent. Love your content.

    • @maximus1172
      @maximus1172 Před 3 měsíci +6

      No one told you to wait

    • @maxpricefield7586
      @maxpricefield7586 Před 3 měsíci +3

      we aint entitled to his time

    • @bakaoug4g491
      @bakaoug4g491 Před 3 měsíci +1

      He can post whenever he feels like posting. How entitled are you. Anyway thanks for the content ignore comment like above

  • @mohamedsayed8697
    @mohamedsayed8697 Před 3 měsíci +3

    Oh my god. I used to watch your videos when I started learning programming in 2021. Right now, I have an interview with Google scheduled next week. You don't know how happy I am to see you back. Thanks for coming and welcome back :)

  • @no-name2031
    @no-name2031 Před 3 měsíci +8

    The man, the legend, welcome back

  • @Michaeljamieson10
    @Michaeljamieson10 Před 3 měsíci +1

    please continue have been rewatching your old stuff so glad to see this

  • @Ruzi1996
    @Ruzi1996 Před 3 měsíci +1

    I started watching him on thursday, fell in love in his way of teaching, and was thrilled when saw that last video was posted 3 years ago. And now he appears out of nowhere 😂 If you were giving lectures on my Uni, i wouldnt miss a single one

  • @CarlRoa
    @CarlRoa Před 3 měsíci +7

    Awesome to see you back Chris! I’ve learned a lot from your videos, and have always enjoyed hearing your perspective on things. 😎👍

  • @kiranmahajan9067
    @kiranmahajan9067 Před 3 měsíci +8

    Your teaching is the greatest and simpler....❤

  • @barisaxo
    @barisaxo Před 3 měsíci +4

    I've been watching your playlists whilst working on my project over the last year, and this video is so relevant to what I'm currently working on, and exactly what I've been asking myself while working on my current systems. Being self taught I don't know the theory very well, so I would had never known that I was dealing with 'sub-type polymorphism'. Good to know I've been going down the right path, and also to know what it's called!

  • @maurobastasini8704
    @maurobastasini8704 Před 3 měsíci +4

    Great to see you again Christopher. Miss your videos of design patterns and discussions about principles. Welcome Back!!

  • @echoes675
    @echoes675 Před 3 měsíci +2

    Best notification from CZcams in ages. I hope you've got more for us. 100% my favourite videos covering software development!

  • @KlamerR69
    @KlamerR69 Před 3 měsíci +1

    Welcome baaaaack!!!!!!!!! Happy to see you around after so much time! Hope you'll stay for a while, your content and way of explaining things are precious.

  • @rizwanfirdous
    @rizwanfirdous Před 3 měsíci +3

    Last month i was discussing with my friend about Design patterns and in that discussion i recommended him your channel. While discussing i said to him that don't know why you aren't posting any content. I was little bit worried about you. Now i am very glad to see you back. 🥳🥳🎉🎉❤❤❤

  • @muhammadhamzashujaat6767
    @muhammadhamzashujaat6767 Před 3 měsíci +1

    Love to see you again, I look forward to rewatching as much of your videos as possible !

  • @r1konTheAutomator
    @r1konTheAutomator Před 3 měsíci +1

    WHOA! i started watching your pattern videos like a week ago, binging them while at the gym, while taking walks, etc. i was like "eh it's been a long time since hes posted...he was a good teacher" and then here you are!!

  • @RajivKumar-qj9nw
    @RajivKumar-qj9nw Před 3 měsíci

    So happy to see you back. You're the sole educator whose CZcams videos I choose to view at regular speed, even when they extend to an hour.

  • @vivekvishwakarma3711
    @vivekvishwakarma3711 Před 3 měsíci +2

    Happy to see you upload after 3 years love the way you teach..

  • @Flutterdev6391
    @Flutterdev6391 Před 3 měsíci +2

    I watch your videos and you are the first teacher whoes course i can buy without thinking about money.

  • @liquidpebbles
    @liquidpebbles Před 3 měsíci

    Glad CZcams was smart enough to recommend your video. Glad to see you posting. Always top notch stuff.

  • @mobasherfasihi690
    @mobasherfasihi690 Před měsícem

    Glad that see you again, hope I can see another series also before I die. But high quality takes times, I'm really enjoying when watching your videos.

  • @nullset11
    @nullset11 Před 3 měsíci

    Glad to see you're back uploading videos! Your series on design patterns were instrumental in taking another step in my career!

  • @rickydownload
    @rickydownload Před 3 měsíci +1

    From last week I started watching your Design Pattern playlist and thinking why Chris stopped posting videos for last 3 years. To my surprise there was a notification😮. Your teaching is so great and natural, not everyone gets so easily. Keep up the Great work!

  • @PbPomper
    @PbPomper Před 3 měsíci

    I've really missed these talks. Welcome back!

  • @eladfeldman1152
    @eladfeldman1152 Před 3 měsíci +1

    happy to see you are back! keep the content coming! learning a lot.|
    love the whiteboard, the editing and the why you teach :)

  • @alexstone691
    @alexstone691 Před 3 měsíci

    I did not expect you coming back, loved your design patterns, glad to see you're doing well!

  • @SudhakarChintapalli
    @SudhakarChintapalli Před 3 měsíci

    Hey @Chris, it's great to see you after such a long time! I've always been a fan of the way you present content with crisp editing.

  • @artemisSystem
    @artemisSystem Před 3 měsíci +4

    Whoa, seeing you in my sub box is not something i expected today. Long time viewer of the channel, since the ramda days! Good to see you back.
    Your channel helped me get into functional programming, and now i'm absolutely obsessed. Do you have any plans for more FP related videos?

  • @RyanCortino
    @RyanCortino Před 3 měsíci

    Man, I was so happy to see your face in my feed again. Glad to have you back, your videos always helped me understand these concepts so much better.

  • @dani2500d
    @dani2500d Před 3 měsíci

    Finally you are back!
    I enjoyed so much your design patterns playlist.

  • @shreyshrivastava
    @shreyshrivastava Před 3 měsíci

    I don't have words to describe how happy I am to see you back.. Love your style of teaching

  • @UnTizioACaso1
    @UnTizioACaso1 Před 3 měsíci +1

    I missed your videos, thanks for coming back!

  • @drewbruggman
    @drewbruggman Před 3 měsíci +3

    I didn't learn anything, but the way you described the information in the video *clarified* what I'd already known -if that makes sense.
    You are excellent at explaining things in very clear terms, and I appreciate that. Subbed.

    • @ChristopherOkhravi
      @ChristopherOkhravi  Před 3 měsíci

      I'm very happy to hear that. Also, thank you for the sub. Welcome 😊

  • @alinapostol2230
    @alinapostol2230 Před 3 měsíci

    We've been waiting for you to come back! - so WELCOME BACK ^^!

  • @NoahNobody
    @NoahNobody Před 3 měsíci +1

    I like this format. My ADD was trying its best to block the info, but this guy just kept going :)

  • @nkramachandran8500
    @nkramachandran8500 Před 3 měsíci +1

    Legend is backkk!!! Good to see you, your system design videos are epic!! MASSIVE THANKS

  • @EmpySoul
    @EmpySoul Před 2 měsíci

    Once again, you enlightened me about something that might seen implicitly known and simple but in reality a truly good software engineer needs to be absolute aware of.

  • @shridharcs
    @shridharcs Před 3 měsíci

    Welcome back Chris. Have learnt a lot from your design pattern series!

  • @noeldionisioparedescuevas5992
    @noeldionisioparedescuevas5992 Před 3 měsíci +3

    We miss your valuable teach.

  • @chirayumishra1185
    @chirayumishra1185 Před 3 měsíci +2

    Welcome back Christopher. Good to see you back 😊 Keep up the good work and make learning fun and easy. All power and good wishes ❤

  • @wbaloo1
    @wbaloo1 Před 3 měsíci

    I missed your tutorials man, good to have you back!

  • @himanshugoyal3862
    @himanshugoyal3862 Před 3 měsíci +2

    The king of Design patterns is back🔥

  • @virtexamit
    @virtexamit Před 3 měsíci

    Love to see you back, hope all good at your end. You have unique way of communicating complex things, Keep making video.

  • @benjones1452
    @benjones1452 Před 3 měsíci

    Welcome back, I have missed your content, it is great. Your analysis has been vital to my programming evolution, if only I'd have had teachers with your energy.

  • @ripanpramanick425
    @ripanpramanick425 Před 3 měsíci +2

    Your Design pattern playlist is the best I have ever seen on any channel/courses. It was about a year ago I started and completed the DP videos, but was disappointed to see no more , and randomly today this video pops up on my feed, very happy to see you again hopefully you will complete the rest of the patterns too 😊. I tried connecting you on LinkedIn too but it was restricted I think 😅.

  • @ppercipio
    @ppercipio Před 3 měsíci

    This is the first video I watch from you. It makes complete sense, no idea why I never though of it like this. Looking forward to more videos!!

  • @archindar5665
    @archindar5665 Před 3 měsíci

    you did really well with this video, i was able to flip into it and out of it, while just listening and you never lost me. i now know what you put into this video and thats always nice!

  • @securethebag1613
    @securethebag1613 Před 3 měsíci +1

    Word to mother....ur design patterns videos helped me nail an interview in 2017 🤣🤣🤣. Now I'm a Lead software engineer. Thanks

  • @observable-compositions-1996

    Thanks for coming back man

  • @hazzhaston4777
    @hazzhaston4777 Před 3 měsíci

    Welcome back! So excited to see you back! huge fan of your videos. Thank you!

  • @yveltdesamours5225
    @yveltdesamours5225 Před 3 měsíci

    Welcome back! Good to see you!

  • @ivankulaga5516
    @ivankulaga5516 Před 3 měsíci

    Oh hey look who's back! Thanks for the video! Ur gof patterns videos helped me a ton a few years back

  • @Ragnarok540
    @Ragnarok540 Před 3 měsíci

    Thanks for your work, I got a masters in software engineering 2 years ago and your videos on the design patterns really helped me.

  • @anda8369
    @anda8369 Před 3 měsíci

    Thanks for all of these and welcome back!

  • @wBkSlayer
    @wBkSlayer Před 3 měsíci +2

    Holy shit. You're back. And the beard is ever so magnificent. Don't you dare disappear for such a long time again.

  • @mohamedsalama2503
    @mohamedsalama2503 Před 3 měsíci +1

    Welcome Back, Great to see you after 3 years :D

  • @muntder1435
    @muntder1435 Před 3 měsíci +1

    it's been a very long time, nice to see again man!!

  • @mick2386
    @mick2386 Před 3 měsíci

    It's so good to have you back. Thank you 😊

  • @bryanscheetz2850
    @bryanscheetz2850 Před 3 měsíci

    Welcome back! Your videos helped me a ton while getting my degree - good to see you're still at it!

  • @dkvvit
    @dkvvit Před 3 měsíci

    Good to see you after 3 years. I love the way you teach.

  • @Indigo00757
    @Indigo00757 Před 3 měsíci +2

    No way!!! You are back! Glad to see!

  • @oganovdavid
    @oganovdavid Před 3 měsíci

    Happy to see you back, Chris!

  • @zakayothuku
    @zakayothuku Před 3 měsíci +1

    He's alive!!! Glad to have you back!

  • @hamzabenarfa7425
    @hamzabenarfa7425 Před 3 měsíci

    Thank you bro for coming back

  • @Jeff-xh4sy
    @Jeff-xh4sy Před 3 měsíci +1

    I used your design pattern videos to pass my cs class last term and here you are again lol

  • @sikchungliu83
    @sikchungliu83 Před 3 měsíci

    Welcome back! Good to watch your video again!

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd Před 3 měsíci

    Welcome back! Please upload more videos on system design and LLD. We appreciate your effort!

  • @shaunhunterit342
    @shaunhunterit342 Před 3 měsíci

    I didn't realise you hadn't posted for three years! I've been taking a break from coding but had to watch this video! Please keep posting!

  • @mohammadz5474
    @mohammadz5474 Před 3 měsíci

    Welcome back ! , good decision to come back , your content is wonderful

  • @ChrisCarlos64
    @ChrisCarlos64 Před 3 měsíci +1

    I did not expect to see this in 2024, but I'm glad to see you're back. 😁

  • @tomthetitan101
    @tomthetitan101 Před 2 měsíci

    This is one of the most engaging software videos I've ever seen

  • @atomic.rabbit
    @atomic.rabbit Před 3 měsíci

    Good to have you back!

  • @JeffJennings82
    @JeffJennings82 Před 3 měsíci

    I am determined to watch this video as many times as it takes to understand what he is talking about. 10 times later, maybe I should watch a refresher on polymorphism.

  • @zfold4702
    @zfold4702 Před 3 měsíci

    Welcome back, sir. Happy to know you are still creating content.

  • @SeanOchoa
    @SeanOchoa Před 3 měsíci

    Loved this video. Thank you so much for the bite-size CS training. Love your style, heart, and expertise.

  • @ramongus1996
    @ramongus1996 Před 3 měsíci

    I'm extremely happy you are back. Thank you.

  • @kodplanet
    @kodplanet Před 3 měsíci

    Happy to see you back Christopher :)

  • @AloisMahdal
    @AloisMahdal Před 3 měsíci +2

    Mr. Okhravi is BACK? Yaaaaaaay!

  • @Rhyrkon
    @Rhyrkon Před 3 měsíci +1

    Damn you're back! Thank you very much for the content!

  • @TruthAndLoyalty
    @TruthAndLoyalty Před 3 měsíci +2

    Good stuff. There are solutions without subtypes though.
    1. The example as stated could have `Move` that accepts a `name` as well as the individual effects.
    `new Move(name, healthEffect, armorEffect)`
    So attack would be `new Move('attack', -50, 0)`
    I'd only start packing in parameters in a language with named parameters though.
    2. You could have a method to add effects to a move as a fluent interface
    `addEffect(property, amount)`
    `new Move('heal').addEffect('health', 50).addEffect('armor`, 50).
    `Use` would just apply those effects to the target. Of course you'd need to do something about validating target properties.
    3. Still polymorphic, but passing a `use` function to the constructor of a `Move` class
    `new Move('attack', attackFunction) keeps your class hierarchy flat and allows you to define any behavior you want in the passed function. Do we really need an interface and subclasses? Maybe. Maybe not.
    Different behavior is just the first condition. For the subtype with polymorphism solution, that behavior has to be complex enough that it's simpler to extend. Class hierarchies add overhead / cognitive load. You need to be gaining enough from the strategy to offset that. This depends on a lot of things. The behaviors, how broad the set needs to be, how its expected to grow, even the language you're writing.

    • @user-sl6gn1ss8p
      @user-sl6gn1ss8p Před 3 měsíci

      Yeah, like, I think I'd just declare a simple struct with the effect data and pass it in, and expand it as things go (including "no effect" defaults so things don't break). If it does grow enough that this gets cumbersome, by then I'd have a better picture of what actually matters.
      Probably would still avoid subclasses tho : p

  • @nonickname1231
    @nonickname1231 Před 3 měsíci

    Previously, thank you for the knowledge, and welcome back.
    What I get from the explanation is:
    We should/must prefer to be implicit about a specific type of data that has the same structure and behavior, instead of being explicit (where we define it based on the actual concept or domain) about that type (CMIW).

  • @oleksandr.pastukhov
    @oleksandr.pastukhov Před 3 měsíci

    Good to see you again! Welcome back!

  • @MewMewCherry1990
    @MewMewCherry1990 Před 3 měsíci +1

    Finally! Didn't expect that. 3 years of silence

  • @sivaram7115
    @sivaram7115 Před 3 měsíci +2

    A video after 3 years. Waiting for many more

  • @felixjonsson4471
    @felixjonsson4471 Před 3 měsíci +1

    omg great to see u again! Ur videos has helped me tremendously during my time at chalmers, and im actually now TEACHING the same course i used ur videos to pass years before! Will recommend ur channel to all my students!

  • @iulianbojinca7309
    @iulianbojinca7309 Před 3 měsíci

    Welcome back! Thank you for this! The cliffhanger in the end...oh man, please don't let us hanging for too long

  • @RockyTheDog756
    @RockyTheDog756 Před 3 měsíci

    Glad that you come back

  • @aneeshthankachan57
    @aneeshthankachan57 Před 3 měsíci

    Finally you are back I missed your lectures, thank you sooooooooooooo much ...

  • @yamogebrewold8620
    @yamogebrewold8620 Před 3 měsíci

    Kul att se att du är tillbaka igen! Dina förklaringar av designmönster hjälpte mig enormt under skoltiden. 👍
    Man önskade att fler lärare var såhär pedagogiska.

  • @2005kpboy
    @2005kpboy Před 3 měsíci

    Good to have you back Chris ❤❤

  • @eugeniogonzato
    @eugeniogonzato Před 3 měsíci

    So nice to see you came back,thanks