I've been playing with returning tuples from methods, so method signatures like this...:
private int GetAlpha() {...} // -1 indicates an error
private bool GetAlpha(out int alphaOut) {...}
...turn into this:
private (bool Success, int Alpha) GetAlpha() {...}
PROS:
- I like avoiding the use of out-of-band values like
-1to signal an error to the caller - I've been reading recently that
outparameters are evil and to be avoided
CONS:
- I can only use the
var (success, a, b, ...) = Foo(...)syntax ONCE in a typical method. Thereafter, I'm forced to declare nearly all variables in the returned tuple, because 'success' is already defined - I not only have to declare return variables, but I also have to explicitly specify their type (vs. implicitly using
var).
var (success, alpha) = GetAlpha();
if (success)
{
//var (success, beta, gamma) = GetStuff(alpha); // ERROR - 'success' is already defined
//(success, beta, gamma) = GetStuff(alpha); // ERROR - 'beta' and 'gamma' are undefined
//(success, var beta, var gamma) = GetStuff(alpha); // ERROR - no such syntax, silly!
string beta;
DateTime gamma;
(success, beta, gamma) = GetStuff(alpha);
.
.
.
The convenience and conciseness of using the implicit declaration and typing is so nice, that it bugs me I'm typically only able to use it once in a method.
Am I missing anything? Is there some other pattern that avoids this?