From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,FREEMAIL_FROM, PLING_QUERY,WEIRD_PORT autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,2f6e39a9d25bcd8b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news4.google.com!feeder.news-service.com!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Jinsong Zhao Newsgroups: comp.lang.ada Subject: Re: how to organize source code for a complete software? Thanks! Date: Mon, 10 Oct 2011 20:09:01 +0800 Organization: A noiseless patient Spider Message-ID: References: <9fe53iFoe7U1@mid.individual.net> <878vouovt4.fsf@ludovic-brenta.org> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 10 Oct 2011 12:09:03 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="3fCTFamTIBvgxR0jRHkjfQ"; logging-data="10511"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/l5VRwRlrN2GHJKpfBL9kK" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: <878vouovt4.fsf@ludovic-brenta.org> Cancel-Lock: sha1:G8N39KUHpNwzMOgudEX5Kx6PLe4= Xref: g2news2.google.com comp.lang.ada:22317 Date: 2011-10-10T20:09:01+08:00 List-Id: On 2011-10-10 2:37, Ludovic Brenta wrote: > 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. In Fortran 90 or newer standard, those variables and type declaration can be put into module. In Fortran 77, the variable are put into COMMON, and block data section. > >>> 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. > Thank you very much for your example and advices on visibility. Regards, Jinsong