473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Re: Design: Custom Exception Usage

On Fri, 11 Apr 2008 02:28:53 -0700, Anthony Jones <An*@yadayadayada.com>
wrote:
>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.

Well thats the crux of the matter isn't it? Why is it "silly" ?
Just my opinion, granted...but it's "silly" because you are doing the same
thing twice. Each time you return a different kind of result: the first
time, you return "did I succeed?", the second time you return "the outcome
of my success". But the work leading up to that is basically the same.
[...]
>If anything the "Try..." pattern is a great example of the usefulnessof
by-reference parameters.

Its also an example of obscurity of function.
As Marc pointed out, the "Try..." pattern isn't obscure. Later you wrote
"it may become part of the expected structure of code". Sorry to break
the news to you: it's _already_ used so often that it's a standard pattern
in .NET.
[...]
>You write "forced" as though there's something about calling TryParse()
that is harmful.

No I wrote "Not having CanParse I'm forced ...". Its not the presenceof
TryParse as an option which bothers me its not having a CanParse that I
have
issue with.
That's not what I meant. Your statement seemed to imply that you find
something wrong with the "Try..." pattern. The rest of your post seems to
say that too.

For what it's worth, you could easily write your own "CanParse". It would
be trivial. You aren't forced to use the "Try..." pattern at all.
[...]
>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.

Its a relative thing. True it isn't actually hard to see that the out
keyword on the parameter indicates where x gets its value. However itis
harder to see than x = because that's what our minds will be condition to
expect when looking for an assignment. This may seem to be a
microscopic
and insigniifcant difference but to think so is a mistake.
Okay. I do have sympathy for the question of what your brain sees versus
what the compiler sees. In this particular case, I don't have any trouble
seeing what the compiler sees, but I've been in other situations where I
do, so I'll grant you that you yourself may have trouble with this.

Still, I think it's a scenario worth retraining yourself for. The
"Try..." pattern isn't going away, and it provides a performance benefit
for trivial effort on your part. I agree most of the time it's not a
needed performance benefit, but I don't think this is a scenario where the
non-performant variation carries some huge benefit over the performant one
from a readability point of view.

Is it really that hard to teach yourself to recognize passing by-reference
semantics?
[...]
The same principle can be applied to readability of code. Small
differences
such as CanParse/Parse vs TryParse only have a small effect individually
on
readability however the cumalitive effect of computer oriented over human
oriented coding is significant.
I guess this is where we simply will disagree. I don't find by-reference
parameters hard to read in code, and on top of that, while I agree that
they should be used very sparingly, they are an important part of the
language and it's my opinion that if you're having trouble recognizing the
pattern, the solution is to spend more time with the language so that the
pattern is recognizable, rather than to change the framework so that you
can call the same thing twice with slightly different semantics each time.

As I said, I have sympathy for your position -- readability is a pet
project of mine too -- but I think that in this case you'll be better
served by drinking the Kool Aid. :)
[...]
>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.

That simply replaces one unusual pattern of code with another. Not a
solution to the human problem I'm looking for.
This I don't understand. All that the try/catch adds is a few more
lines. The assignment is right in there, just as you've expected. That
said, you could always add the CanParse() method yourself.

So, I'm curious: have you? Is this really important enough to you that
you've dealt with the problem? Or is it just something you'd rather just
complain about?
[...] It would be nice to have the choice of all three but if I
could only have two I would take CanParse and TryParse. Sadly I only have
the one TryParse.
That's not really true. You can add both the CanParse() and the nullable
return value pattern to your own code, trivially.
[...]
ByRef parameters are unusual and can catch the less wary out. They can
be
gotchas even for the experienced. Whilst they do have their place they
should be used sparingly and be treated with suspicion. In short, they
smell bad.
I'd say that's the other place I disagree. Granted, "smell bad" isn't a
techincal phrase anyway, so by its very nature using it is going to be
imprecise. But I don't think a particular pattern "smells bad" just
because it should be used sparingly.

Maybe you're just using the phrase differently from the way I'd use it.
For me, code only "smells bad" (inasmuch as it could have a smell :) ) if
and when something is used inappropriately. If it's something that should
be used sparingly, but it's still being used appropriately, then that's
fine-smelling code.

Pete
Jun 27 '08 #1
0 945

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Dunaway | last post by:
VB.Net: I have create a custom collection to hold instances of my custom class. The collection has an Item property which is set as the default property. I can use code such as this to...
3
by: Steve - DND | last post by:
I was wondering if anyone can point me to some articles or practices they use for dealing with errors in their applications; particularly in an n-tier design. I am trying to find one way to...
3
by: Eric Hudson | last post by:
A quick rant and a question. Why does the designer in VS2003 delete code about custom components that it can't load? I've had large sections of forms disappear in the blink of an eye just...
0
by: Eric Hudson | last post by:
Hi, I am using Visual C#.NET (2003 Env., Framework 1.1) I wrote a custom control which seems to work just fine except that if I try to add it to another form I get the following exception: ...
0
by: Steven | last post by:
Hi, I'm creating a custom checkbox list control which will take 2 arraylists as input. (One will contain Names and other will contain 0s and 1s. 0 - uncheck 1- check). I'm able to create the...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
2
by: GarrettD78 | last post by:
I have a form that I am upload rows into an SQL server database. The form has a button that when it is clicked creates a Custom Business Object and then calls the actually upload method from that...
1
by: Mark Olbert | last post by:
Has anyone else noticed that the design-time support for databinding in custom controls in ASPNET2 sucks? At least for GridViews? So far I've spent going on two days trying to get the following...
1
by: Olaf | last post by:
Hi, I try to design a program which has to run on console. There is only one exe/binary and depend on the calling name argv the different tasks/commands should be performed as aliases. It's the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.