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 Жыл бұрын
That is a good point and a reminder to stay away from extreme values in a range.
@krccmsitp2884 Жыл бұрын
In checked mode you would get an overflow error at compilation time.
@zoran-horvat Жыл бұрын
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# ► kzbin.info/www/bejne/bl6VqaWVoKeFpLM Build Your Own Option Type in C# and Use It Like a Prokzbin.info/www/bejne/naGygp9oppWDirc This Decorator Pattern Implementation Will Make Your Day! ► kzbin.info/www/bejne/hqLSqJKom9qNibc Clean Code Tip: Favor Method Chaining Over Nested Calls ► kzbin.info/www/bejne/sIjRYYJmrd-Kg6M
@yanivrubin7202 Жыл бұрын
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.
@iquisickis21033 ай бұрын
Same here!
@coderider3022 Жыл бұрын
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 Жыл бұрын
I was driving to that point through the entire video. That indeed is the whole point.
@lindermannla7 ай бұрын
Excelent content. Congratulations!
@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 Жыл бұрын
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.
@vulcanobyte Жыл бұрын
Thanks GOAT
@silop Жыл бұрын
Where do extension methods belong when working in a clean architecture/vertical slice architecture? Great video!
@zoran-horvat Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
So many hard questions in one post...
@jonasng9554 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
As always, a well seasoned answer. Thanks!
@nickw656 Жыл бұрын
why the properties FirstName and LastName of the model class Person are private?
@zoran-horvat Жыл бұрын
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).
@_NguyenManhToan_ Жыл бұрын
❤❤❤❤❤❤❤
@mocococo2877 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
LINQ is quite remarkable, but I avoid it like crime because of the horrible performance it has.
@zoran-horvat Жыл бұрын
I think you should reconsider that because the performance of LINQ is often better than an alternative nowadays, and it is anything but horrible.