How Libraries Take Configuration: Styles Worth Studying

Almost every library eventually needs options: a timeout, a retry count, a custom logger. How you accept them shapes your public API for years, because configuration is where most "small" breaking changes hide. This showcase compares common styles across ecosystems, each with a short illustrative example we wrote for a hypothetical retry helper.

Example 1: the options object (JavaScript and TypeScript)

export interface RetryOptions {
  attempts?: number;      // default 3
  delayMs?: number;       // default 200
  onRetry?: (err: unknown, attempt: number) => void;
}

export function retry<T>(fn: () => Promise<T>, options: RetryOptions = {}): Promise<T>

Strengths: adding a new optional field is backwards compatible; call sites are self-describing. Pitfalls: changing a default is a behaviour change users may not notice, so treat it as at least a minor release with a clear changelog entry, and arguably a major one if it could break callers. Renaming a field is always breaking; accept both names for a deprecation period.

Example 2: keyword-only arguments (Python)

def retry(fn, *, attempts: int = 3, delay: float = 0.2, on_retry=None):
    ...

The bare * forces callers to name every option. Strengths: you can reorder or add options later without breaking anyone who passed them positionally, because nobody can. Pitfalls: adding a required keyword argument is breaking. Give every new option a default.

Example 3: functional options (Go)

type Option func(*config)

func WithAttempts(n int) Option { return func(c *config) { c.attempts = n } }
func WithDelay(d time.Duration) Option { return func(c *config) { c.delay = d } }

func Retry(ctx context.Context, fn func() error, opts ...Option) error

A widely used Go idiom. Strengths: the unexported config struct can grow freely, and each option is a documented exported function. Pitfalls: more boilerplate, and option functions themselves become public API; removing one is a breaking change requiring a new major version, which in Go means a new module path.

Example 4: the builder

Retry.builder().attempts(5).delay(Duration.ofMillis(100)).build()

Common in Java and some Rust crates. Strengths: discoverable through autocompletion; validation can happen in build(). Pitfalls: a lot of surface area for small libraries.

Example 5: environment variables

Some libraries read settings such as a log level from the environment. Strengths: zero code changes for operators. Pitfalls: hidden global behaviour that is hard to test, and variable names become part of your contract. If you support them, document each one and prefix them with your package name.

Versioning consequences at a glance

ChangeOptions object / kwargsFunctional options
Add an optional settingMinorMinor
Change a defaultMinor at least; consider majorMinor at least; consider major
Remove or rename a settingMajorMajor (new module path in Go v2+)
Make an optional setting requiredMajorUsually not expressible; redesign instead

Whatever style you pick, write down the defaults in your documentation and test them; untested defaults drift. For broader API design principles, see the public API design course outline and our page on semver edge cases.

Related pages