Why replace if statements with Objects

Поділитися
Вставка
  • Опубліковано 7 чер 2024
  • Examples of when replacing if statements with objects yields better software.
    Support my Work 🤝 / raw_coding
    Buy my Courses 📚 learning.raw-coding.dev
    Buy my Merch 🛒 shop.raw-coding.dev
    Join the Community 💬 / discord
    Tweet Tweet 📣 / anton_t0shik
    Tune in LIVE! 🎥 / raw_coding
    #programming #csharp

КОМЕНТАРІ • 25

  • @WookashWackomy
    @WookashWackomy 3 місяці тому +48

    Thank you Moses. Please keep guiding me through the sea of programming.

  • @SnowtoadVRDev
    @SnowtoadVRDev 3 місяці тому

    Mind blown. I had just never thought about it that way. Thanks.

  • @sunnypatel1045
    @sunnypatel1045 3 місяці тому

    Love to see a video about injecting interfaces in DI with a bit of reflection or something smart with functional programming.

  • @p4xx07
    @p4xx07 3 місяці тому +4

    aaa white theme. I'm blind now

  • @fr0ntez
    @fr0ntez 3 місяці тому

    Hi, it seems that discord link doesn't work anymore

  • @_grzehotnik
    @_grzehotnik 3 місяці тому +10

    if you have an if with 2 options there's really no need to do that. Each class you create, each file you have to open increases cognitive load needed to read the code, and then instead of having one class with concise logic, you end up with 3 files that you have to open to understand the flow. Needless splittingof you code which doesn't give you much in return. Just stay with a simple if, don't overengineer.

    • @RawCoding
      @RawCoding  3 місяці тому +2

      Mhmmm, so when you build an Api, you have 1 endpoint, call it for each parts of your application and then use if statements on the payload to determine which logic to execute further? (1 big file = no cognitive load?)
      No you split it up into manageable chunks. I explain that you want to split it up based on business functions admin/customer etc… as a symptom for a module doing too much is alot of if statements. Splitting it up is also safer because introducing a change wont break other parts of your application.

    • @_grzehotnik
      @_grzehotnik 3 місяці тому +1

      @@RawCoding If you split up business logic within on flow, something that is logically cohesive, you introduce more congitive load than needed.
      >Mhmmm, so when you build an Api, you have 1 endpoint, call it for each parts of your application and then use if statements on the payload to determine which logic to execute further?
      Depends. If there's a totally different flow depending on the payload, i'd use differnt objects/functions to handle that or even use different endpoints. But the if would still be necessary, whether I'd check the payload, or make someone choose an endpoint or even use something like keyed services.
      If it's something like subset/superset, e.g. user who executes some action and we do additional stuff depending on whether they're an admin or not, I'd have a User.cs class with a flag and then handle that request with a specific user and check whether they have right or not. In an if.

  • @speakoutloud7293
    @speakoutloud7293 3 місяці тому

    Great video, as it has become more likely like typescript.

  • @pieDesert
    @pieDesert 3 місяці тому

    This is good stuff

  • @isakoveugeneful
    @isakoveugeneful 3 місяці тому +2

    Embedded interface. First time seeing this. Not sure what to think of it.

    • @RawCoding
      @RawCoding  3 місяці тому +2

      Haha, yeah that was just a quick thing dont do that

  • @cocoscacao6102
    @cocoscacao6102 3 місяці тому +2

    2nd example is kind of an overkill. I get the point, but in this case, passing a selector function as a parameter was enough.

    • @RawCoding
      @RawCoding  3 місяці тому +1

      So you’re still replacing the if statement with an object, the downside of that is that you’re expecting the selector function to be the only thing that is different between the two.

  • @giorgidgebuadze219
    @giorgidgebuadze219 3 місяці тому

    Could you please make a video about autofac?

  • @SilverFerrum
    @SilverFerrum 3 місяці тому +11

    how to create problems out of thin air

    • @IronDoctorChris
      @IronDoctorChris 3 місяці тому +3

      The first 2 examples are completely fair imo, I would almost never go for the 3rd one though

    • @RawCoding
      @RawCoding  3 місяці тому +1

      Think of the 3rd example as a kin to Regex, where our expression was a tuple

  • @robertmrobo8954
    @robertmrobo8954 3 місяці тому +1

    KISS

  • @stephenmooney1293
    @stephenmooney1293 3 місяці тому

    @RawCoding this is just not necessary in most cases and in some cases it cannot be done. Even the founders of stack overflow have said they see no reason for it and prefer if statements

  • @tofu1687
    @tofu1687 3 місяці тому

    I had to check if it was the first of april - but no

  • @aephyxen8437
    @aephyxen8437 3 місяці тому

    😐

  • @bighat6501
    @bighat6501 3 місяці тому +3

    My man DARK MODE please... at least a warning that viewer retinas are about to be scorched. 🫣