patz
patz

Reputation: 1316

Pros and Cons of chaining functions using lodash _.chain()

I personally avoid chaining unless its a very small set of functions.

My Team things otherwise.

Lodash _.chain()

The problem that I have with using _.chain() is that if there are a lot of functions chained to it. And in the future if there are any bugs from one of these functions it takes twice the time to debug, then having small bits of functions to achieve the same.

I want to hear what the JavaScript community has to say about this.

Thanks.

Upvotes: 3

Views: 1318

Answers (1)

Alex Robertson
Alex Robertson

Reputation: 1471

When working with ES5.1 code, I used to be a big advocate of the _.chain method. It allowed for semantic flow of data using flexible methods that I could swap in and out easily. It helps to encourage immutable flow of data with collections by forcing you to write mostly pure functions.

The biggest con in my experience is the need to explicitly define what methods are allowed in the chain or implicitly rely on Lodash allowing all methods in the chain. This can cause your build to be bloated and difficult to pin down where a function in the chain comes from.

Additionally, as ECMAScript matures, I'm finding it less and less necessary to rely on Lodash for collection methods. You can accomplish a lot of what you're already using Lodash for with filter, map, some, all, find, findIndex, etc.

Another avenue worth exploring is the _.flow method which takes an array of callbacks and chains them together. It has a lot of the benefits of Lodash’s chaining, without the confusion of how to define the chain methods and their origin.

Upvotes: 1

Related Questions