How does functional programming affect the coding style?

Currently, most programmers use OOPS concepts for software development.

But some of them are also subject to functional programming.

How does functional programming affect the coding style?

+7
functional-programming
source share
10 answers

The main use passed functions to higher-order functions, such as a filter and a map (Where and Select in.Net). This is really the biggest influence, allowing you to write things like given operations only once, and then actions that act or change sets.

So, things like

var SquareOfEvenIntegers = MyListOfInts .Where(i=>even(i)) .Select(i=>i*i); 

instead

 List<int> SquareOfEvenIntegers = new List<int>() foreach(int i in MyListOfInts) { if (even(i)) { SquareOfEvenIntegers.Add(i*i); } } 
+6
source share

It is much easier for me to write multi-threaded code.

I tend to have fewer class level variables than before.

I also tend to have interfaces that are functionally independent ... again, which is a huge gain when it comes to multithreading, as it can significantly reduce the amount of object locking if your threads are also functionally independent.

I also have nightmares about closing braces that haunt me.

+5
source share

Two things come to my mind based on the functional programming that I accept:

  • Changing void methods that change class variables to methods that return values
  • Make classes immutable, not have classes with JavaBeans setters (antipatterns)
+4
source share

By writing a decent piece of Haskell, it is much easier for me to use Linq to SQL in my C # code.

Now I am very inclined to write code that accepts the result and returns the result, instead of changing the variable as a side effect.

I wrote large pieces of both procedural and functional code, which makes it easier for me to factor my code into smaller pieces that are more than one style or the other. When I knew only procedural programming, I had fewer tools in my mental toolbar.

Reading library sources at Haskell taught me how to better weld my code to separate abstractions from the real task.

I got a lot from writing Haskell!

+3
source share

In a way, I found it amazing how much I unconsciously used functional style programming, not recognizing it for what it was.

For example, in Java, I actively use constructs in Commons collections, such as Transformer and Predicate, which are essentially closures, but implemented as Java interfaces. Then you use the provided utility functions to convert and filter items in the collection. This contradicts the β€œstandard” imperative way of doing such things in Java.

This, of course, is a vanilla functional map and filter operations, but I instinctively used them, not understanding what it was. This suggests that the functional style is largely intuitive, even if you are not told about it.

+2
source share

Event handlers that are closures;

 button.OnClick += (sender, args) => MessageBox.Show("we captured " + someCapturedVariable); 
+1
source share

I find the transition of Python 3.x to iterators very natural for me because it reminds me of the lazy Haskell evaluation.

Whether using Python 2.x or Python 3.x, a list of concepts , map , itertools , operator and (to a lesser extent) functools , are my friends!

+1
source share

Since functional programming is orthogonal to OOP, it really opened my eyes to how I structure my classes.

  • I tend to have more meaningful interfaces that resemble concepts.
  • My code is better untied, and I have more or less meaningful classes. Very rarely, I create a class to implement a template.
  • I create fewer setters, instead I call different member functions that compute the value I want and pass it to one set.
  • Before functional programming, I would never have thought of passing a function as a parameter as a solution when trying to solve a problem. Now this solution just appears in my head and really helps with DRY.
  • My code contains more expressions instead of an operator.
  • If this decision allows, I am inclined to a lazy assessment.
+1
source share

Functional programming and OO are not disjoint concepts. One drawback of the functional languages ​​that I see is the lack of expressiveness of ADT. The closest I've seen for functional OO programming is Eiffel. By convention, the functions are referentially transparent; it would be ideal if it were forced.

0
source share

I wrote a little about it in

How does functional programming affect the structure of your code?

Some of them are F # -specific, but there are many common observations.

0
source share

All Articles