comp.lang.ada
 help / color / mirror / Atom feed
From: "Warren W. Gay VE3WWG" <ve3wwg@cogeco.ca>
Subject: Re: Good Ada Style For Impossible Actions?
Date: Mon, 28 Oct 2002 12:52:20 -0500
Date: 2002-10-28T12:52:20-05:00	[thread overview]
Message-ID: <3DBD7954.80502@cogeco.ca> (raw)
In-Reply-To: 3DBD7066.1040205@acm.org

Jeffrey Carter wrote:
> Dr Nancy's Sweetie wrote:
> 
>> Jeffrey Carter wrote, in reply to a question about when to use exceptions
>> and functions for testing error conditions:
>>
>>> It sounds as if you could have mouse events outside the proper window
>>> at any time, so such an event would not be exceptional. So having a
>>> function would probably be a good idea.
>>>
>>> But if you use the function and still get a mouse event outside the
>>> proper window, you should also have an exception to indicate that.
>>
>> If the function tries to compute a logical address for an invalid
>> mouse position, a CONSTRAINT_ERROR will be raised because the logical
>> position will be outside its valid range.
>>
>> Is there a good heuristic for "raise your own exception" and "don't
>> clutter up the exception namespace"?
> 
> The function returns Boolean: True if the mouse pointer is in the active 
> window; False otherwise. So it should catch the exception and return False.
> 
> The other subprogram returns the mouse position within the active 
> window, and should only be called if the function mentioned above 
> returns True. But of course people will ignore that rule, and in any 
> case it may be possible for the mouse pointer to move between the 2 
> calls. If the mouse pointer is outside the active window when this 
> subprogram is called, then it should raise a specific exception to 
> indicate this. Constraint_Error is not very informative in this context.

I'd suggest that your function (or procedure) return
a discriminated record instead:

type Window_Status_Kind is ( Not_In_Window, In_Window );

type Return_Type(Kind : Window_Status_Kind) is
    record
       case Kind in
          when Not_In_Window =>
             null; -- no other information available
          when In_Window =>
             Y :      Line_Type;    -- Translated window Y coordinate
             X :      Column_Type;  -- Translated window X coordinate
       end case;
    end record;

The nice thing about this approach is that if your code attempts to
access Y or X from the returned value when it is inappropriate to do
so, the code will fail immediately, at the source of error.  Returning
the Y,X separately from a Boolean flag (say), leaves you open to
abusing Y,X when they are not valid.

I agree that an exception is probably overkill for something that is
likely to occur with reasonable frequency.

Warren.
-- 
Warren W. Gay VE3WWG
http://home.cogeco.ca/~ve3wwg




      parent reply	other threads:[~2002-10-28 17:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-26 15:59 Good Ada Style For Impossible Actions? Dr Nancy's Sweetie
2002-10-26 21:11 ` Jeffrey Carter
2002-10-28  3:52   ` Dr Nancy's Sweetie
2002-10-28 17:13     ` Jeffrey Carter
2002-10-28 17:33       ` tmoran
2002-10-29  1:35         ` Dr Nancy's Sweetie
2002-10-28 17:52       ` Warren W. Gay VE3WWG [this message]
replies disabled

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