Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the same benefits of functional programming (F#) by using more static methods in C#?

I admit I haven't grokked F# yet. But in the 30,000 foot descriptions, they keep talking about easy to test code that doesn't have mutable state. Is that the same as static methods?

Could I get the benefit of F# by writing some of my code in classes with all static methods?

I'm just looking for the short answer, I know whole books exist on the topic.

like image 464
MatthewMartin Avatar asked Apr 28 '09 12:04

MatthewMartin


People also ask

Does functional programming make you a better programmer?

It makes you a better programmer Unlike in imperative languages, in functional languages, variables are immutable by default, and do not depend on the state of the program. Along with referencial transparency (given the same inputs, a function always return the same results), this increases consistency.

What are three 3 benefits of functional programming?

Advantages Of Functional ProgrammingIt helps us to solve problems effectively in a simpler way. It improves modularity. It allows us to implement lambda calculus in our program to solve complex problems. Some programming languages support nested functions which improve maintainability of the code.

Can you do functional programming everything?

If you're thinking about the thing in terms of actions, calculations, and data, then yes. You can do functional programming in any language. Just like you can do object-oriented programming in any language. Object-oriented programming is also a mindset.


2 Answers

You could certainly write C# code immutably, but that's nothing to do with static functions. Immutability are things like having a struct or object that only "changes state" by making a copy of itself and having the copy be different.

like image 99
Adam Luter Avatar answered Oct 17 '22 23:10

Adam Luter


Absolutely no, immutability has nothing to do with methods being static or instance. String, being an immutable class, has plenty of instance methods, which, in a very functional manner, return a new instance of String class, rather than modifying anything.

You could try to emulate F# by using functional decomposition, but this will still be pretty imperative code.

like image 40
Anton Gogolev Avatar answered Oct 17 '22 21:10

Anton Gogolev