Part 1. Think functionally
Itâs highly probable that most of your experience building professional applications has been with an object-oriented language. You may have heard or read about functional programming in other books, blogs, forums, and magazine articles, but youâve probably never written any functional code. Donât worry; this is to be expected. Iâve done most of my development in an object-oriented environment as well. Writing functional code isnât difficult, but learning to think functionally and letting go of old habits is. The primary goal of part 1 of this book is to lay the foundation for and prepare your mind to embrace the functional techniques discussed in parts 2 and 3.
Chapter 1 discusses what functional programming is and the mindset you need to embrace it; it also introduces some of the most important techniques based on pure functions, immutability, side effects, and referential transparency. These form the backbone of all functional code and will help you transition into functional more easily. Also, these will be the guiding principles that set the stage for many of the design decisions we make in the following chapters.
Chapter 2 provides a first view of JavaScript as a functional language. Because itâs so ubiquitous and mainstream, itâs an ideal language with which to teach functional programming. If you arenât a strong JavaScript developer, this chapter will bring you up to speed with everything you need to know to understand functional JavaScript, such as higher-order functions, closures, and scoping rules.
Chapter 1. Becoming functional
This chapter covers
- Thinking in functional terms
- Learning the what and why of functional programming
- Understanding the principles of immutability and pure functions
- Functional programming techniques and their impact on overall design
OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts.
Michael Feathers (Twitter)
If youâre reading this book, chances are youâre a JavaScript developer with a working knowledge of object-oriented or structured design, and youâre curious about functional programming. Perhaps youâve tried to learn it before and havenât been able to apply it successfully at work or on your personal projects. In either case, your main goal is to advance your development skills and improve the quality of your code. This book can help you accomplish that.
The rapid pace of web platforms, the evolution of browsers, andâmost importantâthe demands of end users have all had a profound effect on the way we design web applications today. Users demand that web applications feel more like a native desktop or a mobile app with rich and responsive widgets. Naturally, these demands force JavaScript developers to think more broadly about the solution space and to adopt adequate programming paradigms and best practices that provide the best possible solutions.
As developers, we gravitate toward frameworks that help us create extensible and clean application architectures. Yet the complexity of our codebase still gets out of control, and weâre challenged to reexamine the basic design principles of our code. Also, the web of today is radically different than it was years ago for JavaScript developers, because we can do many things now that werenât technically feasible before. We can choose to write large server-side applications with Node.js or push the bulk of the business logic onto the client, leaving a thin server behind. In either case, we need to interact with storage technology, spawn asynchronous processes, handle events, and much more.
Object-oriented design helps solve part of the problem; but because JavaScript is such a dynamic language with lots of shared state, it isnât long before we accumulate enough complexity to make our code unwieldy and hard to maintain. Object-oriented design certainly moves the needle in the right direction, but we need more. Perhaps youâve heard the term reactive programming in recent years. This programming paradigm facilitates working with data flows and propagation of change. In JavaScript, this is extremely important when dealing with asynchronous or event-based code. Overall, what we need is a programming paradigm that encourages us to think carefully about our data and the functions that interact with it. When thinking about an applicationâs design, ask yourself the following questions in terms of these design principles:
- Extensibilityâ Do I constantly refactor my code to support additional functionality?
- Easy to modularizeâ If I change one file, is another file affected?
- Reusabilityâ Is there a lot of duplication?
- Testabilityâ Do I struggle to unit test my functions?
- Easy to reason aboutâ Is my code unstructured and hard to follow?
If you answer âYesâ or âI donât knowâ to any of these questions, then youâve picked up the right book as a guide on the path to productivity. Functional programming (FP) is the programming paradigm you need. Although itâs based on simple concepts, FP requires a shift in the way you think about problems. FP isnât a new tool or an API, but a different approach to problem solving that will become intuitive once you understand the basic principles.
In this chapter, I define what functional programming is and tell you how and why itâs useful and important. I introduce the core principles of immutability and pure functions and talk about FP techniques and how those techniques affect your approach to designing programs. These techniques allow you to easily pick up reactive programming and use it to solve complex JavaScript tasks. But before we can get into all this, you need to learn why thinking functionally is important and how it can help you tackle the complexities of JavaScript programs.
1.1. Can functional programming help?
Learning functional programming has never been as important as it is today. The development community and major software companies are starting to realize the benefits of using FP techniques to power their business applications. Nowadays, most major programming languages (Scala, Java 8, F#, Python, JavaScript, and many more) provide either native or API-based functional support. Hence, FP skills are in high demand now and will continue to be in the years to come.
In the context of JavaScript, an FP mindset can be used to shape the incredibly expressive nature of the language and help you write code that is clean, modular, testable, and succinct so that you can be more productive at work. For many years, weâve neglected the fact that JavaScript can be written more effectively in a functional style. This neglect is partly due to an overall misunderstanding of the JavaScript language, and also due to JavaScriptâs lack of native constructs to properly manage state; itâs a dynamic platform that places the burden of managing this state on us (the ones responsible for introducing bugs into our applications). This may work well for small scripts, but it becomes harder to control as your code base grows. In a way, I think FP protects you from JavaScript itself. I discuss this further in chapter 2.
Writing functional JavaScript code addresses most of these concerns. Using a set of proven techniques and practices based on pure functions, you can write code that is easy to reason about in the face of increasing complexity. Writing JavaScript functionally is a two-for-one deal, because you not only improve the quality of your entire application, but also gain more proficiency in and a better understanding of the JavaScript language.
Because functional programming isnât a framework or a tool, but a way of writing code, thinking functionally is radically different from thinking in object-oriented terms. But how do you become functional? How do you begin to think functionally? Functional programming is intuitive once youâve grasped its essence. Unlearning old habits is the hardest part and can be a huge paradigm shift for most people who come from an object-oriented background. Before you can learn to think functionally, first you must learn what FP is.
1.2. What is functional programming?
In simple terms, functional programming is a software development style that places a major emphasis on the use of functions. You might say, âWell, I already use functions on a day-to-day basis at work; whatâs the difference?â As I mentioned earlier, FP requires you to think a bit differently about how to approach the tasks youâre facing. Itâs not a matter of just applying functions to come up with a result; the goal, rather, is to abstract control flows and operations on data with functions in order to avoid side effects and reduce mutation of state in your application. I know this sounds like a mouthful, but ...