Functional Programming Part 1 - Concept
Learn how to use FP to refactor code.
September 3, 2025These are my reading notes from the book Simplicity: Taming Complex Software with Functional programming.
Programming paradigms
There are two major programming paradigms:
- Object-Oriented Programming (OOP)
- Functional Programming (FP).
Characteristics
- Object-Oriented Programming focuses on modeling software as a collection of objects.
- Functional Programming treats computation as the evaluation of mathematical functions."
"Classic" definition of FP
- It emphasizes the use of mathematical functions and aims to avoid side effects.
- It relies on pure functions, which produce no side effects
Now, let's talk about a bit of these two:
Side effects
Side effects refer to any behavior a function performs beyond simply returning a value.
Pure functions
A pure function always produces the same output when given the same input, and we called this "Mathematical functions".
In reality
In the real world, side effects are essential - they're the main reason we use software. After all, we want our programs to interact with the outside world, whether by displaying output, saving files, or sending network requests. Therefore, while functional programming emphasizes minimizing side effects, it doesn't eliminate them entirely - it just manages them more carefully.
Actions, Calculations and Data
When learning Functional Programming, we need to learn how to distinguish between three core concepts:
- Actions: represent operations that involve side effects, it's influenced by time or number of execution or both, such as input/output operations, network requests, or database interactions. These are the parts of our program that interact with the outside world.
- Calculations: pure functions that transform data without side effects. They consistently produce the same output for the same input, making them predictable and easy to test.
- Data: consists of immutable data structures that contain information but no behaviors. Unlike objects in Object-Oriented Programming, data structures in FP are passive containers of information, without attached methods or operations.
Let's have some examples:
Example 1:
{
firstName: "Eric",
age: 18,
};
sendEmailTo(to, from, subject, body);
sum(numbers);
saveUserDB(user);
string_length(str)
getCurrentTime()
[3, "hello", 5, 8]
// Actions
sendEmailTo(to, from, subject, body);
saveUserDB(user);
getCurrentTime();
// Calculations
sum(numbers);
string_length(str);
// Data
{
firstName: "Eric",
age: 18,
};
[3, "hello", 5, 8]
Example 2:
// Can you identify which one is action, calculation and data?
type WeatherStation = {
id: string
location: {
latitude: number
longitude: number
}
lastReading: number
status: 'active' | 'offline'
}
calculateAverageTemperature(num);
saveUserProfile(id:string, name:string, email:string)
filterActiveStations(stations:WeatherStation[])
fetchLatestReadings()
convertToFahrenheit(celsiusTemp:number)
logUserActivity(id:string, action:string)
// Actions
saveUserProfile(id:string, name:string, email:string)
logUserActivity(id:string, action:string)
// Calculations
calculateAverageTemperature(num);
filterActiveStations(stations:WeatherStation[])
fetchLatestReadings()
convertToFahrenheit(celsiusTemp:number)
// Data
WeatherStation
Summary
- Two programming paradigms: OOP and FP.
- Classic definitions of FP are:
- Use mathematical functions and avoid side effects.
- Relies on pure functions.
- Side effects does:
- Modifies state outside of local scope
- Any behavior a function performs beyond simply returning a value
- Pure functions:
- produces the same output when given the same input.
- Also called "mathematical functions".
- Three core concepts for learning FP:
- Actions
- Calculations
- Data