Builder Over Options Bag
When a constructor or factory function takes more than 3–4 parameters, replace the options-bag with a fluent builder.…
$ aoe install @team/pattern-builder-over-options-bag Projection
Always in _index.xml · the agent never has to ask for this.
BuilderOverOptionsBag [pattern] v1.0.0
When a constructor or factory function takes more than 3–4 parameters, replace the options-bag with a fluent builder. A builder surfaces required vs optional configuration, is self-documenting at the call site, and is easy to extend without breaking callers.
Loaded when retrieval picks the atom as adjacent / supporting.
BuilderOverOptionsBag [pattern] v1.0.0
When a constructor or factory function takes more than 3–4 parameters, replace the options-bag with a fluent builder. A builder surfaces required vs optional configuration, is self-documenting at the call site, and is easy to extend without breaking callers.
Label
Builder pattern over large options-bag constructor
Problem
// An options bag with 6 properties — which are required? what are the defaults?
new HttpClient({
baseURL: 'https://api.example.com',
timeout: 5000,
retries: 3,
headers: { Authorization: `Bearer ${token}` },
validateStatus: (s) => s < 500,
debug: false,
});
Solution
// Builder: steps are named, required fields enforced by type state
const client = HttpClient
.create('https://api.example.com') // required: baseURL
.withTimeout(5000)
.withRetries(3)
.withHeader('Authorization', `Bearer ${token}`)
.build(); // returns immutable HttpClient
Loaded when retrieval picks the atom as a focal / direct hit.
BuilderOverOptionsBag [pattern] v1.0.0
When a constructor or factory function takes more than 3–4 parameters, replace the options-bag with a fluent builder. A builder surfaces required vs optional configuration, is self-documenting at the call site, and is easy to extend without breaking callers.
Label
Builder pattern over large options-bag constructor
Problem
// An options bag with 6 properties — which are required? what are the defaults?
new HttpClient({
baseURL: 'https://api.example.com',
timeout: 5000,
retries: 3,
headers: { Authorization: `Bearer ${token}` },
validateStatus: (s) => s < 500,
debug: false,
});
Solution
// Builder: steps are named, required fields enforced by type state
const client = HttpClient
.create('https://api.example.com') // required: baseURL
.withTimeout(5000)
.withRetries(3)
.withHeader('Authorization', `Bearer ${token}`)
.build(); // returns immutable HttpClient
Relations
related: [@team/pattern-named-arguments, @team/rule-no-magic-numbers, @team/principle-readability-over-cleverness] see-also: [@team/pattern-named-arguments]
Label
Builder pattern over large options-bag constructor
Problem
// An options bag with 6 properties — which are required? what are the defaults?
new HttpClient({
baseURL: 'https://api.example.com',
timeout: 5000,
retries: 3,
headers: { Authorization: `Bearer ${token}` },
validateStatus: (s) => s < 500,
debug: false,
});
Solution
// Builder: steps are named, required fields enforced by type state
const client = HttpClient
.create('https://api.example.com') // required: baseURL
.withTimeout(5000)
.withRetries(3)
.withHeader('Authorization', `Bearer ${token}`)
.build(); // returns immutable HttpClient
See Also
- @team/pattern-named-arguments
Source
aoe-engine/examples/coding-style/primes/compiled/@team/pattern-builder-over-options-bag/atom.yaml