How Function Composition Can Make Your Code Better | Functional Programming

  Рет қаралды 14,244

Milan Jovanović

Milan Jovanović

Күн бұрын

Пікірлер: 63
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@soverain
@soverain Жыл бұрын
The second approach is super clean, removes all the boilerplate that comes from static methods. I very much appreciate your videos about functional programming in C#, keep up the good work.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you very much!
@adiazwise
@adiazwise Жыл бұрын
Great Milan , you explanation was very clear.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad it was helpful!
@birukayalew3862
@birukayalew3862 Жыл бұрын
Thank you Milan. I prefer the second one because it's more readable and we can passed many validations at once.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
They both end up doing the same thing though
@andreibicu5592
@andreibicu5592 8 ай бұрын
Hi Milan. Another great video. I have one additional suggestion. If the email is null or empty, we need to check on all predicates against it, otherwise we get null reference error. Instead of repeating the check, I would advise to add the check in the Ensure method like this, and the call stays the same as you showed. public static Result Ensure( T value, (Func Predicate, Error Error) hasValue, params (Func Predicate, Error Error)[] functions) { if (!hasValue.Predicate(value)) { return Failure(hasValue.Error); } ... } What do you think?
@MilanJovanovicTech
@MilanJovanovicTech 8 ай бұрын
How would that change the overall API usage?
@andreibicu5592
@andreibicu5592 8 ай бұрын
@@MilanJovanovicTech The usage remains the same. But in your case, you did not test for a nullable email. In such case, everything will crash.
@ksofficerofficer7858
@ksofficerofficer7858 5 ай бұрын
@@andreibicu5592 I just started programming when tf I'm gonna understand what ya'll talking about !
@krzysztofhandzlik9273
@krzysztofhandzlik9273 Жыл бұрын
Very nice and clean. That would be usefull in many cases, thanks.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanm you!
@Tamer_Ali
@Tamer_Ali Жыл бұрын
Thanks Milan, you awesome I hope you talk about Vertical Slice Architecture and compare it with Clean Architecture.
@AboutCleanCode
@AboutCleanCode Жыл бұрын
these are not mutable exclusive - i always organize my code base "vertically" and inside each "slice" i use clean architecture setup ;-)
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I still need to do more research on VSA to be able to deliver on this, but it will happen
@AndriyBezkorovayny
@AndriyBezkorovayny Жыл бұрын
@@MilanJovanovicTech, great explanation, thanks! What do you think about complexity? I am using this approach in my private projects, but I have been banned from using them at work due to their complexity. Can you suggest how to cope with that?
@mrvastayan
@mrvastayan Жыл бұрын
Great video mate, thank you!
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad you liked it!
@angelldark6426
@angelldark6426 Жыл бұрын
I like both options, but would like more examples using class Rusults. I want more practice. Thanks
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Look for some Railway-oriented programming videos
@dusrdev
@dusrdev Жыл бұрын
About the second approach, it is rather unoptimized, firstly because the parameter is a params array object, in the function you can check the length of it and create an array of results to match the length, and use a for loop instead of the foreach, then pass the array to the combine method. This will entirely eliminate the List allocation, because you are already allocating a new array with the ToArray method which you could then remove. Or even more simple, without using the loop, run over the params object with a .Where method, and pass the Enumerable to the combine. For even more efficiency, you could modify the check to add an object only if an error occurred, and combine that with a .Where method.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
You're welcome to optimize it as much as you want. Great suggestions. 😁
@TT-nh3qt
@TT-nh3qt Жыл бұрын
The title is "How Function Composition Can Make Your Code Better | Functional Programming" ...
@dusrdev
@dusrdev Жыл бұрын
@@TT-nh3qt the suggestions I made don't make it any less functional, most don't even require changing the API.
@greyshopleskin2315
@greyshopleskin2315 Жыл бұрын
This sounds like an obsession with premature optimization. Indeed, great suggestions about performance.
@gardnerjens
@gardnerjens Жыл бұрын
i like the idea of having an array of tuples one question, would it not be more functional had you use Aggregate instead of foreach? since you are creating a new type, then folding over the array of tuples you could then pass in the value t, to each function?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Yes it would :)
@microtech2448
@microtech2448 Жыл бұрын
Great video
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thank you! I see you're brushing up your FP skills 👌
@AmirHashemZadeh
@AmirHashemZadeh Жыл бұрын
it was perfect thanks
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad you liked it!
@darwish1337
@darwish1337 5 ай бұрын
hello thank you its was useful , can i ask you how i can learn how i create my own extenision result ? and Function Composition?
@MilanJovanovicTech
@MilanJovanovicTech 5 ай бұрын
Copy my code and iterate?
@andrewseaton5727
@andrewseaton5727 Жыл бұрын
Another great video! Thanks
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Glad you enjoyed it!
@antonmartyniuk
@antonmartyniuk Жыл бұрын
I like the 2nd approach more. Looks much cleaner and has less code
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I also like it more
@mahmmoudkinawy2783
@mahmmoudkinawy2783 Жыл бұрын
Hello Milan, where to get resources to write a FP code such you.
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Functional Programming in C#: How to write better C# code 1st Edition by Enrico Buonanno
@alberthalbert1153
@alberthalbert1153 Жыл бұрын
Thoughts on making videos showing how to build good WPF applications?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I have no idea how to build a good WPF application 😅
@tienbui2814
@tienbui2814 Жыл бұрын
What’s the Result library you used?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Not a library, it's something I created on my own
@tienbui2814
@tienbui2814 Жыл бұрын
As you said. I had seen full the video and see the Class Result. I think it very helpful to handle exceptions. ❤
@alexlo5655
@alexlo5655 Жыл бұрын
There is a very popular lib which has everything github.com/vkhorikov/CSharpFunctionalExtensions
@AmirHashemZadeh
@AmirHashemZadeh Жыл бұрын
@@MilanJovanovicTech please share it on github or other repos.
@AmirHashemZadeh
@AmirHashemZadeh Жыл бұрын
how could i get the ResultExtensions class?
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Try this: github.com/m-jovanovic/expensely-server/tree/main/src/Expensely.Common.Primitives/Result github.com/m-jovanovic/expensely-server/blob/main/src/Expensely.Common.Primitives/Result/ResultExtensions.cs
@10199able
@10199able Жыл бұрын
You are getting closer and closer to F# :D
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
But I won't make the switch 😁
@AboutCleanCode
@AboutCleanCode Жыл бұрын
@@MilanJovanovicTech maybe u should consider it if u like functional programming as it is much more powerful for FP ;-)
@EdsonViniciusSchmitt
@EdsonViniciusSchmitt Жыл бұрын
Hey there! On your thumbnail ... composition is spelled wrong ... to me it shows as "comopsition"
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Lol fixed it, thanks! 😂
@EdsonViniciusSchmitt
@EdsonViniciusSchmitt Жыл бұрын
@@MilanJovanovicTech great video btw! Keep it up!
@sauravbhatta5303
@sauravbhatta5303 Жыл бұрын
Great Keep adding azure cloud stuff
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
Thanks, will do!
@davlatbek2202
@davlatbek2202 Жыл бұрын
how can i find this reposiory
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
www.patreon.com/milanjovanovic
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I share the code on my Patreon
@davlatbek2202
@davlatbek2202 Жыл бұрын
@@MilanJovanovicTech thank you
@eloherbapol
@eloherbapol Жыл бұрын
Nice video! But stop abusing the phrase "I'am going to" every 3 seconds :D
@MilanJovanovicTech
@MilanJovanovicTech Жыл бұрын
I wish I could control the way I speak 🤣
Functional Programming With C# Using Railway-Oriented Programming
19:15
Milan Jovanović
Рет қаралды 25 М.
indian coding tutorials be like
1:40
NEFOS
Рет қаралды 1,8 МЛН
У вас там какие таланты ?😂
00:19
Карина Хафизова
Рет қаралды 15 МЛН
ПРЯМОЙ ЭФИР. Золотой мяч France Football 2024
4:41:06
Will A Basketball Boat Hold My Weight?
00:30
MrBeast
Рет қаралды 131 МЛН
Why Favor Object Composition Over Class Inheritance? A Deep Dive
19:00
How To Create Smart Enums in C# With Rich Behavior
17:31
Milan Jovanović
Рет қаралды 55 М.
Dear Functional Bros
16:50
CodeAesthetic
Рет қаралды 543 М.
The Absolute Best Intro to Monads For Software Engineers
15:12
Studying With Alex
Рет қаралды 662 М.
Functional Programming with C# - Simon Painter - NDC London 2023
1:09:05
NDC Conferences
Рет қаралды 17 М.
How to Avoid Null Reference Exceptions: Optional Objects in C#
18:13
How To Learn A New Programming Language
6:24
ThePrimeagen
Рет қаралды 273 М.
У вас там какие таланты ?😂
00:19
Карина Хафизова
Рет қаралды 15 МЛН