instead of "bool TryGetSomething(int param, out MyType outvar)", I rather prefer (bool, MyType) TryGetSomething(int param)
instead of "bool TryGetSomething(int param, out MyType outvar)", I rather prefer (bool, MyType) TryGetSomething(int param)
if (TryGetSomething(int param, out var value)) {
// use it here
}
If you returned a tuple you'd have to destructure both variables, and have a separate line for the if and the declaration.Past that, the best tool in an OO language for returning multiple different values is to return a named class created for the task.
It ends up being more readable. Safer, too, since, unlike for tuples, you can refuse to give up a result when the operation failed. It's also more ergonomic to use, since you can make it mappable. That way you can chain together a bunch of things that might fail, and pass along any errors, without having to stick a bunch of extra if-statements into your code.
TValue? TryGetValue<TKey, TValue>(TKey key)
and have it work with any possible TKey. Unless they've fixed this in the past couple years, you get stuck writing your own option type to be able to also work with reference types, which is always fun to try and get past code review, or you fall back on the out parameters idiom, which is not composable.Unnecessary because class types already allow null. I would not be surprised if a method e.g. public Customer TryFindCustomer(int id); returns null if it can't find the customer.
Of course, if you want to return either a customer or an error details, then you need another type or generic wrapper.
I've never liked to see much use of Either because it has the same readability issues as tuples.
if(TryGetSomething(3, out MyType outvar)) { ... }Also, for your example,
Option<MyType> tryGetSomething(int param) is probably better than either.