comp.lang.ada
 help / color / mirror / Atom feed
* on the need to duplicate code for procedure signature in both body and interface files
@ 2012-07-07 23:08 Nasser M. Abbasi
  2012-07-08  4:01 ` Shark8
                   ` (7 more replies)
  0 siblings, 8 replies; 12+ messages in thread
From: Nasser M. Abbasi @ 2012-07-07 23:08 UTC (permalink / raw)


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).

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).

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?

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

thanks,
--Nasser




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  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
                   ` (6 subsequent siblings)
  7 siblings, 1 reply; 12+ messages in thread
From: Shark8 @ 2012-07-08  4:01 UTC (permalink / raw)
  Cc: nma

On Saturday, July 7, 2012 5:08:53 PM UTC-6, 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).
> 
> 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).
> 
> 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?
> 
> I am using GNAT. I Assume this is the same in other Ada
> implementations.
> 
> thanks,
> --Nasser

There is no real way to do clean separate compilation without something like the spec/body separation (though it may be that everything code-wise is the "body" and the "spec" is virtual/internal).

My suggestion here is to let the compiler point out your errors. Unlike C/C++ an Ada compiler is your friend. There might be an adb -> ads generator laying around where you can just feed it an adb file and have it spit out the ads-file, but I've never had reason to look for one.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body   and interface files
  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  4:21 ` Niklas Holsti
  2012-07-08  6:09 ` Jeffrey Carter
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Niklas Holsti @ 2012-07-08  4:21 UTC (permalink / raw)


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
       .      @       .



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  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  4:21 ` Niklas Holsti
@ 2012-07-08  6:09 ` Jeffrey Carter
  2012-07-13 15:51   ` Robert A Duff
  2012-07-08  7:38 ` Stephen Leake
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 12+ messages in thread
From: Jeffrey Carter @ 2012-07-08  6:09 UTC (permalink / raw)


On 07/07/2012 04:08 PM, 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).
>
> 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).

Consider the following:

package P is
    function F (I : Integer) return Integer;

    type Integer_List is array (Integer range <>) of Integer;

    function F return Integer_List;
end P;

How are you going to write the body of P so the compiler knows which body 
implements which function F without repeating the parameter list and return type 
profile, which is what distinguishes these 2 overloaded functions?

-- 
Jeff Carter
"This school was here before you came,
and it'll be here before you go."
Horse Feathers
48



--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-07 23:08 on the need to duplicate code for procedure signature in both body and interface files Nasser M. Abbasi
                   ` (2 preceding siblings ...)
  2012-07-08  6:09 ` Jeffrey Carter
@ 2012-07-08  7:38 ` Stephen Leake
  2012-07-08 10:52 ` Manuel Collado
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Stephen Leake @ 2012-07-08  7:38 UTC (permalink / raw)


"Nasser M. Abbasi" <nma@12000.org> writes:

> I do not like duplicating code at all. 

Why?

> 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).

That covers the main objection I'm aware of; that duplicated code
quickly becomes wrong.

Another objection might be that it takes "too much" time to write the
same code twice. One solution to that is to use a decent IDE (Emacs and
GPS will both copy the spec to the body the first time). Another
solution is to remember that reading is much more important than
writing.

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

Yes, it's in the language standard.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-07 23:08 on the need to duplicate code for procedure signature in both body and interface files Nasser M. Abbasi
                   ` (3 preceding siblings ...)
  2012-07-08  7:38 ` Stephen Leake
@ 2012-07-08 10:52 ` Manuel Collado
  2012-07-09  1:48 ` Britt
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Manuel Collado @ 2012-07-08 10:52 UTC (permalink / raw)


El 08/07/2012 1:08, Nasser M. Abbasi escribi�:
> 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).
>
> 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).

It seems you are looking for a Java way of writing code. Java uses a single 
source file for each compilation unit. Signatures of public elements are 
implicitly assumed to be the interface part, while the remaining code gives 
the implementation.

>
> 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?

As said in other responses, the obvious solution would be a specialized IDE 
that lets you edit a single combined spec+body source, and later split it 
on the fly into spec and body separate files for compilation.

But such IDE seems not to exist yet.

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

Yes.

-- 
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-08  4:01 ` Shark8
@ 2012-07-08 17:13   ` Brian Drummond
  0 siblings, 0 replies; 12+ messages in thread
From: Brian Drummond @ 2012-07-08 17:13 UTC (permalink / raw)


On Sat, 07 Jul 2012 21:01:30 -0700, Shark8 wrote:

> On Saturday, July 7, 2012 5:08:53 PM UTC-6, 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).

> My suggestion here is to let the compiler point out your errors. Unlike
> C/C++ an Ada compiler is your friend. There might be an adb -> ads
> generator laying around where you can just feed it an adb file and have
> it spit out the ads-file, but I've never had reason to look for one.

And the best it could do would be a first pass at the ADS-file. Without 
further information it would expose a lot of procedures properly kept 
internal to the implementation (thus the ADB file). You would have to 
delete the rest yourself, or add annotations in the ADB file (heh, how 
about "static" declarations :-)

- Brian

- Brian



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-07 23:08 on the need to duplicate code for procedure signature in both body and interface files Nasser M. Abbasi
                   ` (4 preceding siblings ...)
  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
  7 siblings, 1 reply; 12+ messages in thread
From: Britt @ 2012-07-09  1:48 UTC (permalink / raw)


On Jul 7, 7:08 pm, "Nasser M. Abbasi" <n...@12000.org> 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).
>
> 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).
>
> 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?
>
> I am using GNAT. I Assume this is the same in other Ada
> implementations.
>
> thanks,
> --Nasser

This topic was recently discussed in the "Ada Programming Language"
LinkedIn group in a thread titled "Imaginary proposal for the next Ada
standard: Ada compilers will automatically generate Package
Specification from Package Body". The discussion started as a poll and
most respondents (including me) strongly dislike the idea.

Edward Colbert just posted a nice summary there.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-09  1:48 ` Britt
@ 2012-07-09 10:49   ` AdaMagica
  0 siblings, 0 replies; 12+ messages in thread
From: AdaMagica @ 2012-07-09 10:49 UTC (permalink / raw)


On Monday, July 9, 2012 3:48:09 AM UTC+2, Britt wrote:
> This topic was recently discussed in the "Ada Programming Language"
> LinkedIn group in a thread titled "Imaginary proposal for the next Ada
> standard: Ada compilers will automatically generate Package
> Specification from Package Body". The discussion started as a poll and
> most respondents (including me) strongly dislike the idea.
> 
> Edward Colbert just posted a nice summary there.

IMHO, this idea indicates a fundamental ignorance of the basics principles of Ada.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-07 23:08 on the need to duplicate code for procedure signature in both body and interface files Nasser M. Abbasi
                   ` (5 preceding siblings ...)
  2012-07-09  1:48 ` Britt
@ 2012-07-09 15:11 ` Adam Beneschan
  2012-07-13 17:12 ` björn lundin
  7 siblings, 0 replies; 12+ messages in thread
From: Adam Beneschan @ 2012-07-09 15:11 UTC (permalink / raw)


On Saturday, July 7, 2012 4:08:53 PM UTC-7, 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).
> 
> 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).
> 
> 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?

Overloading makes this an issue.  But I think that having procedure and function declarations duplicated in the body is a big help for readability.  If I'm looking at a procedure in the body, I can see what the declaration is.  In fact, one of the things I *don't* like about Ada is that when you write a generic procedure or function, you can't duplicate the generic part in the body.  I really like having that information available when I read the body, so that when I see a generic formal I know what it is without having to flip back to the specification.  Usually I just copy the generic formal part into the body and comment it out.

Anyway, I wrote a program a long time ago that takes text input with procedure and function declarations and provides empty bodies for them.  So I just copy and paste the spec part into the body and run it through the program.

                          -- Adam



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-08  6:09 ` Jeffrey Carter
@ 2012-07-13 15:51   ` Robert A Duff
  0 siblings, 0 replies; 12+ messages in thread
From: Robert A Duff @ 2012-07-13 15:51 UTC (permalink / raw)


Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> writes:

> Consider the following:
>
> package P is
>    function F (I : Integer) return Integer;
>
>    type Integer_List is array (Integer range <>) of Integer;
>
>    function F return Integer_List;
> end P;
>
> How are you going to write the body of P so the compiler knows which
> body implements which function F without repeating the parameter list
> and return type profile, which is what distinguishes these 2 overloaded
> functions?

Well, that wouldn't be too hard of a problem to solve.
I think the real reason for duplicating the information
is readability.

For example, you could require that two functions in the same scope
have different names.  You could still use overloading via
renames:

   function F_Integer (I : Integer) return Integer;
   function F (I : Integer) return Integer renames F_Integer;
   ...
   function F_Integer_List return Integer_List;
   function F return Integer_List renames F_Integer_List;

I tend to use this style anyway, because then the debugger
tells me which function I'm in.  Also, if this were the rule,
the compiler could more easily give good error messages.
Consider:

    procedure P (X : Integer);
    ...
    procedure P (X : Long_Integer) is -- Wrong.
    begin
        ...
    end P;

where the mistake is that I changed Integer to Long_Integer
in one place but forgot the other.  A typical compiler will
complain incorrectly that P doesn't have a body.

One could facilitate the above renamings with a shorthand:

   function F_Integer, F (I : Integer) return Integer;
   function F_Integer_List, F return Integer_List;

- Bob



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: on the need to duplicate code for procedure signature in both body and interface files
  2012-07-07 23:08 on the need to duplicate code for procedure signature in both body and interface files Nasser M. Abbasi
                   ` (6 preceding siblings ...)
  2012-07-09 15:11 ` Adam Beneschan
@ 2012-07-13 17:12 ` björn lundin
  7 siblings, 0 replies; 12+ messages in thread
From: björn lundin @ 2012-07-13 17:12 UTC (permalink / raw)
  Cc: nma

Den söndagen den 8:e juli 2012 kl. 01:08:53 UTC+2 skrev Nasser M. Abbasi:
> 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).
> 
I think a good reason is when you hvae a large system to write.
I person does the design, and writes thes specs.
He the writes a basic flow, while others may write  the implementations.

It was perhaps more important in the days before good version handling systems.

But still, it is a useful feature, just as separates are.
When many people wants to change in the same file, separate some functions may help.
And they are still visible in the body.

/Björn






^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-07-13 17:14 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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