473,748 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A common architectural problem

Hi,

Assuming having a method that calls another method which also calls other methods and so on having a long winded tree of methods. What do I do in case it turns out everything has to be aborted? The only reasonable way I see here is to use a exception but everybody is telling exceptions should not be used for control flow. I just can't think of another reasonable way to deal with this.

Are there any good books that teach how to do great architecture?

thanks in advance
Frank
Jan 22 '06 #1
11 1772
Hello stax,

This is definatly a debated issue. A rule of thumb though. Exceptions are
Exceptions. They should really only be used for unexpected cases, like invalid
input to a method or some infrstructure bits not clicking (like opening databse
connections etc). Throwing exceptions is a bit expensive and minimizing thrown
exceptions will affect your performance positivly.

For success/failure indication you should use return values.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
http://www.cornerstone.se

Hi,

Assuming having a method that calls another method which also calls
other methods and so on having a long winded tree of methods. What do
I do in case it turns out everything has to be aborted? The only
reasonable way I see here is to use a exception but everybody is
telling exceptions should not be used for control flow. I just can't
think of another reasonable way to deal with this.

Are there any good books that teach how to do great architecture?

thanks in advance
Frank

Jan 22 '06 #2
I don't know how you scenario looks like, but maybe the class
System.Transact ions.Transactio nScope and
System.Transact ions.DependentT ransaction would be of a good use to you in
case you need to rollback. This class is new to .NET 2.0.

"stax" <_r************ ************@ya hoo.de> wrote in message
news:dq******** **@online.de...
Hi,

Assuming having a method that calls another method which also calls other
methods and so on having a long winded tree of methods. What do I do in
case it turns out everything has to be aborted? The only reasonable way I
see here is to use a exception but everybody is telling exceptions should
not be used for control flow. I just can't think of another reasonable way
to deal with this.

Are there any good books that teach how to do great architecture?

thanks in advance
Frank

Jan 22 '06 #3
> For success/failure indication you should use return values.

I searched quite a bit but didn't find something so maybe you or somebody else can point me to some resources. The problem with return values is it adds a lot boilerplate code as the return value has to be checked for every method call. My application has a really long winded processing going on. Problems with this approach are often I want to return some types instead of a boolean return value and I'm not very comfortable with global variables or ref parameters.
Jan 22 '06 #4
Patrik Löwendahl [C# MVP] <pa************ **@home.se> wrote:
This is definatly a debated issue. A rule of thumb though. Exceptions are
Exceptions. They should really only be used for unexpected cases, like invalid
input to a method or some infrstructure bits not clicking (like opening databse
connections etc). Throwing exceptions is a bit expensive and minimizing thrown
exceptions will affect your performance positivly.
Very few applications will actually see any significant performance
degredation due to using exceptions unless they're thrown hundreds of
thousands of times a second (in which case chances are something more
serious is wrong anyway).

See http://www.pobox.com/~skeet/csharp/exceptions.html for more
empirical data on this.
For success/failure indication you should use return values.


No. If a method has failed, it should throw an exception. Return values
are meant to be used (IMO) for the result of the operation of the
method, not whether or not the method has succeeded in doing what it's
been told to do.

The whole "don't use exceptions for control flow" is very wishy-washy -
it doesn't describe what "control flow" really means, and as exceptions
pretty much *always* affect control flow, it's hard to know what is
actually intended.

There are times when it can be tricky to decide whether or not to use
an exception, but in this case (where a large operation needs to be
aborted) an exception sounds just the ticket to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #5
That really depends on the context.

In your example you're doing nothing else then throwing exceptions in a console
application and, as you yourself state, only at one level deep.

Using a different scenario,

Create an ASP.NET application and let it throw a couple of exceptions around
the call stack. Put a thousand or so worth of user load on the app with ACT
and measure throughput. Then get rid of the exception and do another measure.
You will see a significant increase in the throughput for the application
with less exceptions.

Regardless of the performance issue, there's a semantic and architectural
issue to consider as well.

Exceptions are errors, problems with your code that you couldn't anticipate
writing it. Consider the register user scenario.

1) A user registers for a website.
2) A users login name must be unique.

Implementing this what would be the better way? Trying to persist the user
and throw an exception if it fails or anticipate the problem in advance by
checking the input values and send feedback directly?

I would do this:

public enum RegisterStatus { Ok, LoginNameExists };

public RegisterStatus RegisterUser(Us er newUser) {
bool loginExists = UserRepository. CheckLoginExist s(newUser.Login Name);

if ( !loginExists ) {
UserRepository. Register(newUse r);
return RegisterStatus. Ok;
}
else {
return Register.LoginN ameExists;
}
}

Which IMO is a much clearer. Since we expect users to enter login names that
are already used, that is not an Exceptional case and not really an error
on the users behalf, he couldn't know.

Now in another scenario:

1) We want a email class that are supposed to send emails.
2) When sending an email we expect a correct email address to be given to
us and a message to send.

Now here's a place where exceptions do come in. We expect an email adress
to our function, if we don't get it, an exception should be thrown. In this
case it's all about giving the function enough and correct information to
be able to do its job.

public void SendEmail(strin g to, string from, string subject, string message)
{
if ( !Validator.IsVa lidateEmail(to) )
throw new ArgumentExcepti on("Not a valid email", "to");

if ( !Validator.AreN otEmptyOrNullSt ring(messsage) )
throw new ArgumentExcepti on("I need an email text to send", "message");

// Etc....
}

In this case the user are required to know how an emailadress is written,
we expect a user of an email class to know this. Therefore, not getting a
valid email adress is an Exceptional case.

For some more information about exceptions and performance / scalability,
have a browse over at: http://msdn.microsoft.com/library/de...netchapt05.asp
--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
http://www.cornerstone.se

Patrik Löwendahl [C# MVP] <pa************ **@home.se> wrote:
This is definatly a debated issue. A rule of thumb though. Exceptions
are

Exceptions. They should really only be used for unexpected cases,
like in

valid
input to a method or some infrstructure bits not clicking (like
opening d

atabse
connections etc). Throwing exceptions is a bit expensive and
minimizing t

hrown
exceptions will affect your performance positivly.

Very few applications will actually see any significant performance
degredation due to using exceptions unless they're thrown hundreds of
thousands of times a second (in which case chances are something more
serious is wrong anyway).

See http://www.pobox.com/~skeet/csharp/exceptions.html for more
empirical data on this.
For success/failure indication you should use return values.

No. If a method has failed, it should throw an exception. Return
values are meant to be used (IMO) for the result of the operation of
the method, not whether or not the method has succeeded in doing what
it's been told to do.

The whole "don't use exceptions for control flow" is very wishy-washy
- it doesn't describe what "control flow" really means, and as
exceptions pretty much *always* affect control flow, it's hard to know
what is actually intended.

There are times when it can be tricky to decide whether or not to use
an exception, but in this case (where a large operation needs to be
aborted) an exception sounds just the ticket to me.

Jan 22 '06 #6
Patrik Löwendahl [C# MVP] <pa************ **@n0spam.home. se> wrote:
That really depends on the context.

In your example you're doing nothing else then throwing exceptions in a console
application and, as you yourself state, only at one level deep.
Although I also give the figures for throwing an exception 20 stack
frames - my laptop throws 42 of those per millisecond. I can't see how
using ASP.NET would make any difference.
Using a different scenario,

Create an ASP.NET application and let it throw a couple of exceptions around
the call stack. Put a thousand or so worth of user load on the app with ACT
and measure throughput. Then get rid of the exception and do another measure.
You will see a significant increase in the throughput for the application
with less exceptions.
Do you have any statistics and code handy? I would be interested to see
that increase and investigate it. You see, to have an exception or two
making a significant difference means that there has to be a
significant CPU load on the web server to start with. If most of the
time spent on the request is actually waiting for the database, then
changing the time taken by a fortieth of a millisecond is unlikely to
make much odds. It might happen in an app which doesn't actually have
any work to do - but I doubt that there are many apps of that nature
which actually get enough hits for exceptions to make a difference. For
an exception which takes 1/42 milliseconds to throw to make a 5%
difference, the whole request has to take less than half a millisecond
to execute - 2000 requests per second, or 7 million per hour. And
that's only if *every* request throws a 20-stackframe-deep exception,
in which case something is very clearly wrong. If only one in ten hits
throw the exception, the for those hits to make a 5% difference to
overall performance (assuming a CPU bottleneck) then the average time
taken by a request would have to be even smaller.
Regardless of the performance issue, there's a semantic and architectural
issue to consider as well.


Do you mind if we just address the performance issue for the moment?
I'd like to come back to the semantics later, but I suspect if we mix
up the two we'll get in a muddle. Or I will at least. (It would also
take me longer to write about now, and I want to go to bed very soon.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #7

Only reasonable to show code and stats for a claim like this. I will set
up a scenario I use in my perf/scalability classes in the morning. I too
want to get to bed soon :)

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
http://www.cornerstone.se
Patrik Löwendahl [C# MVP] <pa************ **@n0spam.home. se> wrote:
That really depends on the context.

In your example you're doing nothing else then throwing exceptions in
a c

onsole
application and, as you yourself state, only at one level deep.

Although I also give the figures for throwing an exception 20 stack
frames - my laptop throws 42 of those per millisecond. I can't see how
using ASP.NET would make any difference.
Using a different scenario,

Create an ASP.NET application and let it throw a couple of exceptions
aro

und
the call stack. Put a thousand or so worth of user load on the app
with A

CT
and measure throughput. Then get rid of the exception and do another
meas

ure.
You will see a significant increase in the throughput for the
application

with less exceptions.

Do you have any statistics and code handy? I would be interested to
see that increase and investigate it. You see, to have an exception or
two making a significant difference means that there has to be a
significant CPU load on the web server to start with. If most of the
time spent on the request is actually waiting for the database, then
changing the time taken by a fortieth of a millisecond is unlikely to
make much odds. It might happen in an app which doesn't actually have
any work to do - but I doubt that there are many apps of that nature
which actually get enough hits for exceptions to make a difference.
For an exception which takes 1/42 milliseconds to throw to make a 5%
difference, the whole request has to take less than half a millisecond
to execute - 2000 requests per second, or 7 million per hour. And
that's only if *every* request throws a 20-stackframe-deep exception,
in which case something is very clearly wrong. If only one in ten hits
throw the exception, the for those hits to make a 5% difference to
overall performance (assuming a CPU bottleneck) then the average time
taken by a request would have to be even smaller.
Regardless of the performance issue, there's a semantic and
architectural

issue to consider as well.

Do you mind if we just address the performance issue for the moment?
I'd like to come back to the semantics later, but I suspect if we mix
up the two we'll get in a muddle. Or I will at least. (It would also
take me longer to write about now, and I want to go to bed very soon.)

Jan 22 '06 #8
Patrik Löwendahl [C# MVP] <pa************ **@n0spam.home. se> wrote:
Only reasonable to show code and stats for a claim like this. I will set
up a scenario I use in my perf/scalability classes in the morning. I too
want to get to bed soon :)


Great, thanks. I'll look forward to trying to reproduce your results
and tweak them etc :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #9
I do not believe that exceptions should be used for flow control. Exceptions
should be used for errors. I think it is more appropriate to say that
Exceptions should be used for unexpected errors.

For example, if a user supplies a login/password to login into a database,
and that fails, is that reason enough for an exception? In an interactive
situation I would not consider this appropriate for an exception. In a batch
situation, where the login/password has been "preset", I would probably
consider this an exception.

If you are lamenting the need to add if tests to your code to test for
return values, consider the alternative of adding heaps of nested try-catch
blocks to your code. In terms of readability, I will take the return values
any day over a bunch of try/catch blocks.

"stax" <_r************ ************@ya hoo.de> wrote in message
news:dq******** **@online.de...
Hi,

Assuming having a method that calls another method which also calls other
methods and so on having a long winded tree of methods. What do I do in
case it turns out everything has to be aborted? The only reasonable way I
see here is to use a exception but everybody is telling exceptions should
not be used for control flow. I just can't think of another reasonable way
to deal with this.

Are there any good books that teach how to do great architecture?

thanks in advance
Frank

Jan 23 '06 #10

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

Similar topics

3
6800
by: S.W. Rasmussen | last post by:
With the risk of being accused of multi-posting I would like to draw the attention to a serious visual basic/windows issue discussed in the microsoft.public.vb.bugs newsgroup. As pointed out below by Norman Diamond the bug may result in loss of data when saving files with the standard common dialog control. Norman Diamond describes the problem as follows: ---------- In VB6 SP6, VB6 SP5, and possibly others, the common dialog box...
0
1244
by: Andrzej | last post by:
I am looking up for book about XML architectural forms. Or good site. Regards Andrzej
14
1342
by: John Spiegel | last post by:
Hi all, I'm in the early stages of designing a system that we'll use in-house and am looking for opinions and suggestions for further reading on which directions to take. Background: Though we're a small company, I'd like to design this with the mindset of a larger company from the start. This will be an ongoing project to encompass a lot of needs, which will generally be variations on standard themes: Customer account management,...
2
1272
by: Viet | last post by:
I have an architectural issue that I have been working on for quite awhile now and I would like another person's point of view. This issue involves the conversion of a VB6 app to VB.NET. In this VB6 app, the original programmer used 5 timers to periodically scan a document, process it, and display thumbnail images of the scanned document. These 5 timers implement 5 different subroutines that enable this process. In my conversion of this...
2
1588
by: engwar | last post by:
Which starter kit or open source project would you consider well-architected? Or more specifically which has good examples to showing a newbie how to separate presentation/business logic/data layer? The Time Tracker 1.x version does NOT seem to fit the bill as there is data access code in the business logic layer. The data layer contains only the SQLHelper class. The duwamish codebase seems to be put together a little better but it
0
920
by: Radu | last post by:
Hi. I have the following problem: The user has to be able to upload lists of PINS (in an Excel/CSV format). Upon receiving them, the aspnet app must run many rather complicated queries using the uploaded PIN list and the 'static' files already in the database. To simplify my coding life, I have imagined this solution: a. each user, on login, is assigned a 'Session ID' number, composed of the PIN of the user and Seconds(now), assuming...
2
1240
by: DC | last post by:
Hi, I have designed some data objects that I want to be filled by several providers. Do these objects typically belong into a seperate project (plus each provider is a separate project)? The providers do require a number of XML and XSLT files. I guess these belong into the webproject so the provider dll can access the files?
4
1622
by: Girish | last post by:
i have to implement caching in my application, can any one tell me about the good techniques to implement caching, or provide some architectural help , so i can use it to my application. i want to control caching dynamically according to my configuration. Thanks,
6
11494
Markus
by: Markus | last post by:
Things to discuss: Headers What are they? What does PHP have to do with headers? Why can they only be sent before any output? Common causes
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.