Functional programming is a universally-applicable programming paradigm that yields clear code with evident effects, that is easier to debug and maintain.
This functional approach has become popular in the software world, and I wanted to do my best to provide a thorough introduction.
Here’s what you can expect:
- Fundamental functional programming concepts and basic examples of what it might look like. (This post.)
- Related ideas, patterns, and tools used when writing functional code.
- Reasons for/against using functional programming.
- Resources to learn more, and examples of real-world projects that use functional programming.
What is Functional Programming?
Functional Programming is simply a way of writing and structuring programs. It always felt like I was missing something when I was trying to learn it. But at the core, functional programming can be pretty simply described.
The Fundamental Rule of Functional Programming
When a function is given the same arguments, the function will return the same value every time.
Everything else can be built up from this rule.
What does Functional Programming look like?
I wanted to use a short javascript example to show what the difference between some functional code and some non-functional code looks like.
A good rule of thumb when you’re wondering if some code is functional, is to ask yourself, “Does the code rely only on arguments that are explicitly passed to it?” The answer to this should be the same answer to: “Is this code using ‘functional programming’?”
// non-functional
let aNumber = 5;
const addOne = () => {
aNumber = aNumber + 1;
return aNumber;
}
const newNumber = addOne();
// functional
const aNumber = 5;
const addOne = (num) => {
return num + 1;
}
const newNumber = addOne(aNumber);
The above example is simple, but it demonstrates exactly what makes a function functional. In the first example, 10 calls to addOne
will return 10 different numbers, but calling addOne(5)
in the second example will always return six. Unfortunately, it won’t always be this easy to see functional/non-functional code in the real world.
The Power of Functions
Once we have this core idea in place, functions become an even more powerful tool when we’re programming. We’re used to thinking about functions as a way to abstract away something, or to re-use a common piece of code. But now we can start looking at functions as values. That sentence might sound strange, but I’ll use the example below to illustrate what I’m talking about.
const add = (x, y) => x + y
const subtract = (x, y) => x - y
const multiply = (x, y) => x * y
// Without substitution
const result = add(subtract(4, 2), multiply(5, 3));
// With substitution
const result = add(2, 15);
The example above is pretty trivial, but it illustrates how you can look at functions as values. We can substitute our subtract call and our multiply call with their return values, since they’ll always return the same thing. Again, this is trivial here, but this can make it a lot easier to test, develop, and debug. As an aside, this property of functional programming is called referential transparency.
I’m going to cover more in the way of concepts and terms in the next post, but I really wanted to emphasize that what’s above is the core of functional programming. Anything past this is just a closely related concept, or a way to approach writing functional code.
This is the first post in a series on Functional Programming.
Newbie here. One question, what is the benefit of use the “with substitution”, does that make it more funtcional? I would write without substitution, just to ensure i can replace any of those values with others.
The benefit of the substitution is that you can reduce complex function calls to a simple value. The ability to substitute return values is a benefit of writing functional code, but we’re not trying to write ‘code with substitution’
And to clarify, the ‘without substitution’ is the code we wrote, and ‘with substitution’ is a different way to look at that code. ‘Before/After’ substitution would probably be a better way to think about it.
Does the post make more sense now?