comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <Nick.Roberts@dial.pipex.com>
Subject: Re: How to write TYPECASE in Ada 95?
Date: 1999/02/19
Date: 1999-02-19T00:00:00+00:00	[thread overview]
Message-ID: <7aikfm$nhq$2@plug.news.pipex.net> (raw)
In-Reply-To: 7ai3m2$4t0$1@nnrp1.dejanews.com

robert_dewar@my-dejanews.com wrote in message
<7ai3m2$4t0$1@nnrp1.dejanews.com>...
|tmoran@bix.com (Tom Moran) writes:
|> Dispatching is certainly better than a case statement -
|> if you use it.
|
|I strongly disagree with this statement. This particular
|bit of conventional wisdom is badly in error, and can lead
|to unnecessarily obscure hard to maintain programs.
|
|Case statements and dispatching represent two ways of
|slicing a 2-D array of types vs operations. The use of
|case statements may make it easier to add operations, the
|use of dispatching may make it easier to add types. Which
|is better depends on the particular situation.


I believe I illustrated in a previous post the reason why using dispatching
is sometimes 'overkill'.

To recap briefly, imagine a large tree of types: T; Ta; Tb; Taa; Tab; Tba;
Tbb; Taaa; Taab; Taba; Tabb; and so on. T is at the top. Ta and Tb are
derived from T. Taa and Tab are derived from Ta. You get the picture.

Now imagine a procedure P(X: in out T'Class) which contains a large number
of lines of processing, and which has a slight variation in the middle just
for the types Tabaab and Tbbaba, say. It only needs an 'if' or 'case', in
the appropriate place, to distinguish these types and do the special actions
for them.

Now consider, instead, having P(X: in out T), and then having to overload
this procedure for each and every type Ta, Tb, Taa, etc., etc.. Lots of
types times many lines of code, most of which are all the same. Disastrous.
Even if the fixed parts are factored out, you still have to declare lots of
overloadings; it's just not worth it!

Finally, consider a procedure Q(X: in out T) which performs an operation
that has a substantially different implementation for each and every
different type. Dispatching for procedure Q makes a lot of sense.

Dispatching is very often an excellent approach. But, it is definitely not
_always_ the best solution!

-------------------------------------
Nick Roberts

'The time has come,' the Walrus said,
  'To talk of many things:
Of shoes--of ships--and sealing wax--
  Of cabbages--and kings--
And why the sea is boiling hot--
  And whether pigs have wings.'
                        Lewis Carroll
          "Through the Looking Glass"
-------------------------------------








  parent reply	other threads:[~1999-02-19  0:00 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-05  0:00 How to write TYPECASE in Ada 95? Norman Ramsey
1999-02-05  0:00 ` Brian Rogoff
1999-02-05  0:00   ` David C. Hoos, Sr.
1999-02-05  0:00     ` Brian Rogoff
1999-02-06  0:00   ` Ed Falis
1999-02-06  0:00     ` Nick Roberts
1999-02-06  0:00       ` Nick Roberts
1999-02-17  0:00     ` Tom Moran
1999-02-18  0:00       ` Matthew Heaney
1999-02-18  0:00         ` robert_dewar
1999-02-19  0:00           ` Tom Moran
1999-02-19  0:00           ` Nick Roberts [this message]
1999-02-18  0:00         ` Tom Moran
1999-02-18  0:00         ` Tom Moran
1999-02-18  0:00           ` Matthew Heaney
1999-02-19  0:00     ` Tom Moran
1999-02-19  0:00       ` Tom Moran
1999-02-23  0:00       ` Samuel Mize
1999-02-23  0:00         ` Question (was Re: How to write TYPECASE in Ada 95?) Mike Silva
1999-02-24  0:00           ` Nick Roberts
1999-02-24  0:00           ` (long) programming by extension (was: " Samuel Mize
1999-02-24  0:00             ` (long) programming by extension Samuel Mize
1999-02-25  0:00               ` (shorter and new) " Samuel Mize
1999-02-25  0:00                 ` Mike Silva
1999-02-26  0:00                   ` Samuel Mize
1999-02-24  0:00           ` Question (was Re: How to write TYPECASE in Ada 95?) Samuel T. Harris
1999-02-24  0:00             ` Matthew Heaney
1999-02-24  0:00               ` Tucker Taft
1999-02-06  0:00 ` How to write TYPECASE in Ada 95? Matthew Heaney
1999-02-06  0:00 ` David C. Hoos, Sr.
1999-02-06  0:00   ` Matthew Heaney
1999-02-06  0:00     ` Matthew Heaney
1999-02-06  0:00     ` Matthew Heaney
1999-02-09  0:00     ` David C. Hoos, Sr.
replies disabled

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