Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: consider using variadic options instead of structs #908

Closed
vangent opened this issue Dec 6, 2018 · 3 comments
Closed

all: consider using variadic options instead of structs #908

vangent opened this issue Dec 6, 2018 · 3 comments
Milestone

Comments

@vangent
Copy link
Contributor

vangent commented Dec 6, 2018

Currently we are using Options structs everywhere. Should we use self-referential option funcs instead?

https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html

@jba
Copy link
Contributor

jba commented Dec 6, 2018

No; there are many problems with that idea. Here's a list Ross and I compiled a while back:

  • Options can be hard to discover. While functions that return the option type are grouped under the type, other ways of defining options are not apparent from the godoc.

  • Options bloat a package's API surface.

  • Options pollute the namespace (or require verbose names).

  • Options bloat the codebase. Most new options require a new type and two functions. (Assuming the option style we've adopted for the cloud.google.com/go packages, where we define and return an unexported type for the option rather than returning an anonymous function.)

  • Options have ambiguous properties with respect to ordering. Does order matter, what if you specify the same option twice, etc.

I'd only use functional options if it they were rarely needed, and only one function in the package could use them.

@nhooyr
Copy link

nhooyr commented Apr 20, 2019

Thoughts on a pointer vs a value options structure? I'm liking value options structures right now because then its immediately clear what this extra parameter is for versus nil which masks that.

You also don't need to think about whether there is a difference between nil and the zero value.

@jba
Copy link
Contributor

jba commented Apr 22, 2019

In cases where the options are infrequently used, it's too much of a burden to write pkgname.Options{} for every call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants