-
Notifications
You must be signed in to change notification settings - Fork 605
Use optionals #125
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
Comments
There have been similar proposals in the past. We'd be happy to include something like this but it will not replace the API we already have: breaking it is not an option. Named parameter arguments in new-ish C# versions has been a decent middle ground recently.
|
Nothing would break. In the case of pre-existing code, the optional values would already be provided, so there is no difference to the developer. |
@MisinformedDNA so your suggestion is to simply use the default argument values? That makes a lot of sense. We may be short on time to fit this into the 3.6.0 release but after that, I'm all for it. |
I have started work on this issue and it is mostly straightfoward, however for methods such as
@michaelklishin WDYT? |
@kjnilsson we can introduce a new method, |
…and leave the current method alone. |
We could although that introduces more API and code to maintain. It seems I am able to change the method signature in IModel and still retain the old api using extension methods. See this wip commit: a66e9ee This looks to me like a potential way forward. |
Optional parameters are often not something you want introduce on a public API, because making any changes to those methods results in a binary breaking change if you aren't incredibly careful! http://haacked.com/archive/2010/08/10/versioning-issues-with-optional-arguments.aspx/ It is possible to do it without causing breaks, but it requires following some very strict rules. For example, take a look at the Roslyn guidelines: Multiple overloads are often a better way to go for a public API. |
This assumes breaking binary changes are a no-no. I don't think so. The 4.0 release is a great opportunity to introduce breaking changes to a 10 year old codebase
|
Sure, moving up to a new major release is definitely the time to be considering breaking changes. I'm thinking more about what happens with releases past that point. If you introduce optionals in 4.0, then you now take on the maintenance cost of avoiding breaks as you continue to modify the API. |
Thanks @bording for pointing out the issues with optionals and binary compatibility. I hadn't considered that. It is unlikely that the API is going to change significantly going forward but I agree using optionals in IModel may not be the best approach from a maintenance POW. We could move the signatures with optionals to extension methods as these wouldn't need to change if the underlying API method had to evolve. (Any parameter type change or removal would be a breaking change anyway and would require a major version bump). This would also allow us to reduce the surface area of IModel which I think is a good thing. I am not keen on convenience overloads in interface contracts and would like to see this concern separated out if possible. |
@kjnilsson For interfaces, a good approach I've seen in the past is to only have the the method with all the parameters on the interface itself, and then all of the convenience overloads are extension methods, which lets all implementations of the interface get them for free. I think this what you're suggesting above? Of course, versioning interfaces brings it own set of challenges, regardless of optional parameters, because any changes there can be both compile and binary breaks, depending on the change. |
Make parameters optional.
So instead of this
consider something like
This way we can just set the parameters we are interested in and just accept the defaults for the rest.
The text was updated successfully, but these errors were encountered: