Mar 4, 2022
4 min read
Functional Programming in Dart & Flutter: Foundation — Part 0
Before embarking, I want to take a quick second to introduce myself. I am Yogesh, a software engineer who has been exploring Mobile technologies for quite some time. I started with Android and jumped into the cross-platform world using React-native, then finally switched to Flutter. By no means I am an expert in Functional Programming, but what I hope is to share my learning and provide a roadmap for others looking to travel down the same road.
Let’s Start!
Table of Contents
What is a Paradigm?
Imperative vs Declarative
What is Functional Programming?
Why bother learning Functional Programming?
What are we going to cover?
What is a Paradigm?
A Paradigm is a mindset. It is a way of programming. It is how we think and write code. Although there are several types of paradigms, the ones we are interested in is imperative and declarative.
Imperative
The imperative paradigm is a set of instructions you give to the computer. You give detailed steps of how to execute a program. Let’s see with an example.
Problem Statement: You have a list of numbers, and you want to find out all the even numbers.
In the above snippet, you give instructions in the form of loops and conditions.
First, create a result array.
Iterate over all the elements from the number array.
Check if the current element is even or odd.
If even add it in the results array.
Declarative
The declarative paradigm tells the program what it wants. It focuses on “What” instead of “How”. This helps to make the code more reasonable.
Flutter is a Declarative Framework.
Let’s take the same example and solve it using a declarative style.
Now, you are telling the program what you want. Also, you are no longer mutating any state.
If we compare both the snippets, you’ll observe, for imperative, we have to skim through the loop, execute it mentally regarding what it’s doing, and then we understand the code. Whereas the declarative looks like well-written prose (It can further be enhanced by converting number % 2 == 0
into a separate isEven
function).
You’ll hear this phrase a lot, Imperative focuses on “How” and Declarative focuses on “What”.
What is Functional Programming?
Functional programming (aka. FP ) is a declarative way of writing programs, where a program is composed of functions. This definition will make more sense once we cover some of the terminologies in FP.
In Dart, functions are treated as First-Class Citizens, what this means is Functions are treated like any other values like String, int, bool etc. One can pass functions as arguments, assign them to a variable and return functions as result.
We have assigned the function add
to a variable called _addTwoNumbers
. This means _addTwoNumbers
now has the same signature as the add
method. It takes two arguments, adds them and returns the result.
Dart is not recognized as a functional language, but it supports some concepts and can be used to understand FP.
Many Imperative languages are adopting FP concepts such as lambdas and Higher-Order functions.
Why bother learning FP?
Before jumping into the technical aspects, I want to point out that, Learning FP makes you think in a different way. For me, It was like an exercise for my brain. I feel it helped me to level up my development skills and the way I approach problems.
It makes code easier to reason about. Code becomes more concise and expressive. A reasonable code is easier to maintain.
It helps with modularity and is highly composable.
It Breaks the problem into smaller pieces.
Helps to increase the performance by parallel executions, memoization and other techniques (which we are going to cover).
Easier to write tests and helps with debugging.
What are we going to cover?
Concepts like Pure functions, Side-effects, Higher-Order functions, Currying, Composition, Partial applications, Functors, Referential Transparency, Equational reasoning, Monads, Morphisms and much more. We will also explore packages that help us to implement FP in Dart & Flutter.
“Oh boy! What was all that?” was my reaction when I heard these terms for the first time.
Through this journey, our goal is to understand these terms. While it might help you to sound cool in front of your colleagues, the main motive is to understand the underlying concepts rather than to throw them around to sound cool.
Awesome! Pat yourself on the back for reaching till the end. I hope I added some value to the time you invested. Find out more examples on the GitHub repository, and reach out on Twitter or LinkedIn for suggestions/Questions or any topic you’d like me to cover. You can support it by clapping👏, and thanks for reading :) Follow for more😄
Until next time, folks!