Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 2
    Regarding on the "returns either true or false" part of the answer: Consider twice if you want to do that or use an exception - because one day, you will TryParse and forget to check the return value. And silent failures are the worst thing that can happen to you (better your application crashes & burns - telling you that something went wrong - than it keeps working with oh-so-slightly wrong values, which are a pain to debug and may cause things to crash & burn... in the real world) Commented Feb 17, 2020 at 13:16
  • After reading this it seems so obvious to just add a "try" to the function name. Thanks! I also prefer things do go down guns blazing, so I'll stick to the exception approach. Commented Feb 17, 2020 at 13:18
  • @CharonX I agree. Personally, I don't like much the approach of returning a true/false thing, but I think the naming convention is on-point. Commented Feb 17, 2020 at 14:08
  • Re not checking the return value of a TryParse - if you're working in C#, it's pretty easy to write an analyzer in Roslyn that will turn forgetting to check into an error. Commented Feb 17, 2020 at 15:54
  • Well, aside from returning true or false, and throwing an exception... we could use an option type. And depending on the language/platform, that might be the idiomatic way of doing things. For C# TryX returning bool is idiomatic. And yes, Roslyn can help with that. I wish we didn't have to add a reference (or roll our own) to have an option type... However, since we are talking of an integer, in .NET int? would be an alternative. Commented Feb 17, 2020 at 16:02