• tiramichu@sh.itjust.works
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    I’ll say this now.

    Inheritance is the most misused capability of OOP which programmers think makes their code look smart, but most of the time just makes a giant fucking mess.

    • mesa@piefed.social
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 month ago

      Its the best/worst thing about OOP no matter what language.

      We had a rule at work that if you are 3 levels or more down an inheritance tree, then you are too far. The cognitive load is just too much, plus everything stops making sense.

      One level can be great (MVC all have great conventions, MCP as well). Two can be pushing it (Strategy pattern when you have physical devices and cant be connected all the time, Certain kinds of business logic that repeat hundreds of times, etc…) But even there you are kinda pushing it.

      I need code that I can look at a month from now and know WTF is happening. And sometimes its better to have less DRY and more comprehension. Or maybe im just a forever mediocre dev and dont see the “light”. I dunno.

      • fibojoly@sh.itjust.works
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        1 month ago

        PTSD flashbacks to the codebase I started on in 2008 which had… I don’t even remember. Like six or seven levels. Fucking nightmare. I did a printout of the analysis Doxygen gave me and it ended up as a 4x3 meters poster ;_;

      • Ethan@programming.dev
        link
        fedilink
        English
        arrow-up
        0
        ·
        1 month ago

        When I first started using Go I bemoaned the lack of true inheritance and classes. Now I love it.

    • red_tomato@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      Hold on, I’m in the middle of drawing an inheritance graph so I know how Dog is related to AircraftCarrier.

      • blackn1ght@feddit.uk
        link
        fedilink
        English
        arrow-up
        0
        ·
        2 months ago
        public interface ICanTravelThroughTheAir
        {
        
        }
        
        public class Flea : ICanTravelThroughTheAir
        {
        
        }
        
        public class AircraftCarrier
        {
          private readonly List<ICanTravelThroughTheAir> _aircraft = new();
        
          public void AddAircraft(ICanTravelThroughTheAir flyingThing)
          {
            _aircraft.Add(flyingThing);
          }
        }
        
        public class Dog : AircraftCarrier
        {
            public void Woof()
            {
                Console.WriteLine("Bitch I'm an aircraft carrier!");
            }
        }
        
        public static class Program
        {
          public int Main(string[] args)
          {
            var dog = new Dog();
            
            for (var i = 0; i < 10000; i++)
            {
                dog.AddAircraft(new Flea());
            }
        
            dog.Woof();
          }
        }