Builder Pattern: When to fail?
Posted
by
skiwi
on Programmers
See other posts from Programmers
or by skiwi
Published on 2014-05-28T11:43:35Z
Indexed on
2014/05/29
21:57 UTC
Read the original article
Hit count: 402
java
|design-patterns
When implementing the Builder Pattern, I often find myself confused with when to let building fail and I even manage to take different stands on the matter every few days.
First some explanation:
- With failing early I mean that building an object should fail as soon as an invalid parameter is passed in. So inside the
SomeObjectBuilder
. - With failing late I mean that building an object only can fail on the
build()
call that implicitely calls a constructor of the object to be built.
Then some arguments:
- In favor of failing late: A builder class should be no more than a class that simply holds values. Moreover, it leads to less code duplication.
- In favor of failing early: A general approach in software programming is that you want to detect issues as early as possible and therefore the most logical place to check would be in the builder class' constructor, 'setters' and ultimately in the build method.
What is the general concensus about this?
© Programmers or respective owner