Functional programming is a paradigm where development is based on creating and composing functions. The foundational principles of this paradigm avoid changing state and mutability. It's focused on "what to solve" rather than "how to solve" because it emphasizes the declarative definition of the problem using functions rather than step-by-step instructions to solve it.
"The lambda calculus, developed in the 1930s by Alonzo Church, is a formal system of computation built from function application. In 1937, Alan Turing proved that the lambda calculus and Turing machines are equivalent models of computation,[36] showing that the lambda calculus is Turing complete. Lambda calculus forms the basis of all functional programming languages." Functional programming - Wikipedia
Lisp, developed in the 1950s, was the first high-level functional programming language.
Functional programming has the following key principles:
Pure function
It's a function that produces the same output given the same input. It has no side effects because it doesn't have an external impact.
Immutability
Once the data is created, it cannot be modified. It preserves the initial state and prevents side effects. New data can be added by applying functions to existing data.
First-class Function
"A first-class function is a function that is treated as a “thing in itself,” capable of standing alone and being treated independently. Functional programming seeks to take advantage of language support in using functions as variables, arguments, and return values to create elegant code." What is functional programming? A practical guide | InfoWorld
Higher-Order Function
It's a function that accepts functions as arguments or returns them as a result. Multiple functions can be used in function compositions like pipelines to chain them together.
Other relevant concepts in functional programming include recursion and referential transparency.
Functional features can be applied in multi-paradigm languages. For example, Java SE 8 introduced lambda expressions to represent methods as expressions. C# includes LINQ, which is a form of declarative, functional programming. JavaScript code can be implemented as functional programming by using first-class functions.
Benefits
Expressiveness: Small, declarative and human-readable functions designed to do one thing and do it well.
Debugging and Testing: With Purity and immutability make it easier to reproduce bugs and write unit tests to validate the expected results.
Concurrency and Parallelism: With no side-effects or shared mutable objects, multi-threading issues such as race conditions are not applicable.
Laziness: Functional programming encourages lazy evaluation, meaning that expressions are only evaluated when they are requested.
Modularity: Granular, Isolated functions are easier to maintain and promote reusability.
Caching: The determinism of pure functions allows results to be stored once for given inputs and reused whenever the same function parameters are passed.
Drawbacks
Statefulness difficulty: Challenging to work with mutable states and interactive tasks such as I/O operations.
Functional misunderstanding: Ineffective application of functional key ideas like recursion can lead to infinite loops and memory overheads.
Performance trade-offs: Although functional programming can be more expressive and elegant, it may not be the most performant choice for certain use cases.
Check out my video:
Comments