Master the Design of Functional Behavior in C#

Поделиться
HTML-код
  • Опубликовано: 30 май 2024
  • How do we implement functions in a class-based programming language, such as C#? The latest C# programming language specification offers a rich syntax that supports functional programming in this versatile language.
    Functional design is known for its simplicity and the short code it produces compared to other design techniques we apply. It is based on the separation of state and behavior, where we implement behavior in the form of functions.
    This video demonstrates the techniques we apply in C# when designing functional behavior on top of a set of functional types implemented via records.
    Download source code ► / zoranhorvat
    Join Discord server with topics on C# ► codinghelmet.com/go/discord
    Enroll course Beginning Object-Oriented Programming with C# ► codinghelmet.com/go/beginning...
    Subscribe ► / @zoran-horvat
    Watch related videos:
    The Ultimate Guide to C# Records ► • The Ultimate Guide to ...
    Master the Design of Functional Types in C# ► • Master the Design of F...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    👨 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 RUclips 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.
  • НаукаНаука

Комментарии • 54

  • @marklord7614
    @marklord7614 Месяц назад +33

    The real power of learning comes from understanding the application of concepts. This is where Zoran's content truly stands apart. We get to witness how the features of C# work in harmony in a way that not many other content providers demonstrate. And yes, it can be brain-twisting sometimes, but the things worth learning usually are. Take it from me, watch these videos multiple times and play with the concepts and you'll get so much better. I can't praise this content enough.

    • @zoran-horvat
      @zoran-horvat  Месяц назад +1

      Thanks!

    •  Месяц назад

      You can be brain-twisted, oh! Just imagine the world of juniors coming to be the programmers much faster than old programmers are retired. They use linq but don't understand how and why you can pass a function as an argument to a function.

    • @zoran-horvat
      @zoran-horvat  Месяц назад +2

      True. Functional programming assumes strong programming skills to begin with. I would never advise a beginner to try to dive into it, as that could lead to numerous misconceptions.

    • @7th_CAV_Trooper
      @7th_CAV_Trooper Месяц назад

      @@zoran-horvat I don't know. I think this would have been easier to learn for me in the 1990s before my brain got object orientated.

  • @demarcorr
    @demarcorr Месяц назад +11

    "let me twist your brain"
    consider it twisted, but highely intrigued

  • @goldmund67
    @goldmund67 Месяц назад +2

    Another masterpiece, Zoran. An 'Apply' extension on a delegate... This is pure Zen...

  • @brianm.9451
    @brianm.9451 Месяц назад +3

    I’ve been exercising functional paradigms in my work projects. Separating behavior from data allows me to test behavior regardless of data and state changes makes testing predictable. I had to watch this video a few times to drill the concepts in my head!

  • @Menicoification
    @Menicoification Месяц назад +6

    Follow up on my comment from some months ago regarding your style of presenting.
    I think you nailed it. For me, you've found the perfect balance with tone variation etc. Very nice to listen to!
    About the contents of your video: Great as always!
    I love it!

    • @zoran-horvat
      @zoran-horvat  Месяц назад +3

      Thanks! You will probably be happy to know that I am constantly reviewing my processes, from scripting, over recording, to editing, and that is in good part affected by the comments coming from people like you.

    • @Menicoification
      @Menicoification Месяц назад +1

      @@zoran-horvat I know ;).. That's why I wanted to follow up on my comment from some time ago. You're doing a very good job!
      I've been developing software for more than 25 years now, but it's always of great value to see how other developers think and incorporate some of their style into ones own.

  • @XXnickles
    @XXnickles Месяц назад +2

    It is the most interesting to me that the simple concept of passing functions to a function as parameters (which basically is what the demonstration is about) looks so complicated because of the language syntax. In fact, I got a grasp on the concept by learning a little of F# instead of starting to apply it in C#. The delegate definition trick to high complex function definition is neat, but first you need to really understand the concept of passing function to other functions (high order functions) As always, great explanation!

  • @WolfieVenturi
    @WolfieVenturi Месяц назад +1

    Fantastic video and teaching. Mind is truly bent. Look forward to more!

  • @damianradinoiu4314
    @damianradinoiu4314 Месяц назад +4

    A masterpiece video!

  • @fifty-plus
    @fifty-plus Месяц назад +1

    The power of closures and delegates. Nice one.

  • @reinhardlackner1925
    @reinhardlackner1925 Месяц назад +2

    I'm following the dark si..., just kidding, I am following the steady improvements of C# to make functional style programming in C# less verbose and align it more to the functional first langugage F#. From Language.Ext author Paul Louth to "Functional Programming in C# (@Manning)" of Enrico Buonanno to "Functional Programming with C# (@O'Reilly)" from Simon Painter, I am deeply grateful to Zoran for putting out his view on the application of the functional side of C#. I am eager to see where you are taking us.

  • @martinprohn2433
    @martinprohn2433 Месяц назад +2

    I think one important point of strong typing is is to prevent wrong assignments or wrong casts. Think about types you create to overcome positive obsession. Delegate behaves the same way as other type does. Therefore I don't think that this behavior, that strongly typed delegates cannot implicitly cast to other delegate types, will change in the future.
    I mean your example is a very good example. Think about it, you delegate has a name, a name that describes what the delegate does. Not consider you have a different delegate, with a different name, because it does a different thing. The computer prevents you from accidentally casting the wrong delegates to to the wrong purpose.
    Anyway, using .Invoke is a very good idea. I always used a lambs in this case. Never occurred to me, that I also could use Invoke.

  • @frosky9497
    @frosky9497 Месяц назад +2

    the only problem i have with functional programming
    is that it is impossible to debug in the larger scale
    going line by line is way more helpfull in that case
    Writing readable and debuggable code should be the goal of all of us

  • @jwjanse
    @jwjanse Месяц назад

    I love your functional C# videos and your clear explanations Zoran; the issue I run into is that - as a freelancer - when I introduce these concepts/code in the projects I work on, it is very hard for colleagues to understand and/or review, let alone maintain it after I have left the project/company. So there is a definite pushback to write more OO-style C#.
    Have you run into this and if so, how did you handle these situations?

    • @zoran-horvat
      @zoran-horvat  Месяц назад

      That is normal, since C# is an OO-first language. I think the best approach is to mix FP into an OO design. The principal step that every programmer must make is to adopt the immutable mindset. That is also the hardest step to make, so far as I can see.

  • @7th_CAV_Trooper
    @7th_CAV_Trooper Месяц назад +1

    What books do you recommend on category theory?

  • @mohammedabdalla6452
    @mohammedabdalla6452 Месяц назад +1

    while i watched the video, there's a part where i was confused is he writing c# code or javascript code
    i think i must read more from now on

  • @ryanrobbins3846
    @ryanrobbins3846 Месяц назад +1

    🔥 🤯

  • @Buutyful
    @Buutyful Месяц назад

    hi zoran! im missing the point of this demo example here, why dont implement the add unique method inside the book record directly?

    • @zoran-horvat
      @zoran-horvat  Месяц назад +1

      In functional design, we acknowledge that functions belong to behavior modules that need them. Not all functions defined in one type belong together.
      Therefore, I could test your question with a question: Why would you?

  • @victorchisomnwisu9776
    @victorchisomnwisu9776 Месяц назад +1

    Awesome video
    could using aliases like below help with avoiding to call the 'Invoke' on the delegate object
    using AuthorWithType = System.Func;

    • @zoran-horvat
      @zoran-horvat  Месяц назад +6

      That would mean you don't use strongly-typed delegates but only the Func delegates. The downside of that design is that arguments have no name, causing confusion in a complex model.
      Actually, the inability to assign strongly-typed delegates in an uncontrolled manner is a good thing. That adds type safety to functions and lambdas the same way classes add type safety to objects.

    • @BrianHallmanac
      @BrianHallmanac Месяц назад

      @@zoran-horvat Follow up question, I'm curious to know if there's a performance difference between a strongly typed delegate vs a Func. My guess is that it won't matter because the compiler will lower the Func to the "same" delegate type.

    • @zoran-horvat
      @zoran-horvat  Месяц назад +2

      @@BrianHallmanac From what I know, there is no difference. There is, however, a performance penalty if you assign a Func delegate to a strongly typed delegate via a lambda. You should avoid that whenever possible, because the call then consists of two virtual calls instead of just one.
      There is one magnificent improvement there in Rust, where the compiler can sometimes - and quite often - figure that there is just one lambda ending up in a delegate. It then turns that lambda into a statically resolved function, ending the resolution at compile time! I believe .NET will try something similar, if not working on it already.
      I remember a very old paper which concluded that 1/3 of execution time of C++ programs of that time was spent on virtual function resolution. Joggling with virtual calls has always been the curse of OOP, but also its greatest strength.

  • @HJM9x
    @HJM9x Месяц назад

    I want to learn how do fuctional programming in c#. How would you recoment doing this.
    Would you just try in c# or would it be better to learn a functional language and then return to c#.

    • @zoran-horvat
      @zoran-horvat  Месяц назад

      I would recommend taking F# to learn FP.

  • @Rein______
    @Rein______ Месяц назад +2

    OO (and DDD) has ingrained in me to protect invariants. How to prevent a invalid state transition when the `with` expression can freely do anything?

    • @zoran-horvat
      @zoran-horvat  Месяц назад

      That is easy. The with expressions cannot "freely do anything". It can only do what the containing function tells it to do.
      Isn't it the same with any mutable object? You can set its private state to whatever you want, but you would never emphasize that as an issue, right?

    • @Rein______
      @Rein______ Месяц назад +3

      @@zoran-horvat That thought did cross my mind, but then those mistakes can only be in one place, the class itself.

    • @metallixbrother
      @metallixbrother Месяц назад +2

      This was a really interesting video, and certainly a big eye opener. I was trying to do a lot of what is shown here with interfaces, but the delegate approach feels cleaner.

  • @1Eagler
    @1Eagler Месяц назад

    After lisp,xslt now i have to learn f.p. in c#?
    O tempora o mores.

  • @belongstomyself
    @belongstomyself Месяц назад +3

    Complicated yet interesting

    • @zoran-horvat
      @zoran-horvat  Месяц назад +3

      Functional design has a steep learning curve, but eventually takes you further up than object-oriented design. The end result is very powerful.

    • @pl4gueis
      @pl4gueis Месяц назад +1

      @@zoran-horvat OO also has a steep learning curve with all the rules, anti patterns you have to be aware of. So I wouldn't say its complicated. Its just a different way of thinking. If universities would also teach functional concepts people would have an easier time adjusting.

  • @0x4b55
    @0x4b55 Месяц назад

    We have been told if we want to write performant C# to limit memory allocations as much as possible, reuse objects, and use preallocated memory buffers. On the other hand, in functional programming, objects (records) are thrown at the gabage collector by design. Perhaps .net is not the ideal environment for the function-oriented approach?

    • @zoran-horvat
      @zoran-horvat  Месяц назад +1

      Garbage collection is a prerequisite for functional programming. I don't know who told you those things and what kind of software you are developing in the first place, but your observations do not apply to general programming. It could be true in games development or in embedded systems, but certainly not in general.

  • @TruptiDaliaTC
    @TruptiDaliaTC Месяц назад

    Hello Zoran,
    I try to understand the concepts of Functional Programming via your videos. I often wonder, the model you use have only 2 properties. In reality a model would have several properties. If a model has 10+ properties, would working like this for each property be a good idea ?? What do you think ?

    • @zoran-horvat
      @zoran-horvat  Месяц назад +1

      Actually, such models are frequent in object-oriented design, but not in functional. In functional design, we tend to group related concepts and represent them together, so a top-level type would consist of a few contained types, each consisting of a few other types, and so on. It is a true rarity to encounter a large type definition in a functional domain model.

    • @TruptiDaliaTC
      @TruptiDaliaTC Месяц назад

      @@zoran-horvat thanks for your prompt response. This clears to some extent. So, in OO design, for objects with fewer properties, we can implement functional design and use in other models? I am trying to implement small parts of Functional code in my OO code, but I find it difficult due to these concerns and give up.

  • @luc9volts
    @luc9volts Месяц назад

    Why a separate static class to create a booktype and not an extension method or a static method in the booktype?

    • @zoran-horvat
      @zoran-horvat  Месяц назад +1

      That is a common practice - put creation logic in one place, put each batch of behavior into its own place, etc.

    • @luc9volts
      @luc9volts Месяц назад

      @@zoran-horvat 👍🏽👍🏽

  • @thomaseyde275
    @thomaseyde275 Месяц назад

    I can easily find all implementations of an interface, then identify an offending implementation. Not so easy with delegates. How to fix that?

    • @zoran-horvat
      @zoran-horvat  Месяц назад

      I am not sure what you mean. The compiler reports assignment errors to delegates the same way as with anything else in your code.

  • @Alguem387
    @Alguem387 Месяц назад

    Partial aplication is equivalent to depency injection

    • @zoran-horvat
      @zoran-horvat  Месяц назад

      Partial application of other functions is equivalent to dependency injection. Partial application of values is equivalent to creating a stateful object.

  • @LE8271
    @LE8271 Месяц назад +3

    "let me twist your brain". Too late Buddy. That call is way too late. :)

  • @7th_CAV_Trooper
    @7th_CAV_Trooper Месяц назад

    In the 1700's you'd get burned for witchcraft coding with this black magic. I have to watch every functional video 3 or 4 times.