comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: on the need to duplicate code for procedure signature in both body   and interface files
Date: Sun, 08 Jul 2012 07:21:07 +0300
Date: 2012-07-08T07:21:07+03:00	[thread overview]
Message-ID: <a5sg5kF4q7U1@mid.individual.net> (raw)
In-Reply-To: <jtafi8$pvg$1@speranza.aioe.org>

On 12-07-08 02:08 , Nasser M. Abbasi wrote:
> Basic question from an Ada newbie.
>
> One thing that always bothered me in Ada is the need
> to physically duplicate the code that represents the API
> of the package procedures and functions in both the interface
> and the body files (.ads and .adb).

That is a language property that applies to the source-code files. If 
you use some kind of CASE tool or IDE, it can hide the duplication by 
synchronizing the text in the spec and body. For exampe, I think 
GPS/gnat has the ability to generate a dummy body (.adb) from a given 
spec (.ads) (but not to maintain the connection if the spec changes). 
Possibly the Ada mode of emacs can do that too.

Several years ago I used an Ada CASE tool called HoodNICE, based on the 
HOOD design method. In this tool, one wrote the Ada code once, and the 
tool generated the spec and body files. However, the design method was 
based on packages as "objects", and dependencies were basically managed 
on the object level *without* separation of spec and body, so one had to 
take some special ugly action to show that "object" A depended on 
"object" B only because the *body* of package A needed a "with" clause 
for package B, while the *spec* of A did *not* need such a clause.

(To be fair, the HOOD design method tried to enforce a strict layering 
with no mutual dependencies through bodies. But this is a very limiting 
rule, and we violated it in most projects.)

> I do not like duplicating code at all. Even if it is only
> for the signature of the API, and even though the Ada
> compiler will catch any difference (assuming one changes
> the .ads and forget to update the .adb for example).

The important reason for "duplicating" the code (signature) is that one 
needs some way to show which body belongs to which declaration when the 
subprogram names are overloaded (same name, different signature).

Duplicating the text that defines the signature is IMO the simplest and 
most readable way to show this connection, when the declaration and body 
are in different files. in a sense, there is no more "duplication" here 
than when we "duplicate" the subprogram name and parameter names in the 
calls to the subprograms.

Keeping the spec and body in the same file would mess up "make" logic 
based on file time-stamps. Changing the body code should normally not 
force recompilation of clients of the spec.

> Was there no other alternatives to avoid this situation
> when Ada was originally designed in order to keep the same
> good concept of separating the interface from the body, but
> somehow at the same time, eliminate the need to duplicate by
> hand the API definition code in 2 separate places?

Given the copy/paste abilities of good text editors, I don't see this as 
a problem at all. In my spec files, most of the text is comments (ooh 
how I hate that misleading word!) that describe what the stuff means and 
how it works; the actual Ada code is a small fraction, and of this code, 
only the subprogram signatures are duplicated in the body. (Of course 
there are comments in the body, too, but on different issues.)

No doubt one could devise an Ada compilation system, similar to the 
HoodNICE method, in which only body files exist, with some mark-up to 
show which parts of these files are "public" (belong in the spec). A 
dedicated tool could then generate the spec files, and also avoid 
updating the time-stamp of the spec when the contents of the file do not 
change. But as I said, I don't feel that this (apparent) duplication is 
a problem.

> I am using GNAT. I Assume this is the same in other Ada
> implementations.

I wonder how the Rational Ada enviroment worked, on this issue. I'm sure 
someone on this group remembers.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  parent reply	other threads:[~2012-07-08  4:21 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-07 23:08 on the need to duplicate code for procedure signature in both body and interface files Nasser M. Abbasi
2012-07-08  4:01 ` Shark8
2012-07-08 17:13   ` Brian Drummond
2012-07-08  4:21 ` Niklas Holsti [this message]
2012-07-08  6:09 ` Jeffrey Carter
2012-07-13 15:51   ` Robert A Duff
2012-07-08  7:38 ` Stephen Leake
2012-07-08 10:52 ` Manuel Collado
2012-07-09  1:48 ` Britt
2012-07-09 10:49   ` AdaMagica
2012-07-09 15:11 ` Adam Beneschan
2012-07-13 17:12 ` björn lundin
replies disabled

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