Master the Design of Functional Behavior in C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 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 TH-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.
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 54

  • @marklord7614
    @marklord7614 หลายเดือนก่อน +31

    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!

  • @fifty-plus
    @fifty-plus หลายเดือนก่อน +1

    The power of closures and delegates. Nice one.

  • @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!

  • @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.

  • @frosky9497
    @frosky9497 24 วันที่ผ่านมา +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

  • @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.

  • @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.

  • @ryanrobbins3846
    @ryanrobbins3846 หลายเดือนก่อน +1

    🔥 🤯

  • @jwjanse
    @jwjanse 20 วันที่ผ่านมา

    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  20 วันที่ผ่านมา

      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.

  • @mohammedabdalla6452
    @mohammedabdalla6452 27 วันที่ผ่านมา +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

  • @7th_CAV_Trooper
    @7th_CAV_Trooper หลายเดือนก่อน +1

    What books do you recommend on category theory?

  • @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.

  • @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.

  • @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.

  • @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.

  • @h.j.m4013
    @h.j.m4013 28 วันที่ผ่านมา

    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  28 วันที่ผ่านมา

      I would recommend taking F# to learn FP.

  • @1Eagler
    @1Eagler หลายเดือนก่อน

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

  • @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 👍🏽👍🏽

  • @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.

  • @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.