Fluent builder: The hidden art of programming

Image for post
Image for post

Writing code is like creating art. With years of experience, one starts to recognize the quality of code, and what best practices and patterns works best. One of the popular patterns out there for helping developers to easily read code, is known as the fluent builder pattern, aka. builder pattern.

It’s mostly useful when you need to create an object with lots of possible configuration options. You end up having an object that can dynamically be changed throughout its life-cycle.

In essence, the goal of the fluent builder is reduce number of arguments a constructors takes, and provide a flexible way of adding behavior to an object, check out the example below showing before/after fluent builder is applied:

Example of fluent builder

This pattern is not new, you’ve most likely encounter it in programming paradigms such as functional programming, object-relational mapping (ORM), and reactive programming where methods are chained like words in a sentence. This way, the developer can easily understand the content of the code by reading from left to right.

  1. Why fluent builder?
  2. Launch rocket to space (example)
  3. The fluent builder way
  4. Summary

1. Why fluent builder?

The best way to explain something, is to show an example. For that reason, we’ll start of by showing a “bad” code example, then describe how it can be improved when using the fluent builder.

2. Launch rocket to space (example)

Let’s imagine we work at SpaceX, one of the projects we are assigned to work with is creating the SW responsible for launching a rocket to space. The goal is to create many options so that the engineers have full access of the rockets behavior, such as when to launch, set the speed, set the location and so forth.

Let’s create a Rocket class with these properties.

Falcon Heavy code to launch the rocket

Let’s build the rocket by creating a new object named falconHeavy. Take a moment to notice the number of arguments required in order to create the rocket object.

Create a new instance of class Rocket

Too many arguments makes it difficult to remember the required order of the arguments. This is an antipattern known as telescoping issue.

With 6 arguments it fails to satisfy the single responsibility principle (SRP). However, there are few use cases where this is fully normal, but ending up with many arguments should be enough to evaluate other alternatives.

What if we only want to set few arguments such as the rockets location and time to launch?

Let’s now apply the fluent builder, and see how we can solve these challenges as explained above.

3. The fluent builder way

So how can we implement the fluent builder? Let’s compare the Rocket class with and without the fluent builder.

Falcon Heavy code to launch the rocket
fluent builder typescript

As shown above, we’ve simply moved the properties out of the constructor, and then create a method for each one. This makes it possible to extend the behavior of the class along the way, instead of having to define all arguments in the constructor when we create a new Rocket class.

Doing so, we can chain methods like:

fluent builder — creating a new object

As shown above, we end up with a declarative (easy-to-understand) approach where we can dynamically define the objects representation as we go. It also means we can use this in multiple places, for instance we may only want to set the location and time for the launch.

4. Summary

As with everything, don’t go crazy with the implementation of the fluent builder. Just be aware of when you end up with a constructor with many arguments, the fluent builder may be a good alternative.

Personally, I really like writing code where you have to chain methods in a declarative way. It makes it easy to work with, understand, and also scale. This style of programming is widely recognized, and a friendly approach for extending behavior when needed.


You can find me on Medium where I publish on a weekly basis. Or you can follow me on Twitter.

P.S. If you enjoyed this article and want more like these, please clap ❤ and share with friends that may need it, it’s good karma.

Software Engineer sharing interesting tech topics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store