Version 2 Only - RequestBuilder replaced by Request.Builder

7 views
Skip to first unread message

Daniel Worthington-Bodart

unread,
Oct 30, 2015, 12:26:28 PM10/30/15
to utterlyidle
This is a version 2 only feature.

RequestBuilder is now deprecated and replaced by Request.Builder.

This is a great comparison of OO Builder vs Functional Builder patterns, so firstly how does the usage code compare:

RequestBuilder (OO Style):

Request request = get("/").header(ACCEPT, "Kittens").query("name", "Mimi").build();


Request.Builder (Functional Style)

Request request = get("/", header(ACCEPT, "Kittens"), query("name", "Mimi"));


Not much difference except you never have to call build() and that you never actually create Request.Builder is just a place to store some functions. So what are the trade offs:


Pros
  • No extensibility lock in, all functions on Request.Builder are just static unary functions that take a Request and modify it in some way and then return it. You can create your own functions with out having to use inheritance, i.e its completely open.
  • The code is way way simple, you can compose your functions and handle nested persistent data structures.
  • It has a lot more features for less code, now you can convert between query, form, header, cookie parameters without me ever actually implementing specific code to do it.
  • It's uniform, cookies behave exactly the same as headers even though they are nested in headers.
  • It's more performant for nested structures as you can edit in place a bit like a zipper or lens.
Cons
  • More static imports
  • You can't do 'dot' navigation, always best to start by importing Request.Builder.* and then optimise afterwards.
  • Not as familiar for most people. 

NB: Obviously this is slightly unfair as RequestBuilder organically grew to the mess it is today!

Feedback always welcome

Dan

Daniel Worthington-Bodart

unread,
Oct 30, 2015, 12:30:15 PM10/30/15
to utterlyidle
Oh one thing I forgot to mention is that the new Request.Builder always defaults to replacing headers/query/form etc rather than adding them as this was a general source of bugs
Reply all
Reply to author
Forward
0 new messages