Re: Design: Custom Exception Usage



On Tue, 08 Apr 2008 13:55:55 -0700, Anthony Jones <Ant@xxxxxxxxxxxxxxxx> wrote:

Hmm... yes perhaps 'plague' is too strong. It is however a bad smell and
one would really have to question a choice to use it.

We'll have to agree to disagree.

Taking TryParse as an example I would have prefered to have:-

bool CanParse(string)

I wouldn't want that at all. If I'm calling CanParse(), 99 times out of a 100 it's because I want to parse the string. It'd be silly to call a method that basically has to do the work of parsing the string, but which only returns a flag telling me if actually parsing the string will work.

If anything the "Try..." pattern is a great example of the usefulness of by-reference parameters.

but TryParse would clearly be faster. Not having CanParse I'm forced to use
TryParse even in code where performance is not an issue (which frankly is
true of most code).

You write "forced" as though there's something about calling TryParse() that is harmful.

Beyond that, however, since sometimes performance _is_ an issue, it's not like TryParse() could have been left out of the API.

int x;
string y = "Brass Monkeys";

if (Int32.TryParse(y, out x))
{
// do stuff with x but how did x get assigned a value its not that
obvious
}

Why is it the case that "how did x get assigned a value" is "not that obvious"? The "out" in the parameter list for the call to TryParse() makes it very clear where x is getting assigned.

Especially in the context of C# where the compiler is very strict about dealing with uninitialized variables, I never find myself wondering how something got assigned. Because an "out" parameter is required by the compiler to be assigned in the method being called, a call like this is no exception.

Where I would have prefered:-

if (Int32.CanParse(y))
{
x = Int32.Parse(y);
// do stuff with x and its clear where x got its value.
}

If performance is not a concern (and you already wrote that it almost never is), you can always just wrap a call to Parse() with a try/catch block. That provides the exact assignment pattern you're asking for.

OR alternatively

int? x;
string y = "Jon 'Google' Skeets idea'"

x = Int32.ParseOrNull(y)
if(x != null)
{
//do stuff with x
}

I would not be against the inclusion of a method like that as an _optional_ pattern. However, I certainly would resent being forced to use it in situations where I don't already have a nullable type.

Of course its easy enough to build round but it means doing so out of
principle rather than actual need.

Indeed. And IMHO it's the principle that's flawed. There's nothing inherently wrong with passing by reference. Why go out of your way to avoid it?

Pete
.



Relevant Pages

  • Re: Design: Custom Exception Usage
    ... 100 it's because I want to parse the string. ... TryParse even in code where performance is not an issue (which frankly is ... No I wrote "Not having CanParse I'm forced ...". ... That provides the exact assignment pattern you're asking for. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Design: Custom Exception Usage
    ... 100 it's because I want to parse the string. ... Sorry to break the news to you: it's _already_ used so often that it's a standard pattern in .NET. ... you could easily write your own "CanParse". ... The "out" in the parameter list for the call to TryParse() ...
    (microsoft.public.dotnet.languages.csharp)
  • index function problem
    ... I tried a code to find the indices of the ocurences of a pattern in a ... Incompatible ranks 0 and 1 in assignment at ... What is wrong with the string separation? ...
    (comp.lang.fortran)
  • Re: [KSH] Assign a matched pattern to a shell variable
    ... > portion of the string that the pattern matched. ... but matched against the string: ... > the assignment to extract a substring from $. ...
    (comp.unix.shell)
  • Re: Regex for decimal and money
    ... but how to prevent the user from entering something silly like 1.5.6? ... Try a pattern that will allow only one "." ... You should try the TryParse of the ...
    (microsoft.public.dotnet.languages.csharp)