The One Thing You Cannot Do in Object-Oriented Programming but You Can in Functional

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 มิ.ย. 2023
  • Download the source code for this demo at Patreon: / one-thing-you-do-85222122
    In Object-Oriented Programming, objects are instances of classes and they encapsulate behavior and state - that is what every textbook on OOP is teaching. However, there's a subtle limitation in OOP when it comes to dealing with changes in the state of these objects.
    We often try to mitigate that limitation by applying the State design pattern, but that adds extra complexity and potential confusion to downstream components, especially when using Object-Relational Mappers (ORMs).
    In this video, you will learn to recognize the fundamental limitation of Object-Oriented Programming and how Functional Programming (FP) offers an inverse perspective. Through immutability and transformations of values, FP tends to make state transitions explicit and predictable, leading to a more straightforward way of handling the issues that would otherwise be too large a burden for a corresponding object-oriented model.
    This requires a shift in thinking and can introduce its own challenges and learning curves. It is a fascinating topic, shedding light on the fundamental underpinnings of OOP and FP, and their practical implications in software development.
    Thank you so much for watching! Please like, comment & share this video as it helps me a ton!! Don't forget to subscribe to my channel for more amazing videos and make sure to hit the bell icon to never miss any updates.🔥❤️
    ✅🔔 Become a patron ► / zoranhorvat
    ✅🔔 Subscribe ► / @zoran-horvat
    ⭐ Learn more from video courses:
    Beginning Object-oriented Programming with C# ► codinghelmet.com/go/beginning...
    ⭐ Collections and Generics in C# ► codinghelmet.com/go/collectio...
    ⭐ Making Your C# Code More Object-oriented ► codinghelmet.com/go/making-yo...
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⭐ CONNECT WITH ME 📱👨
    🌐Become a patron ► / zoranhorvat
    🌐Buy me a Coffee ► ko-fi.com/zoranhorvat
    🗳 Pluralsight Courses ► codinghelmet.com/go/pluralsight
    📸 Udemy Courses ► codinghelmet.com/go/udemy
    📸 Join me on Twitter ► / zoranh75
    🌐 Read my Articles ► codinghelmet.com/articles
    📸 Join me on LinkedIn ► / 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 TH-cam channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
    ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
    ⚡️RIGHT 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.
    ⭐For copyright or any inquiries, please contact us at zh@codinghelmet.com
    #csharp #objectorientedprogramming #functionalprogramming

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

  • @hemant-sathe
    @hemant-sathe ปีที่แล้ว +10

    I struggled with this issue with oop for many years but as i have learned a bit of accounting, it helped me crack the issue. If you think of invoice as an object, you feel that it has behaviour. But invoice is simply a record as you call it in functional programming. What does it record? It records the services provided or items sold or a combination of both (example your car mechanic invoice which includes labour charges and parts sold). The payment for an invoice is a separate record and needs to be tracked separately. You may be required at some point in the life of the app how the payment was done (cash, card, wire transfer etc). There may be partial payment or there may be payment of more than one invoice through single instrument like a bank cheque. The state of the invoice is two additional attributes, if it is paid and if yes on which date, come from a different data source and should not ideally map to same table. A transactional database and a reporting database are two different designs. So if you are checking only one of them. If you are checking paid status of the invoices, it's coming from reporting db and that's a read only data. The payment operation you do on the invoice goes to transactional db and it would at times take a while to reflect in reporting db. So this is not as simple problem to club all these concerns into one sample. Oop at times requires deeper thinking of the domain to solve the problem correctly.

  • @1992jamo
    @1992jamo ปีที่แล้ว +7

    I haven't even heard of a record type in c#, I assumed you'd use structs for the functional approach. Time to do some more googling.

  • @LE8271
    @LE8271 ปีที่แล้ว +8

    How does this solve the ORM problem? I see the same issue here. The type has changed. The object needs to update.

  • @kdakan
    @kdakan ปีที่แล้ว +3

    A function in a functional programming language is equivalent to a single method interface in object oriented programming. An object in object oriented programming is equivalent to a closure in functional programming. You don't really miss anything using one or the other paradigm, but ease of use will definitely be an important factor in choosing the paradigm to use. Applying SOLID principles in OOP makes your code more similar to functional code, that is small objects and small interfaces with small state and small number methods and with little or no subclasses.

  • @ClickOkYT
    @ClickOkYT ปีที่แล้ว +9

    I am not fully functional yet, but one thing I learn is that there are really BIG advantages in to separate "functionality" from "data". Rarely now my "Customer" class will have any methods, I will keep it very clean (99% properties) in order be easier to talk to Mappers, Orm, Mocks, etc, and most of functionality can be easily moved to Extension methods. Another thing that I find me doing is rarely creating deep hierarchies anymore: I think max 2 levels is very acceptable. C# is moving to a really great mix of functional and OOP, but using the best of both worlds!

  • @XtroTheArctic
    @XtroTheArctic ปีที่แล้ว

    Another question, why do you write Paid method in FP example as an extension method instead of a member of base Invoice type? Just because it looks good in this isolated state, or is there a technical (C# or compiler-wise) requirement that I miss to see?

  • @k0v4c

    This is coming from a beginner, but why have separate classes for each status of the object, instead of just having one class and have the status (paid, unpaid, etc) be a property?

  • @secury
    @secury ปีที่แล้ว

    Thank you so much for another video.

  • @seankirk2397
    @seankirk2397 ปีที่แล้ว +4

    Awesome video (as always)! I credit you to my increasingly growing interest of applying Functional patterns in C# after I watched your PluralSight course.

  • @franciscorusso8062
    @franciscorusso8062 ปีที่แล้ว +1

    This is pure gold!!!!

  • @istovall2624

    is it a naïve solution to just add a property? or was the example just for illustrative purposes?

  • @joga_bonito_aro

    I'm just spit balling here, but - when using EF Core - you would have to add these classes using a discriminator anyway, which in turn would create a discriminator column. I think it should, in theory at least, be possible to implicitly create a PaidInvoice from an Invoice, if you map the Dirscriminator as a property to your Invoice class and change it to "PaidInvoce". The change tracker would track that change and you can just call SaveChangesAsync. When you query that same entry again, you should get a PaidInvoice back from EF Core. Haven't tried it, but if it does, that would be very hackish I'd have to admit.

  • @vincentjacquet2927

    Interesting video but I might have missed something. The idea behind the State pattern is to decouple the class from its state. Therefore shouldn't you have Invoice and an associated InvoiceState? So what you specialize is the state, not the invoice: PaidState, OutstandingState, ... The behavior of the invoice will then change as its state changes, as if the invoice changed its class.

  • @1992jamo
    @1992jamo ปีที่แล้ว

    I'm not sure I understand the problem. Is it that having a nullable datetime in the base class would be undesirable because you'd then have check if it's null for other logic such as hiding/showing the button?

  • @chrisbaker5284

    HI Zoran, I do love these little snippet courses. I've been a fan of your style of teaching for many years through Pluralsight, first time to your channel though. I have used some of the functional programming styles from your courses on Pluralsight, particularly removing primitive obsession and immutable types.

  • @benqbenq
    @benqbenq ปีที่แล้ว +1

    whats your opinion on language-ext lib?

  • @AndersBaumann

    Hi Zoran.

  • @XtroTheArctic
    @XtroTheArctic ปีที่แล้ว +1

    Can you please simply explain why a nullable PaidOn property in a single Invoice type would be bad? I think it's a perfect model of a real-world invoice. Real-world invoice can be stamped with a red colored paid on this date and the null state of PaidOn property would mean the real-world invoice doesn't have the red stamp on it yet. What's wrong with this simple model? Why complicate it with multiple types like UnpaidInvoice and PaidInvoice?

  • @codingbloke
    @codingbloke ปีที่แล้ว

    This definitely an issue I have come across many times. The question in my mind is when will the ORM (or other persistence helpers) catch up with the FP approach. Right now if you are using EF the simplest and pragmatic approach would be to have the Invoice table have a nullable PaidOn column and let your C# model represent it that way too. This is not great OOP let alone FP.

  • @rotteneggconcept

    Development fundamental, ERD and DFD as a Starting point before coding, will reduce some of the complexities related OOP and domain knowledge required.