They Are Brilliant - But in Small Doses Only!

Поділитися
Вставка
  • Опубліковано 5 вер 2024
  • Only junior programmers use LINQ query syntax. Everybody knows that. Well, no, as it turns out that the most advanced programmers also use LINQ comprehensions regularly. How come?
    Here is the litmus test that will tell your current skills. If you believe that LINQ applies to IEnumerable, then you are only halfway there. LINQ applies to a wide range of types that satisfy specific criteria, IEnumerable included.
    This video sheds light on the dark side of LINQ. Step over and see the hidden mechanisms that comprise it as you have never seen them before.
    Become a sponsor to access source code ► / zoranhorvat
    Join Discord server with topics on C# ► codinghelmet.c...
    Enroll course Beginning Object-Oriented Programming with C# ► codinghelmet.c...
    Subscribe ► / @zoran-horvat
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    👨 About Me 👨
    Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my UA-cam channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⚡️COPYRIGHT NOTICE:
    The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorized to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.

КОМЕНТАРІ • 45

  • @PedroPabloCalvoMorcillo
    @PedroPabloCalvoMorcillo Місяць тому +21

    Astonishing. I stared into the abyss and the abyss stared back at me.

  • @nkesteren
    @nkesteren Місяць тому +17

    I don’t know how, but every video makes me feel smarter and more stupid at the same time.

  • @TazG2000
    @TazG2000 Місяць тому +8

    It's frustrating that we still can't do "from (x, y) in ..." or "let (x, y) = ...". This could also be extended by defining Deconstruct extension methods.

  • @shadowsir
    @shadowsir Місяць тому +6

    Mind = blown. I haven't used LINQ comprehensions in at least 5-6 years. Thanks for showing us its power 😁
    Will this still work with an EF Core query though? I guess not, because the entire query needs to be converted to SQL?

    • @zoran-horvat
      @zoran-horvat  Місяць тому +3

      This won't work with EF Core precisely for the reasons you stated. This coding method is reserved for functional models where you already possess objects and then start calling functions that may return monads, such as validation, lookup, all kinds of transforms with a negative branch, even tasks.

    • @danielarf4409
      @danielarf4409 2 дні тому +1

      I already have used EF Core with Linq and the result was a dynamic SQL on the fly. The base example in EF Core with log enabled you can see the SQL generated.

  • @AK-vx4dy
    @AK-vx4dy 6 днів тому +1

    From other videos i learned from your great practical experience... but this one...
    This one bend my mind literally.
    I have quite huge sql background but i never get in linq compenheshions although should be more familiar to me i always prefered chained version.
    But you made me look at them from diffrent perspective.
    Fun fact.... I was given book about linq before i know c#.
    I loved idea instantly, maybe because i dreamed about simillar scripting for file dataabses but less declarative than sql

  • @ghevisartor6005
    @ghevisartor6005 Місяць тому +2

    Ok the last part is incredibile. Gonna explore it more.
    And overall, im thinking that what im working on would benefit from this coding style.
    I have an in memory rappresentation of xml and i need to transform these tag and attributes in various objects. I have all these nested calls to extensions methods i ve made to map these collections of objects to each others.
    For example in one case i have xml nodes and each node have attributes and each attribute maybe have a value maybe not. If not then i need to either let it empty or take a value from some defaults which are also in a list of their own that are related to the node itself.
    The linq comprehension syntax would simplify this from what im seeing.

  • @marko5734
    @marko5734 Місяць тому +4

    Great Content as always

  • @abhishekbagchi6052
    @abhishekbagchi6052 Місяць тому +4

    Excellent just excellent. Will take me at least 15 repeat views to understand but its beautiful nonetheless.❤

    • @zoran-horvat
      @zoran-horvat  Місяць тому

      @@abhishekbagchi6052 Yes, it does take time to accept.

  • @cmdr_thrudd
    @cmdr_thrudd 12 днів тому +1

    I love your videos, they always inform and amaze me. :D

  • @metallixbrother
    @metallixbrother Місяць тому

    Regarding your Option implementation, I wrote something very similar, but instead I created an IOption interface with two concrete implementations (Some, None) and a Match extension method that allows us to treat it as a sort-of discriminated union via pattern matching. I think that it is very clear when looking at the code, but I worry if it’s maybe not as performant as the suggested implementation.

  • @PlerbyMcFlerb
    @PlerbyMcFlerb Місяць тому

    one suggestion from the trenches of using an option type in production for many years: Always make it a value type (i.e. struct) so that you avoid the pitfalls of the option itself being null! A 'default' option should be a 'none.'
    There are also performance considerations as well, but the correctness is the main reason i'd argue for this.

    • @zoran-horvat
      @zoran-horvat  Місяць тому

      @@PlerbyMcFlerb That makes sense. Some of my earlier implementations of the option were value types. This one was not meant to be really used, so I avoided all complications and focused on LINQ alone.
      However, it is not the "it could be null" argument in my opinion. If you catch a programmer setting an option to null or default, just hit them hard and they will learn. I mean... null? That's my attitude.

  • @neonmidnight6264
    @neonmidnight6264 11 днів тому

    These are rookie comprehensions. Most seasoned engineers in Python write this in 2 seconds and deliver list comprehension-driven applications to production with average algorithmic complexity of O(n!). Also, might as well use F# at this point :) These lambdas sure make DynamicPGO work really hard devirtualizing them...

  • @GlebAOsokin
    @GlebAOsokin Місяць тому +1

    "There is no way to know, whether the value is there or not..." - this is where convenience is the king. There is Option.is_some() in rust, Optional.isPresent() in Java, even isJust :: Maybe in Haskell. Moreover, in C# you can have bool IsSome() and bool IsSome(Func predicate) overloads. IsSome is indispensable when you need an early return.

    • @zoran-horvat
      @zoran-horvat  Місяць тому

      @@GlebAOsokin And tell me, what would you do with that?

    • @GlebAOsokin
      @GlebAOsokin Місяць тому

      @@zoran-horvat that depends entirely on surrounding code. Might be for unit testing:
      Assert.That(maybeResult.IsSome);
      Or an early return:
      var maybeResult = GetSomeData();
      if (!maybeResult.IsSome())
      return;
      // Remaining method body
      ...
      At the end of the day, those monads, options, results, etc. are just ways to express knowledge about something. And sometimes I'm interested not in data itself, but whether the data exists at all. There is also almost always a T Option.Map(Func onSome, Func onNone) or similar in all the implementations, exactly for unwrapping the maybes.
      As I'said, in this case practicality beats the mathematically correct form =)

    • @janbodnar7815
      @janbodnar7815 Місяць тому

      @@zoran-horvat
      int id = 3;
      String query = "SELECT name FROM cars WHERE id = ?";
      Optional res = jdbi.withHandle(handle -> handle.select(query, id)
      .mapTo(String.class)
      .findOne());
      res.ifPresentOrElse(System.out::println, () -> System.out.println("N/A"));

    • @zoran-horvat
      @zoran-horvat  Місяць тому

      @@janbodnar7815 That was not the use of IsPresent. Pattern matching is the intended use of monads, just as you did.
      P.S. There is a simpler form of your expression, where the optional string is first reduced to N/A if none and then printed unconditionally.

  • @MC_DarkMaster
    @MC_DarkMaster Місяць тому +1

    I'm interessted in the Result-Monad. Do you have a video for it?

    • @zoran-horvat
      @zoran-horvat  Місяць тому +5

      @@MC_DarkMaster Not yet, but I plan to make one.

    • @andriyzubyk629
      @andriyzubyk629 Місяць тому

      @@zoran-horvatit would be nice to see how to use this linq to chain methods that return Task

  • @elraito
    @elraito Місяць тому +3

    Is it safe to come out now? Are monadmonsters gone?

    • @zoran-horvat
      @zoran-horvat  Місяць тому +3

      I packed them into three lines of code. It's safe now :)

  • @jonasbarka
    @jonasbarka 12 днів тому

    This is called "LINQ query syntax" now and I think the change was made many years ago.

    • @zoran-horvat
      @zoran-horvat  12 днів тому

      @@jonasbarka It is called query syntax indeed, but it is also referred to as comprehension. The term comes from functional programming, most notably from its similarity to list comprehensions. Now, since comprehensions are an essential tool in functional programming, and some LINQ query expressions do precisely that, you will often hear functional practitioners using the term LINQ comprehensions for that form.
      Neither term works against the other. They both exist, but with slightly different intentions.
      For example, you would hardly ever think of using grouping or joining in a LINQ comprehension - what would the meaning of that be? Comprehensions solely depend on Select and SelectMany as the source of items.

  • @xhavierrhovira2858
    @xhavierrhovira2858 Місяць тому

    I have two questions:
    1) what would be printed in case first "from" returns none?
    2) what would be printed in case first "from" returns a book but second "from" returns None?
    Great video as usual, Zoran!

    • @zoran-horvat
      @zoran-horvat  Місяць тому +1

      In both cases the overall result is None. You can see it from the corresponding chained expression with SelectMany and IEnumerable data - nothing executes when the previous stage ended up with an empty sequence.
      LINQ comprehensions implement railway-oriented programming out of the box. Every stage that can produce a negative result has the power to move execution to the error track and everything beyond that point is skipped. Doing ROP on your own usually takes 2-3 times more typing.

  • @lennysmileyface
    @lennysmileyface 24 дні тому

    The word monad appeared in my brain just from seeing the video thumbnail.

    • @zoran-horvat
      @zoran-horvat  24 дні тому

      @@lennysmileyface Comprehensions are monadic. It seems to have burned onto your brain before.

  • @billy65bob
    @billy65bob Місяць тому

    I should probably have a play with F# or something, just so these functional concepts will look more like programming and less like witchcraft.

  • @isodoubIet
    @isodoubIet Місяць тому

    Why did you say in the beginning they're "only for beginners"? What makes the second syntax preferable to the first?

    • @zoran-horvat
      @zoran-horvat  Місяць тому

      @@isodoubIet Query syntax is alien to C#. Programmers normally abandon it as soon as they see how to use normal methods. I've never seen it used regularly, except in functional programming.

  • @figloalds
    @figloalds Місяць тому +1

    Wow, ngl this video was spectacular

  • @10199able
    @10199able Місяць тому +3

    it was a scary one 🕷🕸

  • @Mortizul
    @Mortizul Місяць тому +1

    this is exactly what language-ext does

    • @zoran-horvat
      @zoran-horvat  Місяць тому +1

      @@Mortizul Yes, it does that, as well as the whole bunch of other libraries. Unfortunately, many programmers don't know about that.

  • @FreaksSpeaks
    @FreaksSpeaks Місяць тому +1

    Insane.

  • @poloolo69
    @poloolo69 Місяць тому

    real af once again

  • @PetrVejchoda
    @PetrVejchoda Місяць тому +1

    Dark magic.