Functional Programming in JavaScript
eBook - ePub

Functional Programming in JavaScript

How to improve your JavaScript programs using functional techniques

Luis Atencio

Buch teilen
  1. 272 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfĂŒgbar
eBook - ePub

Functional Programming in JavaScript

How to improve your JavaScript programs using functional techniques

Luis Atencio

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

Summary Functional Programming in JavaScript teaches JavaScript developers functional techniques that will improve extensibility, modularity, reusability, testability, and performance. Through concrete examples and jargon-free explanations, this book teaches you how to apply functional programming to real-life development tasksPurchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology In complex web applications, the low-level details of your JavaScript code can obscure the workings of the system as a whole. As a coding style, functional programming (FP) promotes loosely coupled relationships among the components of your application, making the big picture easier to design, communicate, and maintain. About the Book Functional Programming in JavaScript teaches you techniques to improve your web applications - their extensibility, modularity, reusability, and testability, as well as their performance. This easy-to-read book uses concrete examples and clear explanations to show you how to use functional programming in real life. If you're new to functional programming, you'll appreciate this guide's many insightful comparisons to imperative or object-oriented programming that help you understand functional design. By the end, you'll think about application design in a fresh new way, and you may even grow to appreciate monads! What's Inside

  • High-value FP techniques for real-world uses
  • Using FP where it makes the most sense
  • Separating the logic of your system from implementation details
  • FP-style error handling, testing, and debugging
  • All code samples use JavaScript ES6 (ES 2015)


About the Reader
Written for developers with a solid grasp of JavaScript fundamentals and web application design.
About the Author
Luis Atencio is a software engineer and architect building enterprise applications in Java, PHP, and JavaScript.
Table of Contents

PART 1 THINK FUNCTIONALLY

  • Becoming functional
  • Higher-order JavaScript

PART 2 GET FUNCTIONAL

  • Few data structures, many operations
  • Toward modular, reusable code
  • Design patterns against complexity

PART 3 ENHANCING YOUR FUNCTIONAL SKILLS

  • Bulletproofing your code
  • Functional optimizations
  • Managing asynchronous events and data

HĂ€ufig gestellte Fragen

Wie kann ich mein Abo kĂŒndigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kĂŒndigen“ – ganz einfach. Nachdem du gekĂŒndigt hast, bleibt deine Mitgliedschaft fĂŒr den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich BĂŒcher herunterladen?
Derzeit stehen all unsere auf MobilgerĂ€te reagierenden ePub-BĂŒcher zum Download ĂŒber die App zur VerfĂŒgung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die ĂŒbrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den AboplÀnen?
Mit beiden AboplÀnen erhÀltst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst fĂŒr LehrbĂŒcher, bei dem du fĂŒr weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhĂ€ltst. Mit ĂŒber 1 Million BĂŒchern zu ĂŒber 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
UnterstĂŒtzt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nÀchsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Functional Programming in JavaScript als Online-PDF/ePub verfĂŒgbar?
Ja, du hast Zugang zu Functional Programming in JavaScript von Luis Atencio im PDF- und/oder ePub-Format sowie zu anderen beliebten BĂŒchern aus Computer Science & Programming in JavaScript. Aus unserem Katalog stehen dir ĂŒber 1 Million BĂŒcher zur VerfĂŒgung.

Information

Verlag
Manning
Jahr
2016
ISBN
9781638353591

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 ...

Inhaltsverzeichnis