comp.lang.ada
 help / color / mirror / Atom feed
* Whether to raise exception or End_of_List function
@ 2001-11-21 15:17 Preben Randhol
  2001-11-21 16:32 ` Preben Randhol
                   ` (5 more replies)
  0 siblings, 6 replies; 16+ messages in thread
From: Preben Randhol @ 2001-11-21 15:17 UTC (permalink / raw)


Hi

I have made a simple double linked list that I use in the application I
develop. Before somebody suggest I use some other libraries I want to
say that I made this double linked list to learn Ada 95 and get aware of
different problems/aspects of implementing this, so at least for this
application I want to keep it for now.

The list code can be viewed here: http://www.pvv.org/~randhol/Ada95/List/
Note that the code is still rough, I haven't removed some debugging
functions/procedures etc...

I put this into a package:

   package Examine_List is
         new Double_Linked_List (Data_Type => Natural);

   Main_Examine : Examine_List.List_Type := Examine_List.New_List;

And use the Main_Examine. So far it has worked nicely, but now I came to
that at one place (at least) in my application I have to go from the
current position of the iterator until the end of the list. I cannot
know the current position of the list iterator so I cannot use a
for-loop. If I want to use a while loop however there are the problem of
knowing when the end of the list is reached. I guess I can make a
function End_of_List (which must also work if one try to backwards in
the list) or I can just raise an exception when the user tries to
step off the list.

My question is which is the better solution; Exception or a End_of_List 
function and which would have fewer side effects?

Thanks in advance.

Preben
-- 
 ()   Join the worldwide campaign to protect fundamental human rights.
'||}
{||'                                           http://www.amnesty.org/



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 15:17 Whether to raise exception or End_of_List function Preben Randhol
@ 2001-11-21 16:32 ` Preben Randhol
  2001-11-21 16:51 ` Mark Biggar
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 16+ messages in thread
From: Preben Randhol @ 2001-11-21 16:32 UTC (permalink / raw)


On Wed, 21 Nov 2001 15:17:52 +0000 (UTC), Preben Randhol wrote:

I forgot to say that the program I'm making can be viewed here:

   http://www.pvv.org/~randhol/Ada95/Glosa/

Preben
-- 
 ()   Join the worldwide campaign to protect fundamental human rights.
'||}
{||'                                           http://www.amnesty.org/



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 15:17 Whether to raise exception or End_of_List function Preben Randhol
  2001-11-21 16:32 ` Preben Randhol
@ 2001-11-21 16:51 ` Mark Biggar
  2001-11-21 17:00   ` Preben Randhol
  2001-11-21 17:01 ` Larry Kilgallen
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 16+ messages in thread
From: Mark Biggar @ 2001-11-21 16:51 UTC (permalink / raw)


Preben Randhol wrote:
> 
> Hi
> 
> I have made a simple double linked list that I use in the application I
> develop. Before somebody suggest I use some other libraries I want to
> say that I made this double linked list to learn Ada 95 and get aware of
> different problems/aspects of implementing this, so at least for this
> application I want to keep it for now.
> 
> The list code can be viewed here: http://www.pvv.org/~randhol/Ada95/List/
> Note that the code is still rough, I haven't removed some debugging
> functions/procedures etc...
> 
> I put this into a package:
> 
>    package Examine_List is
>          new Double_Linked_List (Data_Type => Natural);
> 
>    Main_Examine : Examine_List.List_Type := Examine_List.New_List;
> 
> And use the Main_Examine. So far it has worked nicely, but now I came to
> that at one place (at least) in my application I have to go from the
> current position of the iterator until the end of the list. I cannot
> know the current position of the list iterator so I cannot use a
> for-loop. If I want to use a while loop however there are the problem of
> knowing when the end of the list is reached. I guess I can make a
> function End_of_List (which must also work if one try to backwards in
> the list) or I can just raise an exception when the user tries to
> step off the list.
> 
> My question is which is the better solution; Exception or a End_of_List
> function and which would have fewer side effects?

Even though it may seem redundant, you should have both.  You need the
exception to handle illegal list usage notification.  But, as a general
rue of thumb for packages of this type, such a package should provide 
test functions that pre-test for exceptional conditions so a user can
program around the exception.

--
Mark Biggar
mark.a.biggar@home.com



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 16:51 ` Mark Biggar
@ 2001-11-21 17:00   ` Preben Randhol
  0 siblings, 0 replies; 16+ messages in thread
From: Preben Randhol @ 2001-11-21 17:00 UTC (permalink / raw)


On Wed, 21 Nov 2001 16:51:50 GMT, Mark Biggar wrote:
> 
> Even though it may seem redundant, you should have both.  You need the
> exception to handle illegal list usage notification.  But, as a general
> rue of thumb for packages of this type, such a package should provide 
> test functions that pre-test for exceptional conditions so a user can
> program around the exception.

Hmm. Yes, makes good sense! I was so hung up one either that I didn't
think to use both. Thanks.

Preben who looks at Winnie the Pooh on his coffe cup pad and starts
coding again. :-)
-- 
 ()   Join the worldwide campaign to protect fundamental human rights.
'||}
{||'                                           http://www.amnesty.org/



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 15:17 Whether to raise exception or End_of_List function Preben Randhol
  2001-11-21 16:32 ` Preben Randhol
  2001-11-21 16:51 ` Mark Biggar
@ 2001-11-21 17:01 ` Larry Kilgallen
  2001-11-21 17:03   ` Preben Randhol
  2001-11-21 19:34   ` Ed Falis
  2001-11-21 17:04 ` chris.danx
                   ` (2 subsequent siblings)
  5 siblings, 2 replies; 16+ messages in thread
From: Larry Kilgallen @ 2001-11-21 17:01 UTC (permalink / raw)


In article <slrn9vnh96.55u.randhol+abuse@kiuk0156.chembio.ntnu.no>, Preben Randhol <randhol+abuse@pvv.org> writes:

> My question is which is the better solution; Exception or a End_of_List 
> function and which would have fewer side effects?

Exceptions are supposed to be rare events.  In many applications
reaching the end of a list is fully expected and not necessarily
rare.

Exception processing can take significant overhead, since compiler
implementors know it will be rare.



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 17:01 ` Larry Kilgallen
@ 2001-11-21 17:03   ` Preben Randhol
  2001-11-21 19:34   ` Ed Falis
  1 sibling, 0 replies; 16+ messages in thread
From: Preben Randhol @ 2001-11-21 17:03 UTC (permalink / raw)


On 21 Nov 2001 11:01:04 -0600, Larry Kilgallen wrote:

> Exception processing can take significant overhead, since compiler
> implementors know it will be rare.

Ah thanks didn't know that. :-)

Preben
-- 
 ()   Join the worldwide campaign to protect fundamental human rights.
'||}
{||'                                           http://www.amnesty.org/



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 15:17 Whether to raise exception or End_of_List function Preben Randhol
                   ` (2 preceding siblings ...)
  2001-11-21 17:01 ` Larry Kilgallen
@ 2001-11-21 17:04 ` chris.danx
  2001-11-21 17:09   ` chris.danx
  2001-11-21 22:00   ` Larry Kilgallen
  2001-11-21 17:11 ` Jeffrey Carter
  2001-11-21 17:53 ` Stephen Leake
  5 siblings, 2 replies; 16+ messages in thread
From: chris.danx @ 2001-11-21 17:04 UTC (permalink / raw)



"Preben Randhol" <randhol+abuse@pvv.org> wrote in message
news:slrn9vnh96.55u.randhol+abuse@kiuk0156.chembio.ntnu.no...
> Hi
>
> I have made a simple double linked list that I use in the application I
> develop. Before somebody suggest I use some other libraries I want to
> say that I made this double linked list to learn Ada 95 and get aware of
> different problems/aspects of implementing this, so at least for this
> application I want to keep it for now.
>
> The list code can be viewed here: http://www.pvv.org/~randhol/Ada95/List/
> Note that the code is still rough, I haven't removed some debugging
> functions/procedures etc...
>
> I put this into a package:
>
>    package Examine_List is
>          new Double_Linked_List (Data_Type => Natural);
>
>    Main_Examine : Examine_List.List_Type := Examine_List.New_List;
>
> And use the Main_Examine. So far it has worked nicely, but now I came to
> that at one place (at least) in my application I have to go from the
> current position of the iterator until the end of the list. I cannot
> know the current position of the list iterator so I cannot use a
> for-loop. If I want to use a while loop however there are the problem of
> knowing when the end of the list is reached. I guess I can make a
> function End_of_List (which must also work if one try to backwards in
> the list) or I can just raise an exception when the user tries to
> step off the list.
>
> My question is which is the better solution; Exception or a End_of_List
> function and which would have fewer side effects?

Have both!  i.e. an End_Of_List function for when you need to know when
you've reached the end of a list, and an exception for when you fall of the
list and attempt to use it.

I have a similar problem with a recent assignment, and I found both to be
helpful (exceptions are for getting the bugs out, and knowing where they are
(although not all bugs are detectable by exceptions)).  An exception will be
generated anyway if your iterator attempts to use a null valued access type
(assuming that's how the implementation works), but you may want your own
exception -- it can be helpful sometimes.



Chris




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 17:04 ` chris.danx
@ 2001-11-21 17:09   ` chris.danx
  2001-11-21 22:00   ` Larry Kilgallen
  1 sibling, 0 replies; 16+ messages in thread
From: chris.danx @ 2001-11-21 17:09 UTC (permalink / raw)



> > My question is which is the better solution; Exception or a End_of_List
> > function and which would have fewer side effects?
>
> Have both!  i.e. an End_Of_List function for when you need to know when
> you've reached the end of a list, and an exception for when you fall of
the
> list and attempt to use it.
>
> I have a similar problem with a recent assignment, and I found both to be
> helpful (exceptions are for getting the bugs out, and knowing where they
are
> (although not all bugs are detectable by exceptions)).  An exception will
be
> generated anyway if your iterator attempts to use a null valued access
type
> (assuming that's how the implementation works), but you may want your own
> exception -- it can be helpful sometimes.

I wasn't clear there.  Exceptions are for catching bugs and unexpected
conditions, and *if possible* (and desirable) recover from them.  If not
then they are helpful when you need to fix a program.

Thought it best to be clear on that,
Chris


> Chris
>




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 15:17 Whether to raise exception or End_of_List function Preben Randhol
                   ` (3 preceding siblings ...)
  2001-11-21 17:04 ` chris.danx
@ 2001-11-21 17:11 ` Jeffrey Carter
  2001-11-21 17:53 ` Stephen Leake
  5 siblings, 0 replies; 16+ messages in thread
From: Jeffrey Carter @ 2001-11-21 17:11 UTC (permalink / raw)


Preben Randhol wrote:
> 
[about advancing a position in a list until the end of the list is
reached]
> My question is which is the better solution; Exception or a End_of_List
> function and which would have fewer side effects?

From the component creation point of view, the approach taken by most
industrial-strength components is to provide a function so the client
can avoid doing something meaningless, such as trying to look at the
item referenced by an invalid position, but to also define and raise an
exception if the client ignores or doesn't use the function.

From the client point of view, it's probably best to use the function.

-- 
Jeffrey Carter



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 15:17 Whether to raise exception or End_of_List function Preben Randhol
                   ` (4 preceding siblings ...)
  2001-11-21 17:11 ` Jeffrey Carter
@ 2001-11-21 17:53 ` Stephen Leake
  2001-11-21 18:51   ` Preben Randhol
  5 siblings, 1 reply; 16+ messages in thread
From: Stephen Leake @ 2001-11-21 17:53 UTC (permalink / raw)


Preben Randhol <randhol+abuse@pvv.org> writes:

><snip>
> 
> My question is which is the better solution; Exception or a End_of_List 
> function and which would have fewer side effects?

You should always provide a function that allows a user to determine
whether an exception would be raised, for _all_ operations in your
library.

Some people like to just let the exception be raised, others prefer to
check for it. Some coding guidelines _forbid_ the raising of
exceptions, so checking routines are required.

A library should support both styles of coding, so it should provide
both meaningful exceptions (not just Constraint_Error), and checking
functions. 

Side effects can be an issue with some subprograms. If a subprogram
has 'out' parameters, it is implementation-dependent whether they are
set when an exception is raised. That's an additional reason to
provide a checking routine, but it's not usually very important.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 17:53 ` Stephen Leake
@ 2001-11-21 18:51   ` Preben Randhol
  2001-11-22  3:53     ` Nick Roberts
  0 siblings, 1 reply; 16+ messages in thread
From: Preben Randhol @ 2001-11-21 18:51 UTC (permalink / raw)


On 21 Nov 2001 12:53:06 -0500, Stephen Leake wrote:
> You should always provide a function that allows a user to determine
> whether an exception would be raised, for _all_ operations in your
> library.

I saw now (that I started to think about possible problems that can
arise) that my function Goto_Node_Id can fail if a wrong Id is given. I
guess it is better to turn it into a function and return false if the
Node_id cannot be found.

> Some people like to just let the exception be raised, others prefer to
> check for it. 

I have noticed that some libraries only raises a lot of exceptions and
leave it at that. That was the reason for my questions. As I also read
that exceptions can be a monstrosity if over used.

Thanks

Preben
-- 
 ()   Join the worldwide campaign to protect fundamental human rights.
'||}
{||'                                           http://www.amnesty.org/



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 17:01 ` Larry Kilgallen
  2001-11-21 17:03   ` Preben Randhol
@ 2001-11-21 19:34   ` Ed Falis
  1 sibling, 0 replies; 16+ messages in thread
From: Ed Falis @ 2001-11-21 19:34 UTC (permalink / raw)


Larry Kilgallen wrote:
> 
> In article <slrn9vnh96.55u.randhol+abuse@kiuk0156.chembio.ntnu.no>, Preben Randhol <randhol+abuse@pvv.org> writes:
> 
> > My question is which is the better solution; Exception or a End_of_List
> > function and which would have fewer side effects?
> 
> Exceptions are supposed to be rare events.  In many applications
> reaching the end of a list is fully expected and not necessarily
> rare.
> 
> Exception processing can take significant overhead, since compiler
> implementors know it will be rare.

Personally, I'm inclined to follow the Eiffel DbC style by stating the
conditions for calling any routine, and those that are guaranteed on
return, rather than using exception processing as part of the normal
execution of the routine.  By using pragma Assert (available at least on
GNAT and the Averstar-based compilers), you can enable the assertions
during development, then turn them off for production.

- Ed



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 17:04 ` chris.danx
  2001-11-21 17:09   ` chris.danx
@ 2001-11-21 22:00   ` Larry Kilgallen
  1 sibling, 0 replies; 16+ messages in thread
From: Larry Kilgallen @ 2001-11-21 22:00 UTC (permalink / raw)


In article <68RK7.3295$AQ3.467408@news6-win.server.ntlworld.com>, "chris.danx" <chris.danx@ntlworld.com> writes:

>> My question is which is the better solution; Exception or a End_of_List
>> function and which would have fewer side effects?
> 
> Have both!  i.e. an End_Of_List function for when you need to know when
> you've reached the end of a list, and an exception for when you fall of the
> list and attempt to use it.

"Have both" is exactly equivalent to "have a function", since if there
is a function and the caller ignores it _some_ action must be taken.
Raising an exception in such circumstances is much more in the Ada
tradition than returning a random entry from the middle of the list :-)



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-21 18:51   ` Preben Randhol
@ 2001-11-22  3:53     ` Nick Roberts
  2001-11-22 10:23       ` Preben Randhol
  2001-11-23 14:37       ` Preben Randhol
  0 siblings, 2 replies; 16+ messages in thread
From: Nick Roberts @ 2001-11-22  3:53 UTC (permalink / raw)


"Preben Randhol" <randhol+abuse@pvv.org> wrote in message
news:slrn9vntpg.7nm.randhol+abuse@kiuk0156.chembio.ntnu.no...
> ...
> As I also read
> that exceptions can be a monstrosity if over used.

And sometimes a piece of code can become a monstrosity (in terms of a
proliferation of if-then-elses or whatever) if exceptions are underused.
Indeed, sometimes the neophyte attempting to avoid using the humble 'goto'
can end up in a similar mess.

When should one use exceptions? Exceptionally. The clue is in the name ;-)

--
Best wishes,
Nick Roberts



Let me provide a bit of an example. Supposing we are reading a sequence of
data items from a device. Version A:

   procedure Read_Data is
   begin
      if Status_Bit(Port(1047)) /= 1 then
         Put(Current_Error,"Could not read altitude first byte");
      else
         Read(Port(1050),Alt_Bytes(1));
         if Status_Bit(Port(1047)) /= 1 then
            Put(Current_Error,"Could not read altitude second byte");
         else
            Read(Port(1050),Alt_Bytes(2));
            if Status_Bit(Port(1047)) /= 1 then
               ...

and so on until you run off the right margin!

Version B:

   Read_Port_Error: exception;

   procedure Read_Byte (Byte: out Interfaces.Unsigned_8) is
   begin
      if Status_Bit(Port(1047)) /= 1 then
         raise Read_Port_Error;
      end if;
      Read(Port(1050),Byte);
   end;

   procedue Read_Data is
   begin
      Read_Byte(Alt_Bytes(1));
      Read_Byte(Alt_Bytes(2));
      ...

Much better, eh? If more detailed diagnostics are required, one can always
use Ada.Exceptions and provide extra information in the Message parameter
when calling the Raise_Exception procedure.







^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-22  3:53     ` Nick Roberts
@ 2001-11-22 10:23       ` Preben Randhol
  2001-11-23 14:37       ` Preben Randhol
  1 sibling, 0 replies; 16+ messages in thread
From: Preben Randhol @ 2001-11-22 10:23 UTC (permalink / raw)


On Thu, 22 Nov 2001 03:53:15 -0000, Nick Roberts wrote:
> "Preben Randhol" <randhol+abuse@pvv.org> wrote in message
> news:slrn9vntpg.7nm.randhol+abuse@kiuk0156.chembio.ntnu.no...
>> ...
>> As I also read
>> that exceptions can be a monstrosity if over used.
> 
> And sometimes a piece of code can become a monstrosity (in terms of a
> proliferation of if-then-elses or whatever) if exceptions are underused.
> Indeed, sometimes the neophyte attempting to avoid using the humble 'goto'
> can end up in a similar mess.

Very true.

> When should one use exceptions? Exceptionally. The clue is in the name ;-)

Yes :-)

[snipped good example]

> Much better, eh? If more detailed diagnostics are required, one can always
> use Ada.Exceptions and provide extra information in the Message parameter
> when calling the Raise_Exception procedure.

Yes think that is smart so that it is easier to locate the problem.

Preben
-- 
 ()   Join the worldwide campaign to protect fundamental human rights.
'||}
{||'                                           http://www.amnesty.org/



^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Whether to raise exception or End_of_List function
  2001-11-22  3:53     ` Nick Roberts
  2001-11-22 10:23       ` Preben Randhol
@ 2001-11-23 14:37       ` Preben Randhol
  1 sibling, 0 replies; 16+ messages in thread
From: Preben Randhol @ 2001-11-23 14:37 UTC (permalink / raw)


On Thu, 22 Nov 2001 03:53:15 -0000, Nick Roberts wrote:
> Much better, eh? If more detailed diagnostics are required, one can always
> use Ada.Exceptions and provide extra information in the Message parameter
> when calling the Raise_Exception procedure.

Actually Raise_Exception isn't necessarily needed as I see now. If I use it
my exception looks like this:

   Exception name: LISTS.WORDGROUPS_LIST.OUT_OF_LIST_BOUNDARY
   Message: End of List already reached. No next Node exists.

but if I simply use raise:

   Exception name: LISTS.WORDGROUPS_LIST.OUT_OF_LIST_BOUNDARY
   Message: double_linked_list.adb:80 instantiated at lists.ads:6

but in other circumstances it makes sense as long as one do not use the
same message at all the places where the exception can be raised.

Preben
-- 
 ()   Join the worldwide campaign to protect fundamental human rights.
'||}
{||'                                           http://www.amnesty.org/



^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2001-11-23 14:37 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-21 15:17 Whether to raise exception or End_of_List function Preben Randhol
2001-11-21 16:32 ` Preben Randhol
2001-11-21 16:51 ` Mark Biggar
2001-11-21 17:00   ` Preben Randhol
2001-11-21 17:01 ` Larry Kilgallen
2001-11-21 17:03   ` Preben Randhol
2001-11-21 19:34   ` Ed Falis
2001-11-21 17:04 ` chris.danx
2001-11-21 17:09   ` chris.danx
2001-11-21 22:00   ` Larry Kilgallen
2001-11-21 17:11 ` Jeffrey Carter
2001-11-21 17:53 ` Stephen Leake
2001-11-21 18:51   ` Preben Randhol
2001-11-22  3:53     ` Nick Roberts
2001-11-22 10:23       ` Preben Randhol
2001-11-23 14:37       ` Preben Randhol

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox