Newsgroups: comp.lang.ada
From: Maciej Sobczak <no.s...@no.spam.com>
Date: Tue, 15 Nov 2005 17:03:50 +0100
Local: Tues, Nov 15 2005 11:03 am
Subject: Request for comments on simple Ada program
Hi,
While playing with Ada and exercising its constructs, I have written the with Ada.Text_IO; procedure Primes is -- classic sieve declare Upper_Limit : Positive; begin Print_Primes(Upper_Limit); Now my question is: What would do do to make this code "better"? Would you add comments? Loop labels? Change names? Is one-character name The question is not whether this program is correct, but rather what I'm interested in style and what do you consider to be a *good* Ada Note: This is not a homework, I'm a self-learner. -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Samuel Tardieu <s...@rfc1149.net>
Date: 15 Nov 2005 18:43:39 +0100
Local: Tues, Nov 15 2005 12:43 pm
Subject: Re: Request for comments on simple Ada program
Maciej> I'm interested in style and what do you consider to be a
Maciej> *good* Ada code. Please do not hesitate to be as picky as you Maciej> want - that's exactly the point of this post. Ok, you want some nitpicking, here is some :) After my signature, you can find the way I would have coded it. I My example is much more verbose than yours. However, it is also much Of course, all that is a question of taste. Sam with Ada.Text_IO; use Ada.Text_IO; procedure Primes_Printer is procedure Print_Primes (Primes : Candidates_Array); procedure Print_Primes_Up_To (Upper_Bound : Potential_Prime); ------------------ procedure Print_Primes (Primes : Candidates_Array) is ------------------------ procedure Print_Primes_Up_To (Upper_Bound : Potential_Prime) is Upper_Limit : Potential_Prime; begin Print_Primes_Up_To (Upper_Limit); package Primes is subtype Potential_Prime is Positive range 2 .. Positive'Last; type Candidates_Array is array (Potential_Prime range <>) of Boolean; procedure Check_Primes (Candidates : in out Candidates_Array); end Primes; package body Primes is ------------------ procedure Check_Primes (Candidates : in out Candidates_Array) is procedure Mark_Multiples (Multiplier : in Potential_Prime); -------------------- procedure Mark_Multiples (Multiplier : in Potential_Prime) begin end Primes; with Ada.Text_IO; use Ada.Text_IO; -- Test correctness of the Check_Primes function for primes up to 10_000 procedure Test_Primes is function Is_Prime (N : Potential_Prime) return Boolean; ------------- function Is_Prime (N : Potential_Prime) return Boolean is Primes : Candidates_Array (2 .. 10_000) := (others => True); begin You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Samuel Tardieu <s...@rfc1149.net>
Date: 15 Nov 2005 18:47:09 +0100
Local: Tues, Nov 15 2005 12:47 pm
Subject: Re: Request for comments on simple Ada program
Sam> It doesn't seem logical to have to modify any part involved in
Sam> prime numbers computation, especially if you want to be able to Sam> test it in isolation. Moreover (I forgot this point), you can now change the Check_Primes Sam You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Jacob Sparre Andersen <spa...@nbi.dk>
Date: Tue, 15 Nov 2005 22:28:05 +0100
Local: Tues, Nov 15 2005 4:28 pm
Subject: Re: Request for comments on simple Ada program
Samuel Tardieu wrote: Overall I like Samuel's version, but since we're picking nits, I'll pick some in the comments (the code is as good as it gets) ... > procedure Print_Primes_Up_To (Upper_Bound : Potential_Prime); ... between 2 and Upper_Bound. > -- Print all primes between 2 and Potential_Prime > ------------------ I use similar comments to separate/introduce procedures, but what do > -- Print_Primes -- > ------------------ > procedure Print_Primes (Primes : Candidates_Array) is we add by repeating the name of the procedure in the comment? Except for something which later can be different from the actual name of the procedure? Writing the function of the procedure in a comment is OK, although I Jacob (who believes that the optimal comment/code ratio is zero) You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Samuel Tardieu <s...@rfc1149.net>
Date: 15 Nov 2005 22:53:34 +0100
Local: Tues, Nov 15 2005 4:53 pm
Subject: Re: Request for comments on simple Ada program
Jacob> Jacob (who believes that the optimal comment/code ratio is
Jacob> zero) It depends. Sometimes (particularily when doing embedded stuff), you What I do in general is reread every code I write one week later. Each Sam You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Anders Wirzenius <and...@no.email.thanks.invalid>
Date: Wed, 16 Nov 2005 09:10:44 GMT
Local: Wed, Nov 16 2005 4:10 am
Subject: Re: Request for comments on simple Ada program
Samuel Tardieu <s...@rfc1149.net> writes: Excellent way of working! > What I do in general is reread every code I write one week later. Each > time I ask myself "why did I do this?", even for one second, I add a > comment. My experience shows that even when I go through the code one > year later, I have no question that are not already answered in the > comments. Once upon a time (early eighties) when I was a Fortran programmer, my -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Samuel Tardieu <s...@rfc1149.net>
Date: 15 Nov 2005 22:55:41 +0100
Local: Tues, Nov 15 2005 4:55 pm
Subject: Re: Request for comments on simple Ada program
>>>>> "Jacob" == Jacob Sparre Andersen <spa...@nbi.dk> writes: Jacob> ... between 2 and Upper_Bound. >> Print all primes between 2 and Potential_Prime Yeah, typo while coming up with an educative example :) Jacob> I use similar comments to separate/introduce procedures, but Visual effect. If you have an editor which emphasizes subprogram names Sam You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Niklas Holsti <nob...@nowhere.fi>
Date: Wed, 16 Nov 2005 11:03:12 +0200
Local: Wed, Nov 16 2005 4:03 am
Subject: Re: Request for comments on simple Ada program
Jacob Sparre Andersen wrote: People differ. In my Ada code, the "comments" average 33% of > Writing the function of the procedure in a comment is OK, although I > would like to keep all the non-checkable stuff outside the source > file. > Jacob (who believes that the optimal comment/code ratio is zero) non-blank lines (59% for package specs, 23% for package bodies, and note that I never repeat comments from specs in bodies). I still occasionally find it hard to *fully* understand code from a couple of years back. It's not the details -- I know what N := N + 1 means by itself -- but the background assumptions, intentions, limitations, usage rules, interactions. I put "comments" in quotes above, because I think that this is a As for keeping such text in separate design documents, I would do -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Jacob Sparre Andersen <spa...@nbi.dk>
Date: Wed, 16 Nov 2005 15:21:06 +0100
Local: Wed, Nov 16 2005 9:21 am
Subject: Re: Request for comments on simple Ada program
Niklas Holsti wrote: My numbers are 33% in specs and 18% in bodies (22% in total), but I > Jacob Sparre Andersen wrote: >> Writing the function of the procedure in a comment is OK, although >> I would like to keep all the non-checkable stuff outside the source >> file. >> Jacob (who believes that the optimal comment/code ratio is zero) > In my Ada code, the "comments" average 33% of non-blank lines (59% consider them to be too high. My reason for mentioning what I believe is the optimal comment/code > I still occasionally find it hard to *fully* understand code from a That can happen to me too - and to almost any other programmer too, I > couple of years back. It's not the details -- I know what N := N + 1 > means by itself -- but the background assumptions, intentions, > limitations, usage rules, interactions. suppose. > I put "comments" in quotes above, because I think that this is a bad But I don't put the rationale in the source code. The rationale is > case of mis-naming in programming language terminology. The word > "comment" implies something skimpy, an addition, a note; in my view, > what is needed is a rationale, description or motivation that is > mainly written *before* the code itself. kept in a separate file. What I still do put in the source code - and don't like putting there - is the abstracts for the subprograms and packages. But how can I easily cross-link the documentation (with the abstracts) and the code (with the implementations)? Should I cite the package name/full subprogram specification in the documentation to create the cross-link? Or can somebody come up with a more elegant solution? > As for keeping such text in separate design documents, I would do it I always keep the design documents separate (when they exist ;-), but > if a customer demanded it, but I think it would be much harder to > manage changes, versions and configurations accurately. Still, some > very high-level descriptions are nice to keep apart from the > source-code, if they are not outdated by day-to-day code changes. I must admit that programming for research purposes is a bit different from commercial software development. Jacob You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
Newsgroups: comp.lang.ada
From: Niklas Holsti <nob...@nowhere.fi>
Date: Wed, 16 Nov 2005 19:08:50 +0200
Local: Wed, Nov 16 2005 12:08 pm
Subject: Re: Request for comments on simple Ada program
Jacob Sparre Andersen wrote: On a philosophical level, I agree with you -- writing "comments" > Niklas Holsti wrote: >>... >>>Jacob (who believes that the optimal comment/code ratio is zero) is an extra burden, and one risks contradictions between the comments and the code (I recall an article long ago in SIGPLAN Notices with the title "Comments considered harmful" :-) An ideal language should be expressive and readable enough to > My reason for mentioning what I believe is the optimal comment/code I agree that such a metric can be very misleading, because then > ratio, is actually due to an article in ACM Queue, where the authors > used a code quality metric, which equalled more comments with higher > code quality. you should measure the quality of the comments, too, which is impossible to automate. But I think anyone who has tried to reuse two code packages, one with (good) comments and the other without, must feel that there is *some* truth in the metric. > But I don't put the rationale in the source code. The rationale is There are some IDE-like commercial tools -- if memory serves, some > kept in a separate file. What I still do put in the source code - and > don't like putting there - is the abstracts for the subprograms and > packages. But how can I easily cross-link the documentation (with the > abstracts) and the code (with the implementations)? Should I cite the > package name/full subprogram specification in the documentation to > create the cross-link? Or can somebody come up with a more elegant > solution? sort of souped-up requirements databases + design tools + code generatorts with "automatic" document generation functions -- that claim to keep these cross-links for you. But burying my code in that sort of a monster machine makes me very nervous, and they aren't cheap either. The conversation seems to be drifting towards Knuth's "literate -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
| Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy |
| ©2010 Google |