comp.lang.ada
 help / color / mirror / Atom feed
From: "Alan and Carmel Brain" <aebrain@webone.com.au>
Subject: Avoiding Kludges
Date: Wed, 30 Oct 2002 22:45:28 +1100
Date: 2002-10-30T11:32:44+00:00	[thread overview]
Message-ID: <3dbfc357$2@iridium.webone.com.au> (raw)

"Dr Nancy's Sweetie" <kilroy@elvis.rowan.edu> wrote

> At this point, several alternative approaches occurred to me.  The first
is
> to have the function return an illegal logical location -- made difficult
> because the "Location_Type" is range limited to only legal possibilities.
> It could be kludged to have a "Any_Location_Type", which would include
some
> impossible spots, and "Real_Location_Type", which would be only legal
> spots, and then cast things around, and then . . .  Well, it's starting to
> sound pretty kludgy

Indeed it is.

> Then I realised that this could also be done with an exception:

But it's not an unusual or erroneous situation. Bad Form.

> But of these last two approaches, I don't have a feeling for which one is
> Good Ada Style(tm).

Neither: Good Ada Style , valid for -83 and -95 is to return
something of this type

<code>

type Mouse_Location_Type( InWindow : boolean := false) is
record
  case InWindow is
    when true  => CoOrdinate : Location_Type;
    when false => null;
  end case;
end record;

</code>

When referencing something of this type, you'll have to do an
explicit test to see if CoOrdinate exists. If doing a test on
the co-ordinates, the "and then" clause is useful.

Example:

<code>

-- Get the mouse location and see if it's within normal bounds.
begin

  MouseLocation := GetLocation;
  if MouseLocation.InWindow             and then
     InBounds(MouseLocation.CoOrdinate) then
     -- do stuff
  else
     -- do other stuff.
  end if;

exception
   when others => raise; -- No expected exceptions,
                         -- and we wouldn't know what to do with one
                         -- if we get one. So kick it upstairs to
                         -- a higher authority.
end;

</code>

> My own inclination is to write two functions and use an if, but I thought
> it might be worthwhile to ask people with a better sense of the style.

My qualifications as a Style Guru:

I've got a dozen successful Ada-83 projects under my belt over nearly 20
years, and was in charge of the Ada-95 spaceflight avionics for FedSat, due
for
launch in December. The style above is what I'd recommend for practical use
in large projects.

Note that I make no claim to infallibility.

Regards, AEB



--
Alan & Carmel Brain
mailto:aebrain@webone.com.au





                 reply	other threads:[~2002-10-30 11:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
replies disabled

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