Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How hard is it to change from OOP thinking to pure functional-oriented programming thinking in .NET? [closed]

I have been working with C# for many years, since beta. I am having a bit of a time of it, moving from OOP to functional-oriented programming (FOP). I started out with the concepts of procedural programming and functional composition in the late 1980s and have been doing OO since around 1995 when UML was in its infancy where I was programming in Delphi with the multi-paradigm approach of procedural and OO. Are there any good books suggestions, to helping the transition over to functional programming from a deep rooted OO programmer?

I tried a Haskell book, but it seemed to require some background in the subject and was very unapproachable.

I have a couple of O'Reilly, Apress books on F#, but it all seems a bit woolly and half way house, of just getting the job done. It also mixed objects up with functions to suit the needs.

F# seems to forgive me in my OOP thinking a lot more than Haskell, but I think to get the true benefits I need to get into the mindset of FOP to get the best out of the code. I have read around functional programming for about two years in between my day-to-day C# stuff. But I feel I am not getting into the thinking.

I am pretty sure that there are many people out there like myself. Any answers would be, I think, a great help to a lot of C# guys wishing to move over to F#. Especially with many big hitters in the market place asking for F#. I am a contractor who needs to get up to speed as soon as possible.

like image 473
WeNeedAnswers Avatar asked Oct 25 '10 00:10

WeNeedAnswers


1 Answers

This is definitely subjective, but I think that many people are asking similar questions. I certainly did when I started learning F# (as someone with C# experience). Here are some assorted ideas:

  • Pick the right learning problems - The best way to learn functional programming is to start working on some non-trivial projects. If you pick a project that is easy to solve in the OO way, then you'll probably lean towards OO solution. However, if you choose some project that naturally fits FP solutions, then you'll learn something. In my case, I worked on translator from F# quotations to JavaScript, which involves a lot of recursive processing and discriminated unions.

  • Don't try to be perfect - If you try to write something in the functional way, it may not be perfect at the first attempt. Don't worry about that (and don't say to yourself that you could do better in OO way). You can do better in the FP way as well - it just takes some time to find out how to do that.

  • Try to be purist - If you start learning Haskell then you'll have to write pure functional code. F# doesn't enforce that, but I don't think that makes F# less suitable for learning functional programming. If you know C# and .NET, you can reuse a lot of your experience. Just try to be more strict to yourself and avoid using non-functional constructs like mutable state and inheritance - they are sometimes useful, but you can use them after you learn to think in the FP way.

And a little shameless plug - answering a question like this was one of the key motivations for my Real World Functional Programming book, so maybe that could help...

like image 105
Tomas Petricek Avatar answered Sep 18 '22 10:09

Tomas Petricek