The goal was to provide flexibility for developers to enable what they wanted.
Two downsides to this approach are:
Every new option requires a new function
Every new option requires a function call when call knuu.New()
Additionally this has the New method applying options and then setting defaults.
This approach is flexible, but could lead to developer errors because the method is making assumptions based on inputs verse handling those inputs more strictly.
Proposed Change
If we use an Options struct we can have the same flexibility for developers with the following benefits:
No new function definitions
No additional function calls on knuu.New()
Ability to define DefaultOptions
To start the Options struct would be the following.
type Options struct {
system.SystemDependencies
timeout time.Duration
proxyEnabled bool
}
This currently is basically duplicating the Knuu struct, but the benefit here is that it allows us to update the knuu and options structs independently.
This would also allow us to update the New method in a way that still provides flexibility while also being more strict on validating inputs.
func New(ctx context.Context) (*Knuu, error) {
return NewWithOptions(ctx, DefaultOptions())
}
func NewWithOptions(ctx context.Context, opts Options) (*Knuu, error) {
// First validate options
...
// Then apply
k := &Knuu{opts}
// Do other things
return k, nil
}
func DefaultOptions() Options {
// Initialize defaults
return
}
This allows the use of New() without needing to worrying about any options, which is create to testing and getting started.
Also refactoring out the DefaultOptions makes it easy for developers to get the defaults and just update the options they need to.
Current
With the new refactor the following knuu option structure was introduced.
The goal was to provide flexibility for developers to enable what they wanted.
Two downsides to this approach are:
Additionally this has the New method applying options and then setting defaults.
This approach is flexible, but could lead to developer errors because the method is making assumptions based on inputs verse handling those inputs more strictly.
Proposed Change
If we use an Options struct we can have the same flexibility for developers with the following benefits:
To start the Options struct would be the following.
This currently is basically duplicating the Knuu struct, but the benefit here is that it allows us to update the knuu and options structs independently.
This would also allow us to update the New method in a way that still provides flexibility while also being more strict on validating inputs.
This allows the use of
New()
without needing to worrying about any options, which is create to testing and getting started. Also refactoring out theDefaultOptions
makes it easy for developers to get the defaults and just update the options they need to.