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
Options
value toSendN
is straightforward and easy to grasp. - Explicit—All options are under a single
Options
type and are easy to read and maintain. - Concise—We declare only an
Options
struct and some helper functions.
This approach has downsides as well:
- Defaults—We must pass an empty
Options
if we want the defaults. - Zero-value ambiguity—Unset
Options
fields do not always mean using the default value. We cannot differentiate between0
and 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: