PC Review


Reply
Thread Tools Rate Thread

C# Flow Control

 
 
Dave
Guest
Posts: n/a
 
      6th Jul 2005
Hi,

I've read a few contradictory statements on how to do flow control between
methods. Main problem is that I've seen recommendations (or "standards") to
*not* use exceptions for flow control, but also that exceptions now replace
the need to pass return codes between methods.

So whats best to use?

Thanks.


 
Reply With Quote
 
 
 
 
Tim Haughton
Guest
Posts: n/a
 
      6th Jul 2005
A little article is....

http://www.c-sharpcorner.com/Code/20...Management.asp

There are many, many articles on the web dealing with exception best
practices.

Return codes on methods can occur in all kinds of scenarios. Talking to
hardware, COM interop etc. Generally, it's best to wrap such interfaces in
OO exception savvy code if they're going to be used extensively from OO
code - mainly because branching on return codes is usually indicative of a
procedural design.

If you're writing C# that branches on return codes, ask yourself whether you
would be better served by an OO solution.

Regards,

Tim Haughton

"Dave" <(E-Mail Removed)> wrote in message
news:zQMye.63875$(E-Mail Removed)...
> Hi,
>
> I've read a few contradictory statements on how to do flow control between
> methods. Main problem is that I've seen recommendations (or "standards")

to
> *not* use exceptions for flow control, but also that exceptions now

replace
> the need to pass return codes between methods.
>
> So whats best to use?
>
> Thanks.
>
>



 
Reply With Quote
 
Dave
Guest
Posts: n/a
 
      6th Jul 2005
Thanks Tim.

So it seems that either method (return codes, throwing exceptions) may be
acceptable in certain situations in order to allow branching in the caller
method.

Unless when you are not referring to throwing exceptions when you mention an
OO solution?




"Tim Haughton" <(E-Mail Removed)> wrote in message
news:GdNye.141755$(E-Mail Removed)...
>A little article is....
>
> http://www.c-sharpcorner.com/Code/20...Management.asp
>
> There are many, many articles on the web dealing with exception best
> practices.
>
> Return codes on methods can occur in all kinds of scenarios. Talking to
> hardware, COM interop etc. Generally, it's best to wrap such interfaces in
> OO exception savvy code if they're going to be used extensively from OO
> code - mainly because branching on return codes is usually indicative of a
> procedural design.
>
> If you're writing C# that branches on return codes, ask yourself whether
> you
> would be better served by an OO solution.
>
> Regards,
>
> Tim Haughton
>
> "Dave" <(E-Mail Removed)> wrote in message
> news:zQMye.63875$(E-Mail Removed)...
>> Hi,
>>
>> I've read a few contradictory statements on how to do flow control
>> between
>> methods. Main problem is that I've seen recommendations (or "standards")

> to
>> *not* use exceptions for flow control, but also that exceptions now

> replace
>> the need to pass return codes between methods.
>>
>> So whats best to use?
>>
>> Thanks.
>>
>>

>
>



 
Reply With Quote
 
Tim Haughton
Guest
Posts: n/a
 
      6th Jul 2005
"Dave" <(E-Mail Removed)> wrote in message
news:gwNye.63910$(E-Mail Removed)...
> Thanks Tim.
>
> So it seems that either method (return codes, throwing exceptions) may be
> acceptable in certain situations in order to allow branching in the caller
> method.
>
> Unless when you are not referring to throwing exceptions when you mention

an
> OO solution?
>


Yes that's basically what I'm saying. If you write code that forces clients
of your code to branch, you're writing procedural code, and you're forcing
them to write procedural code too. If your code demonstrates good exception
handling, then you're more likely to be writing OO code and encouraging (but
not forcing) your clients to write OO code to use it.

Regards,

Tim Haughton


 
Reply With Quote
 
=?Utf-8?B?RXJpYyBNYXJ0aGluc2Vu?=
Guest
Posts: n/a
 
      6th Jul 2005
Dave-

There is no absolute answer to this. Exceptions are a perfectly fine
mechanism for flow control between methods, but they should only be used in
truly exceptional cases. The reason for this is twofold. First,
dogmatically speaking, exceptions are intended for exceptional situations,
not for ones likely to occur. Second, pragmatically speaking, exceptions are
costly (read, not performant) so their use should be confined to situations
where they are appropriate. I wrote a brief article that talks about this in
terms of parsing types here:

http://www.codedaily.com/Post_View.aspx?postId=6

Regards-
Eric

"Dave" wrote:

> Hi,
>
> I've read a few contradictory statements on how to do flow control between
> methods. Main problem is that I've seen recommendations (or "standards") to
> *not* use exceptions for flow control, but also that exceptions now replace
> the need to pass return codes between methods.
>
> So whats best to use?
>
> Thanks.
>
>
>

 
Reply With Quote
 
jce
Guest
Posts: n/a
 
      8th Jul 2005
Exceptions are just that...used to catch exceptions and not to report
"status" unless that status is part of an exception such as a network
communication failure - it's not expected, or a database unavailable.

I think the issue in the original post is the confusion of the term "return
codes" versus "return references".

Exception should be used to replace "return codes" where a return code
defines a type of unexpected problem (DBMS is down, network unreachable).
For other flow control you can still return something of value - its just
not called a return code necessarily :-)

JCE

"Tim Haughton" <(E-Mail Removed)> wrote in message
news:GdNye.141755$(E-Mail Removed)...
>A little article is....
>
> http://www.c-sharpcorner.com/Code/20...Management.asp
>
> There are many, many articles on the web dealing with exception best
> practices.
>
> Return codes on methods can occur in all kinds of scenarios. Talking to
> hardware, COM interop etc. Generally, it's best to wrap such interfaces in
> OO exception savvy code if they're going to be used extensively from OO
> code - mainly because branching on return codes is usually indicative of a
> procedural design.
>
> If you're writing C# that branches on return codes, ask yourself whether
> you
> would be better served by an OO solution.
>
> Regards,
>
> Tim Haughton
>
> "Dave" <(E-Mail Removed)> wrote in message
> news:zQMye.63875$(E-Mail Removed)...
>> Hi,
>>
>> I've read a few contradictory statements on how to do flow control
>> between
>> methods. Main problem is that I've seen recommendations (or "standards")

> to
>> *not* use exceptions for flow control, but also that exceptions now

> replace
>> the need to pass return codes between methods.
>>
>> So whats best to use?
>>
>> Thanks.
>>
>>

>
>



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      8th Jul 2005
The article you linked to is hit-and-miss. It contains some good
advice, but also contains a few bits of bad advice, and I found it
rather difficult to read.

Most of the methods you write will naturally return things. The
properties you write will definitely return things. Most of the time
these will be object references. The usual C# idiom for "information
not available" is to return null. So, in that case, you are doing some
"flow control" based on a return value:

Employee e = Employee.GetEmployee(employeeNumber);
if (e == null)
{
... handle "no such employee" case ...
}
else
{
... found the employee ...
}

A method like GetEmployee() should throw an exception only if something
awful and unexpected happens. Depending upon your design, an invalid
employee number may be commonplace or "awful and unexpected". Design
accordingly.

If you don't know ahead of time how your method is going to be used, so
an invalid employee number may be either "awful and unexpected" or
commonplace, throw an exception but provide a validation method:

if (!Employee.IsEmployeeNumberValid(employeeNumber))
{
... handle invalid employee number ...
}
else
{
Employee e = Employee.GetEmployee(employeeNumber);
...
}

One point on which the article errs is that it shows catch clauses for
the Exception type in several places:

try
{
...
}
catch (Exception ex)
{
...
}

You should (almost) never catch an Exception! (Almost) all of your
catch clauses should catch specific types of exceptions, not the
general Exception type. You shouldn't be catching most exceptions
anyway. Instead, you should let them percolate up the call stack and
catch them in global exception handlers.

I find Microsoft's writeup on exception best practices much more
readable and reliable:

http://msdn.microsoft.com/library/de...Exceptions.asp

 
Reply With Quote
 
Tim Haughton
Guest
Posts: n/a
 
      8th Jul 2005
> I think the issue in the original post is the confusion of the term
"return
> codes" versus "return references".
>
> Exception should be used to replace "return codes" where a return code
> defines a type of unexpected problem (DBMS is down, network unreachable).
> For other flow control you can still return something of value - its just
> not called a return code necessarily :-)


Possibly. When I think of return codes, I think of a method returning an
int, then a dirty great switch statement to decide what to do.

Tim Haughton


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Control program flow Dave Unger Microsoft Excel Programming 6 21st Jul 2010 12:02 AM
Control flow =?Utf-8?B?QWJieQ==?= Microsoft Dot NET 0 29th Mar 2007 06:30 PM
RFC: Exceptions and control flow Alvin Bruney [MVP] Microsoft C# .NET 9 20th Feb 2005 11:07 AM
Flow control souris Microsoft Access Macros 6 27th Jan 2005 11:52 PM
Flow control Dennis D. Microsoft VB .NET 10 16th Dec 2004 09:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:37 AM.