comp.lang.ada
 help / color / mirror / Atom feed
From: Justin Gombos <rpbkbq.xax.gld@uluv.kbq>
Subject: Re: Handling invalid objects
Date: Sat, 18 Mar 2006 16:10:59 GMT
Date: 2006-03-18T16:10:59+00:00	[thread overview]
Message-ID: <niWSf.1278$I7.339@trnddc03> (raw)
In-Reply-To: 7LOSf.34253$oL.29742@attbi_s71

On 2006-03-18, Jeffrey R. Carter <spam.not.jrcarter@acm.not.spam.org> wrote:
>
> There's a basic SW-engineering principle that one thing should have
> one meaning.  Using one thing with 2 meanings, which is what you're
> asking for, clearly violates that principle.

That's a good point.  Given that perspective, would you say the
practice of using null access values to indicate whether the access
value is useable violates this principle?

>>   type clock_type is mod 12;
>> 
>>   function hour_of_day return clock_type;
>
> This seems to specify a function that is expected to always be able
> to return a valid value. 

Pretend that there are cases where the hour of day is unavailable.  I
chose an example using mod only because it has added complications
when increasing the range of values to allow for an invalid object,
and hour of day is simple.

> If it is expected to only be able to return a valid value some of
> the time, that should be documented, probably by naming the function
> something like Try_To_Get_Hour_Of_Day.

There's that principle of naming non-boolean functions nouns, since
they return an object.  Also, if an API is often queried for
information that it cannot retrieve, and often this is normal
operation, subprogram prefixes like "try_to_get_" seem lengthy.  I
would have to use some other means to indicate that the returned value
may be unusable.

> If the function should always be able to return a valid value, and
> it can't, that is clearly an exceptional situation, and an exception
> is clearly the correct way to deal with such a situation.

I recall working to a coding standard that barred exception raising
when other mechanisms for error detection are possible.  It
effectively banned exception raising altogether because you can always
send an error notice by other means.  Exceptions are synonymous with
goto statements, with the additional effect of creating an
unpredictable state so I didn't challenge the rule.

Exception raising and handling is often viewed as writing code that
will never execute.  Predefined exceptions must be handled when there
is an expectation they will be raised, but their usefullness stops
there.  Beyond that, it's like writing erroneous code on purpose.

> If valid values are not always possible, then it's a question of how
> often they are not available. If it's a fairly common occurrence
> (and that can be less than 50% of the time), then it's not an
> exceptional situation, and an unconstrained record type with a
> Boolean discriminant is probably the best thing to return.  If, on
> the other hand, it's a rare occurrence, then it's still an
> exceptional situation, and an exception is the correct solution.

I try to avoid variant records, but this sounds like a good use for
them.  I'll have to try that.

> Anyone who has seen C code that correctly checks and deals with all
> returned error codes and success flags knows how complicated and
> unreadable such code is.  The common practice in C is to not do the
> necessary checks so that the code is simpler and more readable. That
> is clearly not an acceptable solution.  Exceptions separate the
> normal processing from the exceptional processing, allowing simple
> and readable code for the normal case while ensuring that all checks
> are performed and dealt with.
>
> There are cases where exceptions are outlawed; most of them are not
> justified.  Even if they were all justified, advocating a return to
> the mess that error codes create is not a viable option.

It's a mess regardless.  Clearly Ada is deficient in this respect, as
all languages seem to be.

-- 
PM instructions: do a C4esar Ciph3r on my address; retain punctuation.



  reply	other threads:[~2006-03-18 16:10 UTC|newest]

Thread overview: 91+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-13 19:58 private types ada_student
2006-03-13 20:27 ` Mark Lorenzen
2006-03-13 21:05   ` Pascal Obry
2006-03-13 21:07   ` ada_student
2006-03-13 21:45     ` Simon Wright
2006-03-14  4:51 ` Jeffrey R. Carter
2006-03-14  7:44   ` Brian May
2006-03-14  8:25     ` Ludovic Brenta
2006-03-14  8:47     ` Alex R. Mosteo
2006-03-17  4:33     ` Justin Gombos
2006-03-17  5:17       ` Brian May
2006-03-17 22:50         ` Justin Gombos
2006-03-18  1:17         ` Randy Brukardt
2006-03-18  2:17           ` Justin Gombos
2006-03-21  0:08             ` Randy Brukardt
2006-03-18  8:39           ` Uninitialized variables (was: Re: private types) Dirk Craeynest
2006-03-18 14:06             ` Gautier
2006-03-18 14:36               ` Uninitialized variables Jeffrey Creem
2006-03-21  0:22             ` Uninitialized variables (was: Re: private types) Randy Brukardt
2006-03-21  0:38             ` Randy Brukardt
2006-03-18 12:06           ` private types Martin Dowie
2006-03-18 12:47           ` Robert A Duff
2006-03-17  7:40       ` Maciej Sobczak
2006-03-17 16:41         ` Frank J. Lhota
2006-03-17 23:36         ` Justin Gombos
2006-03-18  1:32           ` Randy Brukardt
2006-03-18  3:21             ` Handling invalid objects Justin Gombos
2006-03-18  7:35               ` Jeffrey R. Carter
2006-03-18 16:10                 ` Justin Gombos [this message]
2006-03-19 11:00                   ` Simon Wright
2006-03-20 23:57                   ` Randy Brukardt
2006-03-22  2:06                     ` Justin Gombos
2006-03-22  5:23                       ` tmoran
2006-03-22  8:48                         ` Dmitry A. Kazakov
2006-03-22  9:24                           ` Maciej Sobczak
2006-03-22 11:05                             ` Dmitry A. Kazakov
2006-03-22 16:42                               ` Maciej Sobczak
2006-03-22 18:06                                 ` Stefan Lucks
2006-03-23 13:20                                 ` Dmitry A. Kazakov
2006-03-18  8:57               ` Jacob Sparre Andersen
2006-03-19 19:07                 ` Dr. Adrian Wrigley
2006-03-20 15:25                   ` Robert A Duff
2006-03-19 22:06               ` Brian May
2006-03-20 21:17                 ` Jeffrey R. Carter
2006-03-20 23:44               ` Randy Brukardt
2006-03-22  1:27                 ` Justin Gombos
2006-03-18  9:20           ` private types Dmitry A. Kazakov
2006-03-17 13:18       ` Robert A Duff
2006-03-17 23:44         ` Justin Gombos
2006-03-18  9:24           ` Dmitry A. Kazakov
2006-03-18 12:56           ` Robert A Duff
2006-03-18 15:06             ` Justin Gombos
2006-03-19  9:35               ` Martin Krischik
2006-03-19 14:52                 ` Peter C. Chapin
2006-03-19 15:08                   ` Björn Persson
2006-03-19 16:34                     ` Martin Krischik
2006-03-20  9:57                       ` Maciej Sobczak
2006-03-20 10:58                         ` Peter C. Chapin
2006-03-20 11:19                           ` Peter C. Chapin
2006-03-20 13:06                           ` Maciej Sobczak
2006-03-20 15:19                         ` Robert A Duff
2006-03-20 16:47                           ` James Dennett
2006-03-20 19:12                         ` Martin Krischik
2006-03-21  7:27                           ` Maciej Sobczak
2006-03-20 19:32                         ` Martin Krischik
2006-03-21  7:41                           ` Maciej Sobczak
2006-03-20 20:29                       ` Simon Wright
2006-03-19 17:43                     ` Larry Kilgallen
2006-03-19 22:11                     ` Peter C. Chapin
2006-03-19 18:15                 ` Robert A Duff
2006-03-19 19:20                   ` Martin Krischik
2006-03-19 20:43                     ` Dr. Adrian Wrigley
2006-03-20 15:01                       ` Robert A Duff
2006-03-27  4:07                       ` Dave Thompson
2006-03-20  9:40                     ` Maciej Sobczak
2006-03-20 15:09                       ` Robert A Duff
2006-03-21  8:07                         ` Maciej Sobczak
2006-03-26 18:53                           ` Robert A Duff
2006-03-19 19:27                 ` Jeffrey R. Carter
2006-03-25 21:40               ` Robert A Duff
2006-03-26  0:10                 ` Justin Gombos
2006-03-26  1:00                   ` Robert A Duff
2006-03-26  6:37                     ` Jeffrey R. Carter
2006-03-26 15:43                       ` Justin Gombos
2006-03-26 16:32                         ` Robert A Duff
2006-03-26 16:51                       ` Robert A Duff
2006-03-26 19:41                         ` Jeffrey R. Carter
2006-03-26  3:15                 ` Frank J. Lhota
2006-03-26 18:28                   ` Robert A Duff
2006-03-26 19:43                     ` Jeffrey R. Carter
2006-03-26 19:59                     ` Simon Wright
replies disabled

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