-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Use structures for object configuration rather then configuration methods #7178
Comments
This is specific to the application at hand. While we could modify We could however, group the non-optional args into an options struct like you're suggesting, that I'm in favor of 👍 /cc @erikgrinaker |
I think it's good to use best practice for examples. |
So, if you will find it useful, I can handle that task. |
Sure. Please keep the variadic option funcs, but you you can turn all other args into a struct. |
But the whole goal of this proposal is to use Struct Config instead of variadic option funcs. |
No, I am not in favor of that. Please read that link I posted above. |
I know the Dave Chaney post. I read it when it was out. In enterprise projects we were more in favor to grouping functions arguments. The same motivation is also nicely discussed in the recent golang.org blog post: https://blog.golang.org/module-compatibility (also linked in the OP). |
interestingly this is the approach we are looking into for runtime to make the system more modular, stay tuned for a adr/rfc in the coming week |
Summary
Currently we are using configuration methods for setting optional arguments to constructor, example:
This approach is more verbose and less explicit about available options. Also, it doesn't detect duplicates.
Problem Definition
When designing a constructor for objects, very often we need to pass some configuration parameters. Some of them are optional, and some of them are more complex. Moreover we need to design for future extensibility.
Proposal
Recently, in recent post at blog.golang.com provides a great background about interface extensibility and maintenance: Keeping Your Modules Compatible. It also discuss the configuration methods vs configuration structure.
Any change to a function’s signature is a breaking change. The situation is much better with structs. If you have an exported struct type, you can almost always add a field or remove an unexported field without breaking compatibility. When adding a field, make sure that its zero value is meaningful and preserves the old behavior, so that existing code that doesn’t set the field continues to work.
Instead of having a terribly lengthy function parameters:
we can do:
TODO:
appOpts
somehow Update x/banking and x/crisis InitChain re slow Gaia startup #7764 (comment)For Admin Use
The text was updated successfully, but these errors were encountered: