You May Have Never Learned This Lesson Right

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ •

  • @coderider3022
    @coderider3022 Рік тому +1

    12:20 is key bit. I probably over use them but love them. I get a bit frustrated at times having to add packages to give me more methods and namespace conflicts arise. The main ‘must have packages’ handle this differently.

    • @zoran-horvat
      @zoran-horvat  Рік тому

      I was driving to that point through the entire video. That indeed is the whole point.

  • @vyrp
    @vyrp Рік тому +4

    Just a detail, not related to the main point of the video:
    If `CompareTo` returns `int.MinValue`, negating it results in `int.MinValue` again.
    That's why I prefer inverting the arguments instead of negating the result when reversing the sort direction: `b.Height.CompareTo(a.Height)`.
    But anyway, most `CompareTo` implementations don't return `int.MinValue`, so this is not a big deal.

    • @zoran-horvat
      @zoran-horvat  Рік тому +1

      That is a good point and a reminder to stay away from extreme values in a range.

    • @krccmsitp2884
      @krccmsitp2884 Рік тому +1

      In checked mode you would get an overflow error at compilation time.

  • @lindermannla
    @lindermannla 8 місяців тому +1

    Excelent content. Congratulations!

  • @zoran-horvat
    @zoran-horvat  Рік тому +4

    Download the source code for this video: www.patreon.com/posts/source-code-for-93364170
    Enroll the *Advanced Defensive Programming Techniques* course ► codinghelmet.com/go/advanced-defensive-programming-techniques
    Learn more from the video course *Beginning Object-Oriented Programming with C#* ► codinghelmet.com/go/beginning-oop-with-csharp
    How to Avoid Null Reference Exceptions: Optional Objects in C# ► ua-cam.com/video/8-2xr_kBRnQ/v-deo.html
    Build Your Own Option Type in C# and Use It Like a Proua-cam.com/video/gpOQl2q0PTU/v-deo.html
    This Decorator Pattern Implementation Will Make Your Day! ► ua-cam.com/video/Pqow_rfuZSU/v-deo.html
    Clean Code Tip: Favor Method Chaining Over Nested Calls ► ua-cam.com/video/zWn0O0xzWMA/v-deo.html

  • @vulcanobyte
    @vulcanobyte Рік тому +3

    Thanks GOAT

  • @yanivrubin7202
    @yanivrubin7202 Рік тому +6

    I used to hate extension methods when they came out. Thought of them as just a glorified static methods, and static was the enemy. But with time learned to enjoy the way they make the code more readable. I think i am over using them now.

  • @thomasschroter3802
    @thomasschroter3802 Рік тому

    I just don't catch where the functional code in the Smallest function is. IComparer is an interface and thus "comparer" appears as an instance object and that is not functional at all?! Also, the sorting is a pretty weak example, as it can be replaced with built-in functionality....
    var list = new int[] {7, 9, 2, 6, 8, 1, 4, 5, 0, 3}; //etc.
    var top3 = list.Pick ( 3, (a, b) => (a < b) ? -1 :(a == b) ? 0 :1 );
    public static class SortingExtensions
    {
    public static IEnumerable Pick (this T[] list, int count, Comparison comparison)
    {
    var sorted = list.ToList();
    sorted.Sort (comparison);
    return (count

    • @zoran-horvat
      @zoran-horvat  Рік тому +1

      It is not important that a method is instance level to conclude it is not functional in languages such as C# or Java. Delegated are all instances and "calling" a delegate (such as Comparer) in C# is nothing but a call to its instance-level method Invoke.
      Java has the term "functional interface" for interfaces like IComparer. You could freely assign a Comparison delegate or even a lambda to a reference declared as IComparer.
      For a method to be substantially functional, it must satisfy the same conditions as in any functional language: to depend on no mutable state, make no observable side effects, produce stable output, things like that. You would be surprised to learn how large areas, and even entire libraries in .NET, are now built in functional style.
      BTW, your solution violates the requirements, which specifically say that the input is large. You should neither sort it, nor keep it in memory.

  • @silop
    @silop Рік тому

    Where do extension methods belong when working in a clean architecture/vertical slice architecture?
    Great video!

    • @zoran-horvat
      @zoran-horvat  Рік тому +3

      Extension method is just a function - it is up to you to decide when to call it and where from. When an extension method contains domain logic, and consequently belongs to one vertical slice, then you would probably want to inject it through a Func delegate when there is the need for variation. Otherwise, if it is an orthogonal concern, like methods from LINQ, then it can reside in a separate, reusable namespace and never belong to any vertical slice.

  • @sandromagalli8587
    @sandromagalli8587 Рік тому +1

    this is something i'm experiencing now in my third year as a programmer, before it was learning different technologies and actually complete projects, regardless of how much were they put togheter with tape. Now it's expecially the code i write, sometimes things being null when they shouldn't, checking for null and throwing exception because a method might return null in other cases but in this one it shouldn't! Then those extensions methods "ToDto" or putting them directly in the model class. Well let's try owned entities in EF Core! lol gotta be carefull to update them with reference when setting current values. What about complicated entities that are used in many places, should the sorting be done by the viewmodel on the Ui or always be the same in the model to ensure invariance?
    In other words your videos are the only ones online helping me to clear this confusion.

    • @zoran-horvat
      @zoran-horvat  Рік тому +1

      So many hard questions in one post...

  • @nickw656
    @nickw656 Рік тому

    why the properties FirstName and LastName of the model class Person are private?

    • @zoran-horvat
      @zoran-horvat  Рік тому +2

      Because the model started off as pure OO, and in OO we tend to hide the components and expose methods that use them (a.k.a. tell, don't ask).

  • @jonasng9554
    @jonasng9554 Рік тому +2

    My colleagues hate using extension methods because it is within static class, hence you cannot mock it... However, when the extension is put on the object that is mockable, that shouldn't be a problem, right? Professor Zoran I love functional programming, but my colleagues and company culture thinks we need to keep things as simple as possible, so when junior programmers come in, they wont have to learn all the functional secrets that we left behind. What us your thought on this??

    • @zoran-horvat
      @zoran-horvat  Рік тому +10

      They should mock things that vary. Static methods don't vary. Actually, the variation is implemented by selecting one static method or the other.
      Programmers who avoid static methods due to tests are missing the point entirely, both of methods and of tests.

    • @jonasng9554
      @jonasng9554 Рік тому +2

      As always, a well seasoned answer. Thanks!

  • @_NguyenManhToan_
    @_NguyenManhToan_ Рік тому

    ❤❤❤❤❤❤❤

  • @mocococo2877
    @mocococo2877 Рік тому +1

    I can't agree that complicating code for the sake of some imaginary biblical rules is justified.
    Especially in the second example where for just avoiding one if statement you clime the mount Everest and return back. I don't think that this is lighter on resource expenses and speed either.
    How in the world if statement breaks the whatever rule ? Except that you invented that rule and then you say it breaks it.

    • @zoran-horvat
      @zoran-horvat  Рік тому +1

      You are taking a demo and acting as if it were a production-grade application. It is not "an if". It is "ten thousand ifs" in a domain model.
      Now that we are on the same page, we can start talking programming. What was your concern, then?

  • @anm3037
    @anm3037 Рік тому

    LINQ is quite remarkable, but I avoid it like crime because of the horrible performance it has.

    • @zoran-horvat
      @zoran-horvat  Рік тому +12

      I think you should reconsider that because the performance of LINQ is often better than an alternative nowadays, and it is anything but horrible.