comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: how to organize source code for a complete software? Thanks!
Date: Sun, 09 Oct 2011 20:37:43 +0200
Date: 2011-10-09T20:37:43+02:00	[thread overview]
Message-ID: <878vouovt4.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 9fe53iFoe7U1@mid.individual.net

Niklas Holsti writes on comp.lang.ada:
> On 11-10-09 18:20 , Jinsong Zhao wrote:
>> Hi there,
>>
>> I am new to Ada, and I just have some experiences in using Fortran.
>
> Welcome, then. May I ask what attracted you to Ada? I am asking partly
> out of curiosity, but also because if you tell us what you hope to
> gain by using Ada, we may better advise you on how to organize your
> code, and on other Ada usage.

I am curious, too.

>> When using Fortran, I generally put each subroutine or function in a
>> separate file, e.g., dot.for for dot product of two vectors. Then a
>> main.for that call the necessary subroutine or functions. It seems to
>> be rational.

OK but where do you place variables that are shared by multiple
subroutines or functions?  Where do you place type declarations?  This
is quite crucial for my point below.

>> When I using Ada, I don't know how to do.

In addition to the rules of thumb given by others, I'll now explain the
reasoning behind a proper choice of where to place your subprograms.
It's all about _visibility_.

package P is
   -- this part is visible to anyone who says "with P;"
private
   -- this part is visible only from the body of P
end P;

package body P is
   -- this part is completely hidden
end P;

procedure Proc is
   -- this part is completely hidden
begin
   -- this part is completely hidden
end Proc;

A subprogram declared in the package P can see everything in the public
and private parts of the spec; it can also see everything that has been
declared above it inside the body.  Thus, a package spec will normally
look like:

package P is
   type T is private;
   procedure Foo (Param : in out T);
private
   type T is ...;
end P;

If several subprograms use the same type T in their specs, then they
should all be declared in the same package.  So, my advice to you is to
organize your sources not in terms of subprograms, but in terms of
types; each major type should be in a package of its own, along with all
the subprograms that operate on it.  One example that I wrote is here:

http://green.ada-france.org:8081/revision/browse/108fe173864fa16185a547d486849a475dd3c2a3

You will find one main subprogram (test.adb), one spec
(s_expression.ads) declaring a type T and all subprograms operating on
it (the type T is private and its details are in the private part of the
spec), and one package body (s_expression.adb) containing the
implementation of those subprograms.

-- 
Ludovic Brenta.



  reply	other threads:[~2011-10-09 18:37 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-09 16:20 how to organize source code for a complete software? Thanks! Jinsong Zhao
2011-10-09 17:16 ` stefan-lucks
2011-10-10  5:52   ` Jinsong Zhao
2011-10-09 17:37 ` Yannick Duchêne (Hibou57)
2011-10-10  6:02   ` Jinsong Zhao
2011-10-10 13:15     ` Paul Colin Gloster
2011-10-10 15:46       ` Simon Wright
2011-10-10 19:03       ` RasikaSrinivasan@gmail.com
2011-10-10 22:12       ` Randy Brukardt
2011-10-09 17:48 ` Niklas Holsti
2011-10-09 18:37   ` Ludovic Brenta [this message]
2011-10-09 21:24     ` J-P. Rosen
2011-10-10 12:09     ` Jinsong Zhao
2011-10-10  6:45   ` Jinsong Zhao
replies disabled

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