Suggestion for Best Practice RE Default Values and structs #86099
Unanswered
TonyValenti
asked this question in
Ideas
Replies: 1 comment
-
Why don't you call it with named parameters? using var lease = await AcquireAsync(cancellationToken: token); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi All,
I wanted to mention something I see as not-ideal in a lot of libraries and then provide my solution.
The goal being that code is more clear and has less magic values in it.
As an example, consider this API:
If I want to call this with the default value for
permitCount
with no cancellation token, I can just do this:but if I want to provide a token, then I have to re-specify the default value for ```permitCount``:
This is not ideal because then in a code review, I'm left wondering if 1 is something the developer picked or if it is a default that is just being respecified.
It would be nice if I could simply do this:
as it would logically make sense that I am calling the method with the default value of the parameter, however, per C# rules, that actually means "default value of the type" which is 0 and not what I want.
To get around this, in my own APIs I do something like this:
This allows me to cleanly use
default
to represent the default value for the parameter.Anyways, It would be nice if core APIs did something like this as well.
Beta Was this translation helpful? Give feedback.
All reactions