Hey! My name is Katerina, and I have the warmest feelings for functional programming, I use functional language on an ongoing basis and even teach a little.
Our main development language at Typeable is Haskell, and while everyone was arguing about whether Haskell was ready for production, we just used it and considered it a competitive advantage. We would like to share our opinion based on this experience.
How FP improves programming
Functional programming is quite popular at the moment, and FP elements began to appear in many imperative languages, such as lambda functions, partial application (currying), higher-order functions (map, filter, convolutions). Somewhere these borrowings look good, but somewhere they lead to a rather strange, foreign syntax. But the programming paradigm is an approach that lives in the programmer's head and is not, in general, part of the language. To one degree or another, any language supports different paradigms, and its designs allow programs to be developed in different styles. Whether it is worth developing in a functional style is an open question, and each developer answers it based on their preferences, language capabilities and other considerations.
We believe that using the functional style in imperative languages, or even better, a functional language, especially with statically typed ones, will help make the code much better, namely:
- The code will become more concise and expressive. "Expressiveness" can be defined as the number of ideas per code unit, and in general functional languages, being higher-level, are also more expressive. For example, the transformation of every element in an array or list is done in a functional one-liner (using map / foreach / whatever and an anonymous function), while in an imperative style one would have to organize a loop, declare a variable for a counter or iterator, and use explicit assignment. For more complex examples, the difference in expressiveness only increases.
- . « » . , . , , , - , : , (, ) , . , . ( ), « » . , , , . (working memory), , . , . — . , , C, SOLID GoF-, , , .
- . — , . : , , . , , , ? . «» , , « ».
- The code will become easier to debug and test. This point follows from the previous two: we have a set of small functions, some of which are pure, i.e. we know that their result depends only on the input. The code becomes easier to debug - you just need to check what the functions used are returning separately to understand how they will work together. It is just as easy to write unit tests for the "clean" logic of your application.
How FP improves the programmer
Next, I would like to share my experience that is not directly related to writing programs in functional languages, and tell you about what else knowledge and use of FP was useful for me and may be useful for you:
- , -. - , . , «» « » , , . , : , , Haskell, Brainfuck, Piet. . , .
- , , computer science. , . , , - , , .
- . , « » , , . , . , . , , , . , , - , - , . , , .
- : Haskell! , Haskell, . , Haskell-, . - . «» «» , , . , , «Haskell-». , , . , , . . , , , , . , , , . , : — !
Conclusion
The emergence of FP elements in popular industrial development languages such as Python, C ++, Kotlin, Swift, etc. confirms that this approach is really useful and has strengths. Applying the functional style allows for more robust code that is easier to break apart, generalize, and test, regardless of the programming language. Of course, a functional language allows you to take full advantage of these benefits by providing natural constructs with a high degree of expressiveness.
There are a large number of functional languages, but, as it seems to us, at the moment the most convenient and useful for development are those that use static typing. This allows you to catch many errors during compilation and express part of the application logic at the type level.
In conclusion, I want to wish everyone not to be afraid to use alternative approaches and try them in practice, even if it will be useful solely for your self-development.