2

In some .NET libraries, there's a pattern of two alternative ways to call a function.

int i = x.GetValue(k); /* Might throw. */
if (x.TryGetValue(k, out int i) { /*...*/ } else { /* ... */ }

I don't like the names of these two functions as the not-Try variant presents itself the "normal" way to call a function. TryX, having the added word in the name, feels like a specialist form. For my new library that doesn't have to worry about existing users, I'd like to switch that around.

If I rename TryGetValue to just GetValue, what do I call the original GetValue that will throw an exception if it can't complete my request?

Is there a standard name for this pattern? (I'm hoping the industry has an established pattern rather than coining a new word in the comments, but you're welcome to do so if you wish.)

EDIT: I could be persuaded that TryGetValue is the correct name, but I'd still like to change the name of GetValue that throws an exception, leaving no function called just GetValue.

7
  • 3
    In the .NET space, for better or worse, the patterns in your example above are the norm, so nothing is jumping to my mind as a common naming pattern for reversing that.
    – GHP
    Mar 27, 2020 at 13:42
  • 1
    Does this answer your question? Naming guard clauses that throw exceptions Mar 27, 2020 at 14:09
  • 1
    See also stackoverflow.com/questions/1169158/… Mar 27, 2020 at 14:10
  • If you don't like the idea of having two methods in your API, consider returning Maybe<T>. So for example public Maybe<int> GetValue(string val).
    – Rik D
    Mar 27, 2020 at 15:59
  • Use XMLdoc on your GetValue() method to explicitly call out the exception that might be thrown.
    – mmathis
    Mar 27, 2020 at 17:29

1 Answer 1

2

The TryX is a C# convention to indicate that the caller must check for errors explicitly and manually: failure is to be expected as part of normal program execution, so there won't be an exception.

Eliding this prefix is likely to lead to buggy code. Don't try to save these three characters, just stick to the convention.

In other languages, the compiler can enforce that the return value is checked, e.g. via [[nodiscard]] in C++ or #[must_use] in Rust. C# has no comparable feature.

3
  • I could be persuaded that TryGetValue is correct, but is GetValue the best name for the other varient?
    – billpg
    Mar 27, 2020 at 13:57
  • @billpg: I'm inclined to agree with Graham. There's no established convention like GetValueAndThrowIfInvalid. Feel free to create your own convention. Mar 27, 2020 at 14:09
  • 2
    @billpg You don't have to create a throwing or non-throwing variant – that depends entirely on how these functions are supposed to be used. But if you want to steer users towards the non-throwing version, picking a longer name for the throwing version would be desirable. I'd consider GetValueOrThrow().
    – amon
    Mar 27, 2020 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.