Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Mastering Immutable.js
Mastering Immutable.js

Mastering Immutable.js: Better JavaScript development using immutable data

Arrow left icon
Profile Icon Adam Boduch
Arrow right icon
£7.99 £26.99
eBook Sep 2017 216 pages 1st Edition
eBook
£7.99 £26.99
Paperback
£32.99
Subscription
Free Trial
Renews at £16.99p/m
Arrow left icon
Profile Icon Adam Boduch
Arrow right icon
£7.99 £26.99
eBook Sep 2017 216 pages 1st Edition
eBook
£7.99 £26.99
Paperback
£32.99
Subscription
Free Trial
Renews at £16.99p/m
eBook
£7.99 £26.99
Paperback
£32.99
Subscription
Free Trial
Renews at £16.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mastering Immutable.js

Why Immutable.js?

Immutable.js is yet another JavaScript library. If this makes you cringe, don't worry—I feel your pain. The last thing that JavaScript developers need is another library to learn. Then, I used the library and realized that Immutable.js is one of those gems that you don't have to feel bad about including as a dependency.

This first chapter will show you what Immutable.js is all about. Hopefully, I can dispel any mysteries about Immutable.js while making you want to try it out for yourself.

Here's what you'll learn in this chapter before getting your hands dirty in the chapters that follow:

  • Why changes to data are destructive
  • What are persistent changes?
  • The hybrid functional/object approach used by Immutable.js
  • Why data flows in one direction in Immutable.js
  • Alternative libraries and approaches to immutability

Mutations are destructive

When you mutate data, you change it. If data is immutable, you can't change it.

The purpose of Immutable.js is to bring unchangeable data to JavaScript.

Immutability is native to some programming languages. But why is changing data bad, and how does one write software with data that never changes?

Deleting old data

When you first learned how to program, one of the earliest concepts that you were taught was the variable. A variable is something that's used to store a value, so of course it's important! When you create a variable and assign its initial value, it feels like you're creating new data. This is because you are creating new data.

Then, later on in your code, you assign a new value to your variable. It's a variable, so naturally it varies. This might also seem like you're creating new data, which you are; if you assign the value 5 to your variable, then that's a new value that didn't exist before. Here's the catch: you're also destroying data. What if your variable had a value of 2 before you assigned it 5?

This might not seem like a big deal. Isn't a variable supposed to store whatever value you assign to it? Yes, variables work perfectly fine that way. They do exactly what we ask them to do. So, the problem isn't strictly that variables can change their values. Rather, it's a human limitation—we can't reason our way through the huge number of variable changes that happen in our code. This leads to some scary bugs.

A scary class of bugs

Imagine that you're trying to bake a loaf of bread. Nothing fancy, just a plain loaf. You don't even need to look at the recipe—it's just a handful of ingredients, and you've memorized the steps. Now suppose that one or two of your ingredients have been changed without your knowledge. You still follow the steps, and the end result still looks like bread, but then your family eats it. One family member says something tastes off, another doesn't seem to notice anything, while yet another heads straight for the washroom.

These are just some of many possible scenarios that could have played out, and the problems all stemmed from the ingredients used. In other words, because the ingredients were allowed to change, they did change. Consider the game Telephone. In this game, there is a line of people and a message is whispered to the first person in the line. The same message is whispered (allegedly) to the next person, and so on, until the message reaches the last person who then says the message out loud. The ending message is almost never the same as the starting message. Once again, the message changes because it's allowed to change.

When writing software, you're no different from the person making bread using incorrect ingredients or the Telephone game player relaying an incorrect message. The end result is a scary type of bug. If you mess up your bread, can you pinpoint exactly what went wrong? Can you identify the Telephone game players who broke the message? Your variables change because they can. You've written code to make the variables change, and sometimes everything works exactly as you want it to. But when something goes wrong, it's very difficult to figure out what went wrong.

Persisting changes

If data isn't supposed to change, just how are we supposed to get anything done? How do we move the state of an application along from one state to the next if our data is immutable? The answer is that every operation that you perform on immutable data creates new immutable data. These are called persistent changes, because the original data is persisted. The new data that's created as a result of running the operation contains the changes. When we call an operation on this new data, it returns new data, and so on.

What are we supposed to do with the old data when we make a persistent change that results in new data? The answer is – it depends. Sometimes, you'll just replace the old data with the new data. Yes, the variable is changed, but it's replaced with an entirely new reference. This means that something that is still referencing the old data is never affected by your persistent changes.

The Immutable.js approach

Different JavaScript libraries each have their own approach to deal with certain things. For example, React relies on JSX—an XML dialect embedded in JavaScript for declaring user interface components. Lodash exposes lots of little functions, some of which can be chained together to simplify complex code. The idea behind Immutable.js is to expose a small number of collection classes as its API. How does it work?

A collections API

A collection in JavaScript refers to anything that can be iterated over, for example, when using a for..of loop. In practice, this means arrays and objects. Arrays and objects are collections of values; the only difference between them is how you look up values. An array is an indexed collection, because you use a numerical index to lookup a value. An object is a keyed collection, because you use a key string to lookup a value.

The issue with these primitive JavaScript collection types is that they can change; that is, they're not immutable. The collections exposed by Immutable.js feel a lot like native arrays and objects. In fact, you can take an array or an object and use it to construct an Immutable.js collection. You can also turn an Immutable.js collection into a JavaScript array or object.

This is the extent of the Immutable.js API. You use collection classes to hold your immutable data. To use this immutable data, you call methods on these collections, which is at the very heart of the Immutable.js API.

Collection methods return new data

Let's say that you have an Immutable.js collection: coll1. Then, you call a method to insert a new value into the collection: push('newValue'). This is what a persistent change looks like: coll1 persists and push() returns a new collection that includes the new value.

Not all collection methods are mutative. For example, if you want to filter a collection so that only items that pass a given criteria are returned, you would call the filter() method. This would result in a new collection, just like adding a new item would result in a new collection. The difference is that filter() isn't a persistent change—it's part of a sequence transformation.

Think of a sequence transformation as an Instagram filter. You have the original collection: the picture. Then, you have a filter() transformation, which takes the original picture data and rearranges some of the pixel data. You want to be able to see the results of the filter, but you don't want to change the original. The filter is just a view of the original.

Chaining method calls

Immutable data doesn't just sit inside of a constant. Somehow, your applications need to make use of this data. These are called side-effects. A side-effect is something that is effected by immutable data, such as rendering data on the screen in a user interface or writing data to a file. By contrast, operations performed on immutable collections are either persistent changes or sequence transformations, both of which result in new collections. These operations don't have any side-effects; they just result in new data being created. Another term used for functions that don't cause side-effects is pure functions.

With Immutable.js, you start with a collection of immutable data and work your way toward some sort of side-effect. In functional programming, it's generally best to avoid side-effects due to the problems they cause. But side-effects are unavoidable. Without them, your software cannot interact with its environment. By chaining together Immutable.js collection methods, you can write clean and concise code that transforms immutable data into something that you need—something that a side-effect can use.

Unidirectional data flow

React and Flux have popularized the concept of unidirectional data flow as the fundamental concept that drives web application architecture. A unidirectional data flow is exactly what it sounds like—data that flows in one direction. It's a simple idea, but it's an important mindset to adopt when thinking about immutable data.

What other direction is there?

The best way to visualize unidirectional data flow is in a top-down fashion. Data starts in one state at the top, changes state as it flows downward, ending with a side-effect that does something with the data. When this is enforced as a property of the architecture, side-effects are predictable. We can easily trace the starting point of data, through the transformations it makes, ending with the visible side-effect.

When we don't enforce a unidirectional data flow, it's difficult to trace cause and effect. This is the main reason that Facebook started promoting the concept with the creation of Flux—to prevent components from changing state at will and passing the changed state on to another component. For example, let's say that you aren't using immutable data, and that one component changes its state in response to an event. Then some other component that references this state renders itself, causing its state to change as a result of the first change. These are nothing more than uncontrolled side-effects.

Immutable.js is a low-level library compared to the ideas of Flux or a UI component library such as React. Even if you're not using either of these, you can still leverage Immutable.js to build a unidirectional architecture.

Subscriptions are out

One approach to handling data that changes is to observe it. This means using some mechanism to attach a listener callback function that's called whenever the data changes. For example, you could have data that models a user interface component, and when that data changes, you would render the component so that it reflects the changed data.

To set up subscriptions like this will require data that can change, which we don't want. Since we're working with immutable data that never changes, subscriptions are a dead end. This means that you have to rethink your approach for notifying components about the state of your data. The rule of thumb with immutable architectures is that only new data is passed around when things change.

Data is only created

Let's revisit the visualization of data flowing from top to bottom, ending with a side-effect. Along the way, we're either changing the state of data with persistent changes, or we're shaping the data that we need using sequence transformations. From the beginning to the end of this flow, we're only creating new data.

The chained Immutable.js collection method calls result in new data—every time. This means that if we make a mistake and accidentally try to use data in a way that falls outside of the unidirectional flow that we're following, Immutable.js will protect us from ourselves. When this happens, the result is often a broken application that doesn't work. This is better than a half-working application that has mutability bugs hidden deep inside of it.

For example, suppose that we call set() on an immutable map to set a value, expecting that simply calling this method would be enough to change the state of the map. But since the set() method is a persistent change, it doesn't change the map—it creates and returns a new map. So while we weren't expecting this behavior, it's better than accidentally changing the state of something.

Implicit side-effects are hard to do

Side-effects in code that uses mutable data are implicit. Immutable.js, on the other hand, promotes explicit side-effects by placing them at the end of a method call chain. This makes the side-effects in your code easy to spot, and easy to reason our way through the sequence of transformations and persistent changes that lead up to the side-effect occurring.

Implicit side-effects are problematic because we don't have any meaningful way to track them. For example, you change some data that results in four function calls being made. Do any of them have side-effects? Two of them? All of them? Do the side-effects cascade into other side-effects? We're creating too much work for our brains to handle here.

The trick with Immutable.js is to make explicit the things that matter when you're reading code. This means quickly figuring out what caused a given side-effect to occur. On the other hand, you can't make everything explicit otherwise you'd have a mountain of code to sift through. The implicitness of Immutable.js comes with piecing together data by gluing it together using chaining—there's a lot going on behind the scenes that you don't need to think about.

Other libraries similar to Immutable.js

Immutable.js isn't the only library out there that does what it does. While it is unique in its approach to handling immutable data, it's never a bad idea to compare one library with another. What, exactly, are we comparing it with, though?

What are we comparing?

There's no point in comparing a library such as Immutable.js with something that has no notion of functional programming or immutability. For instance, Angular is a framework for building applications, and observing changes in state is a core pattern. This is something that Immutable.js doesn't do. Comparing Immutable.js with something such as React doesn't make much sense either. Despite the fact that React honors concepts such as avoiding side-effects, we wouldn't be comparing apples with apples, as they do different things at different levels of abstraction.

Some criteria that you would use to compare Immutable.js with other libraries include the following:

  • Is it a low-level library?
  • Does it support the notion of immutability?
  • Does it have the concept of collections?
  • How large is the API compared with that of Immutable.js?
  • How efficiently can it handle immutable data?

Lodash is a good bet

Lodash is a popular utility library that does a lot of the same things as Immutable.js. It supports the notion of immutability and avoids side-effects. It also supports the concept of collections. Its approach to efficiency is different from what Immutable.js does, but it's there. It might have a larger API than Immutable.js, but it's not that much larger.

The two libraries differ greatly in their overall approach and design, but they're comparable in the aspects that matter. Learning Lodash before switching to Immutable.js isn't a total loss and vice versa. You won't know which libraries work best for you until you write code that uses them.

We'll start writing Immutable.js code in a moment.

Summary

This chapter introduced you to the conceptual foundation of Immutable.js. Immutable data is how we prevent unwanted side-effects. With Immutable.js collections, everything results in new data. This includes changing the collection somehow—these are called persistent changes. It also includes shaping the data in order to do something with it—these are called sequence transformations.

The typical Immutable.js pattern involves chaining collection method calls together—the persistent changes and sequence transformations—ending with a side-effect.

In the next chapter, we'll write some code that creates Immutable.js collections.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Master the Immutable.js JavaScript framework
  • • Build predictable and dependable applications using immutability
  • • Control how data flows through your application
  • • Control the effects of data flow in your user interface using Node.js

Description

Immutable.js is a JavaScript library that will improve the robustness and dependability of your larger JavaScript projects. All aspects of the Immutable.js framework are covered in this book, and common JavaScript situations are examined in a hands-on way so that you gain practical experience using Immutable.js that you can apply across your own JavaScript projects. The key to building robust JavaScript applications using immutability is to control how data flows through your application, and how the side-effects of these flows are managed. Many problems that are difficult to pinpoint in large codebases stem from data that’s been mutated where it shouldn’t have been. With immutable data, you rule out an entire class of bugs. Mastering Immutable.js takes a practical, hands-on approach throughout, and shows you the ins and outs of the Immutable.js framework so that you can confidently build successful and dependable JavaScript projects.

Who is this book for?

This book is for JavaScript developers, from intermediate level and beyond, who need to create dependable JavaScript projects, using the Immutable.js JavaScript framework.

What you will learn

  • • Learn how Immutable.js can improve the dependability of your JavaScript code
  • • Discover how to create Immutable data, and work with persistent changes
  • • See how to combine and filter collections, and find items
  • • Learn how to work with sequences and side effects
  • • Sort collections, maps, and sets
  • • Get to know tricks to avoid processing chains
  • • Compare and move between lists, sets, and maps
  • • Work with Immutable patterns and Immutable architecture

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 28, 2017
Length: 216 pages
Edition : 1st
Language : English
ISBN-13 : 9781788397247
Vendor :
Facebook
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 28, 2017
Length: 216 pages
Edition : 1st
Language : English
ISBN-13 : 9781788397247
Vendor :
Facebook
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
£16.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
£169.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts
£234.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total £ 111.97
Mastering Blockchain
£41.99
Mastering JavaScript Functional Programming
£36.99
Mastering Immutable.js
£32.99
Total £ 111.97 Stars icon
Banner background image

Table of Contents

16 Chapters
Why Immutable.js? Chevron down icon Chevron up icon
Creating Immutable Data Chevron down icon Chevron up icon
Persistent Changes Chevron down icon Chevron up icon
Filtering Collections and Finding Items Chevron down icon Chevron up icon
Sequences and Side-Effects Chevron down icon Chevron up icon
Sorting Collections Chevron down icon Chevron up icon
Mapping and Reducing Chevron down icon Chevron up icon
Zipping and Flattening Chevron down icon Chevron up icon
Persistent Change Detection Chevron down icon Chevron up icon
Working with Sets Chevron down icon Chevron up icon
Comparing Collections Chevron down icon Chevron up icon
Combining Collections Chevron down icon Chevron up icon
Declarative Decision Making Chevron down icon Chevron up icon
Side-Effects in User Interfaces Chevron down icon Chevron up icon
Side-Effects in Node.js Chevron down icon Chevron up icon
Immutable Architecture Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.