Visitor Design Pattern Is Giving Way To Pattern Matching Expressions!

Поделиться
HTML-код
  • Опубликовано: 29 ноя 2024

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

  • @zoran-horvat
    @zoran-horvat  Год назад +1

    Become a patron and get access to source code and exclusive live streams: www.patreon.com/posts/visitor-design-81382001

  • @iPazooki
    @iPazooki Год назад +24

    That UnreachableException was a great bonus!
    Thanks for your excellent content.

  • @sunnypatel1045
    @sunnypatel1045 Год назад +16

    I always struggle to find a use case for this pattern. You opened my eyes!

  • @kenbrady119
    @kenbrady119 2 месяца назад +1

    Excellent example of simplifying code - thank you! One of the barriers to my adoption of type-based pattern matching goes back to the earliest years of C#, when I was advised to use reflection sparingly. Pattern matching, or in this case type-testing if you will, is a form of reflection. It is evident that C# reflection is much faster than in the old days.

  • @pawel131657
    @pawel131657 Год назад +4

    Very accessible way of explaining programming patterns! Imo even better than nick chapsas channel :) You have my sub sire!

  • @theyuribanker
    @theyuribanker 4 месяца назад +2

    I love it!! I didn't know that Visitor Design pattern was function pattern.

  • @dmitrybunin9720
    @dmitrybunin9720 2 месяца назад +1

    Top quality content as always! Love your Pluralsight courses! Thanks for sharing.

  • @BoteeQ
    @BoteeQ Год назад +5

    Great video! I am waiting for other design patterns with practical use cases, especially those infrequently used.

  • @sudqi
    @sudqi Год назад +1

    This the same thing came to my mind when I've read why Accept method needs be added and do a double dispatch. Thanks for showing that here.

  •  Год назад +1

    Well, in traditional visitor compiler won't capture "a new class" error because the domain class hierarchy is required. Compiler will generate a call to superclass Accept method. No compile time, no runtime error will occurr, heavy debugging time...

  • @xrysav11
    @xrysav11 11 месяцев назад +1

    Just found, nice tutorial. Got to see more your videos. Thanks

  • @marcotroster8247
    @marcotroster8247 7 дней назад

    The visitor pattern helps processing complex tree structures. But yeah, that requires a tree structure first.
    People in compiler programming use visitors to translate program code (syntax tree) into machine code. Or you can replace flawed domain models using such a translation technique via fluent api syntax trees as well. I think the visitor pattern is still a powerful pattern for trees.

  • @mollybjork5499
    @mollybjork5499 Год назад +2

    Very nice video Zoran, good work!

  • @vladas6645
    @vladas6645 Год назад +1

    Quite effective video! I like the "fluency" or how well the writing code is aligned with the talk. The first thing that comes to me is to check if there's some analyzer that can ensure exhaustive pattern matching at compile time, perhaps that is good fit for larger projects?

  • @mdev3987
    @mdev3987 Год назад +2

    Thank you so much for this video, was interesting to see VDP!

    • @zoran-horvat
      @zoran-horvat  Год назад

      Visitor is by far the least frequently used pattern. But it is still irreplaceable in usages such as transforming expression trees and similar.

  • @milandjukic88
    @milandjukic88 Год назад

    Koji si ti kralj. Tako je lepo slusati te

  • @djpaulytee
    @djpaulytee Год назад +1

    Great video and format!

  • @majixx
    @majixx Год назад +1

    Amazing video. Thank you for sharing.

  • @koboldhelper
    @koboldhelper Год назад +2

    Between the traditional visitor and pattern matching approaches is there any significant performance difference? Is the pattern matching just compiler magic that is type checking under the hood (which you could do in older c# direct with “is” and cast) which is slower than a polymorphic call?

    • @HizusHiz
      @HizusHiz Год назад

      No, it converts to "as" construct with null checking for reference types. And these works faster than double virtual calls.

  • @adambickford8720
    @adambickford8720 Год назад +3

    I think the compile time error is a pretty big difference. I don't' know as its worth all the complexity and boiler plate just to prove its well-formed, but it is a difference.

    • @zoran-horvat
      @zoran-horvat  Год назад +7

      That difference is important in sensitive portions of code, like tree transformation visitors, library code, and similar. Take ExpressionVisitor in C# as a good example. It is virtually unimaginable that someone could transform expressions in .NET using any approach other than implementing the explicit visitor.
      On the other hand, most business applications do not have that problem. You would model business concepts as they appear and then define mappings to other concepts for years. Practice shows that adding variants to a business concept happens so rarely that you can safely ignore the nuisance that comes with that. How hard can it be to augment mapping expressions for a domain type once in two years? That is how often that happens!
      Of course, there are certain prerequisites to making it work that way. For one thing - discipline, discipline, discipline. Make every concept a small one. Build larger concepts by applying composition to smaller ones, not by deepening the hierarchies. Avoid hierarchies more than one level deep. Any hierarchy deeper than that must come with a rock-solid justification.

    • @adambickford8720
      @adambickford8720 Год назад +2

      @@zoran-horvat Just pointing out to viewers that the OO version is solving a 'harder' problem, it isn't just verbose for the sake of it.
      The real question is do you have this problem and even if you do, is it worth this price to solve? Probably not.

    • @Sindrijo
      @Sindrijo 9 месяцев назад

      This approach saves on complexity in the implementation but requires a special tool/testing to ensure correctness before deployment.
      It would be nice to be able to instruct the compiler that a type matching switch statement must be exhaustive over the derived types. Though it is probably not hard to write an analyzer for that.

  • @Quimoth
    @Quimoth 10 месяцев назад

    I think the main benefit for the visitor pattern is when you expect others to write their own visitors to walk your (sealed) composite structure. In LINQ there is an abstract ExpressionVisitor that you could use to translate the Expression to whatever you like, using whatever you want. You could ofcourse still opt for pattern matching instead when walking the Expression object but why would you if you could just have your class inherit from the abstract so you get all the different types in their own method with autocomplete from your IDE? This does seem like the sole reason for this pattern to stay around in C# however.

  • @furo.v
    @furo.v 9 месяцев назад +1

    Many design patterns are way easier to implement with functional programming. Strategy is just a function with the specified signature (params and return). My only complain here is that languages like Rust would not need the _ => wildcard for the matching pattern, since implementing all subtypes is already exhaustive in my opinion.

  • @joaomachado9105
    @joaomachado9105 Год назад +1

    your content is awesome, pretty glad i found it :D

  • @carlosboyanosky8044
    @carlosboyanosky8044 7 месяцев назад

    original visitor design pattern can leverage dependency injection? Given the functional pattern matching approach lacks of it that could mark a difference

    • @zoran-horvat
      @zoran-horvat  7 месяцев назад

      You can inject dependencies into functions, too. The pattern matching expression can operate within a closure.

  • @MoA-fm8ph
    @MoA-fm8ph Год назад

    @zoran-horvat Thank you very much for the demonstration of the visitor pattern, excellent work keep them coming,
    one question about the names, in most systems you get the names from a database or an API and then you map them to a single DTO like `Person` record but in your solution, you have 3 records `SimpleName`, `Mononym` and `CompoundName`. and for the pattern to work you need multiple DTOs to work
    How would you resolve this problem if you want to implement the Visitor pattern?

    • @Quimoth
      @Quimoth 10 месяцев назад

      For reference: ruclips.net/video/tq5ztZO45-g/видео.html
      The thing you need to ask yourself is whether the receiving party of your API cares which type of name the person is using.
      The visitor in this example is meant to return a string representation of the name object, regardless of its type. So your DTO, or receiving end would simply have a string property.
      In this case traffic would be one way and you would need a different approach if you were to ever allow creating or updating of the names.
      The Author API would probably care a lot about the types of names, but in the publications/books API you simply want to know the names.
      You could also change the return type of your visitor, exploring a different pattern/approach altogether might in the end be better.

  • @Darebit2k1
    @Darebit2k1 Год назад

    If we remove the "pattern matching" in the "implicit visitor" we'd have a "type checking" analysis into that method, Which would smell, and be a little confusing because besides, the proposed "implicit visitor" pattern looks more like the "factory method" pattern than the "visitor" pattern. That's my doubt.

    • @zoran-horvat
      @zoran-horvat  Год назад +1

      Type matching expressions are the basis of working with discriminated unions in functional programming and discriminated unions are the basis of domain modeling.
      It is interesting to note that the Visitor pattern is adding this technique to object-oriented languages, whereas that same technique is supported natively in any functional language. Therefore, the Visitor was introduced to overcome the native deficiency of OOP.
      As C# gains more functional elements, pattern matching, including type matching, will gain more and more prominence.
      Regarding code smell: it could turn into a code smell if target objects were mutable. In an immutable design, it is a mapping like any other.

    • @Darebit2k1
      @Darebit2k1 Год назад +2

      Professor Horvat thank you very much for your explanation, it was very enriching.

  • @crazyfox55
    @crazyfox55 7 месяцев назад

    One big thing you forgot to mention is that these are extension methods so its also possible to call them from the name itself. Name.CommonName()

    • @zoran-horvat
      @zoran-horvat  7 месяцев назад

      What do you mean forgot? I made that an extension method.

    • @modernkennnern
      @modernkennnern 4 месяца назад

      ​​@@zoran-horvatYou made them into extension methods, but you never mentioned it :)

  • @rezakaaccount
    @rezakaaccount Год назад +1

    SwitchExpressionException is better suited for this scenario as it can also capture the value of unmatched case.

    • @zoran-horvat
      @zoran-horvat  Год назад

      That is an interesting suggestion. I will consider it next time, though it has a drawback that it is telling out the implementation detail. But I see your point and I will give it some time of thinking.

  • @redcrafterlppa303
    @redcrafterlppa303 Год назад +1

    I think the visitor pattern plays well with java sealed class hierarchies. It would be nice to have something like that in c# as well.
    It's a powerful feature on its own allowing for example exhaustive enum like hierarchies similar to rusts enums.
    For the "implicit visitor" in particular it allows for a exhaustive switch without an catch all and giving you an compile error once someone adds a class to the sealed hierarchy.

  • @wertrager
    @wertrager Год назад +1

    Very similar to a catamorphism in F#

  • @dwhxyz
    @dwhxyz Год назад

    The idea of needing to know/remember to implement code somewhere else (along with nasty switch/if class type checking and exception throwing if it does not exist) once a new class implementation is added to the solution has always felt wrong to me. Its not something I personally would do with the only exception being if I was trying to extend existing classes which reside inside a third party library and even then I would probably do it a different way.

    • @zoran-horvat
      @zoran-horvat  Год назад

      Here is the answer I have already posted on a similar question:
      The problem of having or not having compile-time safety is important in sensitive portions of code, like tree transformation visitors, library code, and similar. Take ExpressionVisitor in C# as a good example. It is virtually unimaginable that someone could transform expressions in .NET using any approach other than implementing the explicit visitor.
      On the other hand, most business applications do not have that problem. You would model business concepts as they appear and then define mappings to other concepts for years. Practice shows that adding variants to a business concept happens so rarely that you can safely ignore the nuisance that comes with that. How hard can it be to augment mapping expressions for a domain type once in two years? That is how often that happens!
      Of course, there are certain prerequisites to making it work that way. For one thing - discipline, discipline, discipline. Make every concept a small one. Build larger concepts by applying composition to smaller ones, not by deepening the hierarchies. Avoid hierarchies more than one level deep. Any hierarchy deeper than that must come with a rock-solid justification.

    • @dwhxyz
      @dwhxyz Год назад +1

      @@zoran-horvat I agree that in reality adding another implementation may be infrequent or never happen but it still feels sloppy to me. I would say there is a case that the matching approach in the video breaks OCP but even I would say nothing is ever perfect and there could be times where breaking a SOLID rule(s) might be ok. Anyway, I'm really enjoying your videos - you're a very clever guy who deservers more subs/views.

  • @obinnaokafor6252
    @obinnaokafor6252 Год назад +2

    Thank you.

  • @alxjones
    @alxjones Год назад +1

    The fact that this is not checked at compile-time creates an unnecessary cost, and a cost which is atypical for functional-style design. An unreachable exception is just a run-time crash for something you failed to check at compile-time, and should be considered a code smell. This seems to be a symptom of squeezing a functional pattern into an object-oriented context. A functional language would have PersonalName as a sum type, and so it could be checked at compile-time if all the summand types are handled in pattern matching. I'm not sure that this is impossible to check for the class hierarchy of OOP, but it's clear that C# at least isn't interested in doing that check.

    • @zoran-horvat
      @zoran-horvat  Год назад +1

      Everything you said is right - to a point. By requesting compile-time safety, you are dismissing entire programming languages. C# is going this path for the last 15 years already and you can expect that all remaining deficiencies in its current state are temporal.

    • @alxjones
      @alxjones Год назад +1

      @@zoran-horvat I'm not trying to be a stickler about compile-time checking everything. I will always prefer it, but I understand that there are reasons to not have it, and some people like those reasons enough.
      My issue is that we took something that can be compile-time checked in OOP (interfaces), replaced it with a feature that can be compile-time checked in FP (pattern matching), and somehow ended up with something that isn't compile-time checked. It seems like a waste to not be able to check this.

    • @zoran-horvat
      @zoran-horvat  Год назад +2

      @@alxjones I see your point, and indeed the things will settle down once we get discriminated unions in C#.

    • @ppasieka
      @ppasieka Год назад

      And this is precisely why I still prefer to use the Visitor pattern explicitly. The compile-time checks are very compelling 😇