๐ Behavior (what the program does) ๐ Data (data, is well, data) ๐ 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, in functional programming, is handled purely using functions in functional programming. Functions are โself containedโ pieces of code that accomplish a specific task. They define 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.
๐ซ Data, in functional programming, 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.
๐ซ Functions and immutable data are the only two things you need to ever deal with in functional programming. To make it even simpler, functions are treated no differently than data.
๐ซ Put another way, functions in functional programming can be passed around as easily as data. You can refer to them from constants and variables, pass them as parameters to other functions, and return them as results from other functions.
๐ซ This is the most important thing to understand when approaching functional programming.
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.
๐ 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