Тёмный

Functional Programming In C#: Refactoring Tip 

James Hickey
Подписаться 643
Просмотров 15 тыс.
50% 1

Struggle with functional programming? Here's a small tip to get started thinking about solutions using functional techniques.
Using C#, we will look at a functional programming technique known as "outside-in refactoring." Basically, you can take a piece of code that is often duplicated in a way that "wraps" other logic and extract it into a reusable function.
Based on my blog post @ blog.jamesmich...
The post was later featured on the MSDN Dev. blog @ blogs.msdn.mic...

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 38   
@sebastianfjastad7928
@sebastianfjastad7928 5 лет назад
Guilfoyle CAN code
@baz_sh
@baz_sh 6 лет назад
The functional decorator pattern! :D Nice. Love your content, please keep them coming.
@irrefl1672
@irrefl1672 3 года назад
What up Guilfoyle, nice to meet you in real life.
@enihar
@enihar 6 лет назад
simple yet excellent example. loved it
@jameshickey84
@jameshickey84 6 лет назад
Thanks for watching! Glad you enjoyed it.
@chefbennyj
@chefbennyj 4 года назад
Where's Anton?
@jameshickey5774
@jameshickey5774 4 года назад
😂
@rafaelbitencourt6021
@rafaelbitencourt6021 2 года назад
Nice video... it's an old video, but was interested by its title now that I'm having to check on C# for work... by the looks of your examples, those DoExpensiveOperationX could be passed directly (point free) as the action when you don't need to nest them, ie.: `FPStopWatch.LogMs("Took {0} ms", DoExpensiveOperation);`.
@about2mount
@about2mount 4 года назад
Nice presentation and informative. However, what you are actually doing is called "Procedural Paradigm Programming" and not Functional Paradigm Programming. Functional Programming literally means to evaluate repetitive task using loop driven algorithms with math. Also Procedural is well established and accepted as Function Oriented. C# is not a “functional” programming language, because it doesn’t fully support all of the functional programming paradigm requirements. (“Functional programming” is an actual industry term, while “function-oriented” is not.). Here is a small example of Functional Programming that C# can use for creating ten GUI WxButtons using Functional Programming algorithm and its iterate-able math; var WxButtons = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int count = 0; foreach (int NumButtons in WxButtons) { count++; WxCreate.WxButtonWidget(master="WxMain.Window", ButtonText=ToString(ButtonConstantList [bEnum]) ); } No Offence I began programming in 1974 and thought you should know the difference. C, C++, C# are all considered to be Functional Oriented underneath which means Procedural. And C++ and C# can be both Procedural or Object Oriented done with all functions using Structs or Typedef data structures and or class's. On the other hand many schools have and are teaching this wrong online on many websites. Python because it is uses a C base is much like C# in that it can do all three Paradigms. But Guido is a master of the C languages and he explains this fully in his Python Docs likewise. Thanks, its good to see others into programming.
@jameshickey84
@jameshickey84 4 года назад
Thanks for the detailed comments Robin 👍
@prasadhkumarjadhav4066
@prasadhkumarjadhav4066 Месяц назад
is there any sources where we could learn better coding or refactoring tips and guidelines for C# likes this wonderful one
@jameshickey84
@jameshickey84 Месяц назад
I wrote a book that shares many of these kinds of tips. It's focused on Typescript, but the tips apply to C# too: leanpub.com/refactoringtypescript Otherwise, keep an eye out for great C# blogs by developers who are posting good tips. A couple that I like are: - andrewlock.net/ - www.stevejgordon.co.uk/
@BlazarVision
@BlazarVision 4 года назад
This is excellent.
@fj8551
@fj8551 2 года назад
Thanks James, great explanation!
@jameshickey84
@jameshickey84 2 года назад
Thanks!
@zerosypher0114
@zerosypher0114 2 года назад
You can Ramp up the speed in the settings of the vid.
@radiagulzan
@radiagulzan 4 года назад
wow you have great examples in this channel!
@jameshickey84
@jameshickey84 4 года назад
Thanks!
@Linkario86
@Linkario86 4 года назад
I think I might accidentally mix Functional Programming in my generally Object Oriented Programs. Just trying to keep stuff simple
@jameshickey84
@jameshickey84 4 года назад
lol Sometimes functional techniques can simplify your code, sometimes OOP can 🤷‍♂️
@MartinHAndersen
@MartinHAndersen Год назад
This was a real good example
@Grymyrk
@Grymyrk Год назад
This is exactly what I would do, never considered that it was functional programming.
@nickbarton3191
@nickbarton3191 5 лет назад
Is this what is called Currying, higher order functions?
@jameshickey84
@jameshickey84 5 лет назад
No, currying and higher-order functions are two different things. Currying takes a method that has multiple parameters and replaces that one method with a chain of methods that can be composed together. It's easier to understand if you see it ;) I'll shoot to do some videos on this in the new year 👍
@nickbarton3191
@nickbarton3191 5 лет назад
@@jameshickey84 yes please.
@mattmarkus4868
@mattmarkus4868 4 года назад
no, it's not, unfortunately
@macfley
@macfley Год назад
are you secretly Martin Starr?
@jameshickey84
@jameshickey84 Год назад
Yes 🤫
@luiscencio
@luiscencio 6 лет назад
What IDE are you using?
@jameshickey84
@jameshickey84 6 лет назад
Hey Luis, I've been using Visual Studio Code in Ubuntu for these videos :)
@salimalamcse
@salimalamcse 3 года назад
More on functional programming.
@hossamkhaled7779
@hossamkhaled7779 3 года назад
can you send me the code please
@yashshah6919
@yashshah6919 3 года назад
Thank You Sir.
@rongliao9255
@rongliao9255 4 года назад
Great tutorial! Thanks a lot!
@jameshickey84
@jameshickey84 4 года назад
Thanks!
@Vlfkfnejisjejrjtjrie
@Vlfkfnejisjejrjtjrie 4 года назад
Excellent stuff.
@mikeypox
@mikeypox 5 лет назад
I am not going to downvote, but when you said "This is obviously not production code" you were right. If I call a function, I want a result. Maybe I want the statistics / error or log messages to piggyback with them. Doing an expensive operation that doesn't return a result, isn't that like the definition of a side-effect -- the primary reason functional programming strategies exist to avoid? I am still trying to wrap my brain around "functional programming" (hence no downvote), but I really feel like this video muddles more than it helps. Correct me if I am wrong (I want to learn).
@jameshickey84
@jameshickey84 5 лет назад
One of the examples I gave actually was returning a result - as you mentioned. LogMs allows you to wrap an anonymous method and capture the results. One of the focuses of functional programming is, as you said, to minimize side-effects. That's because functions with side-effects are hard to test and therefore aren't predictable. But functional programming has other focuses/benefits beyond this. It allows new ways to build abstractions that either may not be available in other paradigms or are just way easier to do in FP -> composition (using currying for example), expressions over statements, higher-order functions (which allow you to scope/inject dependencies), etc. I've got a few videos on these topics if you're interested 👍 There may be cases when you need to use a higher-order function that returns void. For example, perhaps the "expensive operation" grabs some data from a database, does some intensive processing on them and then pushes them back to the database. Maybe it fetches data from an API and stores the processed results in a file? In those cases, the function may not be "academically pure" - but that's one of the things I want to avoid with these videos. Sometimes academic ideas aren't the most practical. We need to perform "side-effects" all the time in the real-world - saving to ANY IO stream would be technically considered a "side-effect" given the rigid academic definition of a "pure" function. At some point, you come to a crossroad where using FP (as an example) has some great principles that you can apply all the time (even to your OOP programming) but can sometimes go to an extreme where it becomes unpractical for most use-cases. Hopefully that helps! Let me know 👍
Далее
Functional Programming in C#
47:26
Просмотров 20 тыс.
Лайфак года 😂
00:12
Просмотров 84 тыс.
What is Span in C# and why you should be using it
15:15
Object Oriented Programming vs Functional Programming
18:55
Refactoring in C# - Improving an Existing Application
1:22:15
What is functional programming | Easy way
9:18
Просмотров 186 тыс.
The Ultimate Guide to C# Records
12:55
Просмотров 19 тыс.