Becoming A Functional Programmer
Date: Sat, 28-Oct-2017By: Chukwurah Chuka
First of all, functional programming isn’t a tool or a framework, its just a way of writing codes. Just like object oriented programming.
In functional programming a major emphasis is placed on writing code using functions as “building blocks.”
Your program is defined in terms of one main function. This main function is defined in terms of other functions, which are in turn defined in terms of still more functions — until at the bottom level the functions are just language primitives like “number” or “string.”
Ok lets slow down and start from the basics:
First of all… what are functions?
Functions are “self contained” pieces of code that accomplish a specific task. It defines a relationship between a set of possible inputs and a set of possible outputs — they usually take in data, process it, and return a result. Once a function is written, it can be used over and over and over again.
When we’re learning about a programming paradigm — like functional programming — it’s often helpful to consider how the paradigm approaches behavior and data respectively.
Behavior, for example, is handled purely using functions in functional programming.
Data is, well, data. In functional programming, data is immutable — meaning it can’t be changed. Rather than changing data they take in, functions in functional programming take in data as input and produce new values as output. Always.
By treating functions as nothing more special than a piece of data and by only using data that is immutable, we are given a lot more freedom in terms of how we can use functions.
Namely, it allows us to create small, independent functions that can be reused and combined together to build up increasingly complex logic. We can break any complex problem down into smaller sub-problems, solve them using functions, and finally combine them together to solve the bigger problem.
Considering the ever-growing complexity of software applications, this kind of “building-block” approach makes a huge difference in keeping programs simple, modular, and understandable. This is also why developers strive to make their functions as general-purpose as possible, so that they can be combined to solve large, complex problems and reused to speed up development time for subsequent programs.
Ultimately, the reason that functions are so powerful in functional programming is because the functions follow certain core tenets. Those tenets will be the subject of my email course:
- Functions are pure
- Functions use immutable data
- Functions guarantee referential transparency
- Functions are first-class entities
After that, I’ll briefly touch on how functional programming applies these tenets to encourage us to think carefully about our data and the functions that interact with it.
By the end, you’ll be able to understand how this approach leads to code that is:
- Easier to understand (that is, “expressive”)
- Easier to reuse
- Easier to test
- Easier to maintain
- Easier to refactor
- Easier to optimize
- Easier to reason about
Sound exciting? Come along for the ride!
Post A Comment