Builders for Testing

Recently Shaun Abram blogged Builder Pattern: Good for code, great for tests, and he references a talk I gave at the Silicon Valley Code Camp back in 2008. What’s interesting to see is that it’s still useful and is in active use today where I work almost 6 years after I introduced it. If I can dig up my slides from back then I’ll post them here.

One advantage of Builders that I don’t remember if I had mentioned in my talk was that using them makes merging much easier. For example (using one of Shaun’s code fragments), if you had code like this

And then you changed the balance amount to be 20 (line 5):

But at the same time someone else submits a change that looks like this:

So now you have to merge, but because of the code layout, it’s easy to see what’s change and do the appropriate merge (probably even let the automatic merge do its job):

diff

 

2 thoughts on “Builders for Testing

  1. Good point Ted.
    Another advantage this made me think of is if you add a new constructor argument, for example to Account.
    Every reference to that Account constructor will break.
    Classes using the builder will not however. Only the builder itself will break so only one place to update. Then all uses of the Builder will automatically use the new default value for that constructor argument, and can of course override if needs be.

  2. Pingback: Shaun Abram » Blog Archive » Builder Pattern: Good for code, great for tests

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">