appendix-f
This appendix presents an alternative options API for the HIT client in chapter 6. We’ll start with our current approach and then move to the self-referential options pattern.
F.1 Struct options
- Simple—Passing an
Optionsvalue toSendNis straightforward and easy to grasp. - Explicit—All options are under a single
Optionstype and are easy to read and maintain. - Concise—We declare only an
Optionsstruct and some helper functions.
This approach has downsides as well:
- Defaults—We must pass an empty
Optionsif we want the defaults. - Zero-value ambiguity—Unset
Optionsfields do not always mean using the default value. We cannot differentiate between0and unset integer fields, for example.
F.1.1 Zero-value ambiguity
type Options struct {
Concurrency int
MaxRetries int // Defaults to 5 if zero.
}
The zero value of int, 0, makes it hard to distinguish between users who want the default and those who want no retries. Because 0 is ambiguous (it could mean an unset field or no retries), we might add a Boolean field like NoRetries to indicate turning off retries: