comp.lang.ada
 help / color / mirror / Atom feed
From: Alan E & Carmel J Brain <aebrain@dynamite.com.au>
Subject: Re: Documenting Code (was:Programming language vote - results)
Date: 1997/10/31
Date: 1997-10-31T00:00:00+00:00	[thread overview]
Message-ID: <3459A84A.246@dynamite.com.au> (raw)
In-Reply-To: 345776DD.424B@hal-pc.org


Don Guinn wrote:

> > Don Guinn wrote:
> > Why is it OK to spend a whole morning understanding a couple pages of
> > FORTRAN code, yet it is unreasonable to spend the same amount of time
> > with a 2-liner in APL or J which does the same thing?

> > I prefer to use APL but I have long ago realized that it is beneficial to
> > spread it out a bit rather than use very complex 1 or 2 liners.

> You are right.  It does make it easier to read if things are spread out.

> As far as documenting code.  Documentation is wonderful when it adds
> information and insight on what the program does.  I have seen too many
> programs full of documentation which does nothing but repeat what is
> already stated in the code itself.  It's as if the person who wrote it
> can't read his own code.

> Documentation needs to explain the concepts used in an application.  It
> should provide definitions of terms (variables) and possible things to
> watch out for.  But it should not have to translate the source code into
> English.  It should not be measured by the pound.

My viewpoint:

If one is using a language that is "terse", such as APL, where WHAT the
thing does is not immediately obvious, then a quickly-read line which
explains what the code is _supposed_ to be doing is very helpful. At
first glance, it can tell a reader what the code does, more quickly than
ploughing through cryptic things like

GHTransmit(const char* const X);

which is basically a read-only string input to a function.

Secondly, comments often tell what the program is _supposed_ to do, not
always what it does. This means that a quick-scan of the comments only
will not reveal a bug. OTOH a more careful reading, which would reveal
the difference between actual and intended, automatically finds the bug
and suggests a solution.

OK, that's it for documenting WHAT the code does. In many, less terse
languages, comments are more useful for describing _why_ something is
done. For example (in Ada-83);

with Tracker;			-- package for Radar Trackers only!
with TacticalPicture;           -- what's the environment we're in

procedure AllocateRadarTrackers
(TheTrackerStates         in Tracker.StateTableType,
 TheTacticalPicture       in TacticalPicture.BasicRecordType,
 ResultantTrackerCommands out Tracker.CommandListType)
is
-- Procedure that, given a table of the current radar tracker
-- states, and the tactical picture, decides on what commands
-- to give to the radar trackers.
-- These commands are then expected to be transmitted to the
-- trackers by another procedure.
-- Exceptions that can be raised here are:
--   AllTrackersKaputException ( The Trackers are all KO )
--   UnexpectedErrorException  ( Bug or hardware fault)

begin
  -- and so on and so on
end;

Note that in this (deliberately) verbose and non-terse example, the
variables, procedure, exception and type names are self-explanatory.
To some degree, the code _is_ the comment, at least on "what it does".
Most of the comments "as such" are either explanations of the intention,
or give implications of events.

A terser piece of code that is exactly hemeomorphic is:

with Trk;
with TacPic;
procedure TCmds ( T in Trk.Lst, TP in TacPic.TP, Cmds out Trk.Cmds )
is
-- Procedure that, given a table of the current radar tracker
-- states (Trk.Lst), and the tactical picture (TacPic.TP), decides on
-- what commands to give to the radar trackers (Trk.Cmds).
-- These commands are then expected to be transmitted to the
-- trackers by another procedure.
-- Exceptions that can be raised here are:
--   ATK ( _A_ll _T_rackers _K_O )
--   FUBAR  ( Bug or hardware fault)
begin
-- etc etc
end;

Note that despite some bad names, the comments explain what's happening.
Without the comments, this would be so cryptic as to be baroque. A
translation into C++ would be something like

#include TP.h;  // Tactical Picture Class
#include Trk.h; // Tracker Class

Trk::CmdList* TCmds( const Trk::Lst* const TL, const TP::Data* const TP)

// Procedure that, given a table of the current radar tracker
// states (Trk::Lst), and the tactical picture (TP::TP), decides on
// what commands to give to the radar trackers (Trk::Cmds).
// These commands are then expected to be transmitted to the
// trackers by another procedure.
// It's assumed that TL and TP are both going to be used later, so
// the pointers have been made const, as well as the data pointed to.
// Exceptions that can be thrown here are:
//   ATK ( _A_ll _T_rackers _K_O )
//   FUBAR  ( Bug or hardware fault)

{
 // etc etc
}  

When using Hungarian Notation, a lot more verbosity sneaks in, as in the
first Ada example, which has an equivalent of Hungarian Notation in it ( 
Tracker.StateTableType rather than Tracker.State for example)

How NOT to do it (in any language):

procedure P_12_356( X99 in integer )
is
  NumberOfBananas : integer; -- The number of Bananas
  NumberOfBaskets : integer; -- The number of Baskets
  F : float;
begin
 -- and so on in this deliberately BAD example
 -- which shows that it's possible to write really bad Ada,
 -- and make comments that are both verbose and useless,
--  but you have to really, really work at it!
end;

I still for the life of me can't understand why C++ is so popular, yet
Ada is described as obsolete, etc etc when for a lot of purposes, Ada is
C++ with (usually more) clarity, power and safety belts. Certainly in my
experience C++ programmers who've had some exposure to Ada have an
advantage over the rest. But never mind.
--     
aebrain@dynamite.com.au     <> <>    How doth the little Crocodile
| Alan & Carmel Brain|      xxxxx       Improve his shining tail?
| Canberra Australia |  xxxxxHxHxxxxxx _MMMMMMMMM_MMMMMMMMM
 abrain@cs.adfa.oz.au  o OO*O^^^^O*OO o oo     oo oo     oo  
                    By pulling MAERKLIN Wagons, in 1/220 Scale






  parent reply	other threads:[~1997-10-31  0:00 UTC|newest]

Thread overview: 147+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <343fbb5a.0@news.iprolink.ch>
1997-10-11  0:00 ` Programming language vote - results Gary L. Scott
1997-10-12  0:00   ` Jack Rudd
1997-10-13  0:00     ` safetran
1997-10-13  0:00       ` FRS DES
1997-10-13  0:00       ` Jack Rudd
1997-10-14  0:00         ` Philip Brashear
1997-10-14  0:00           ` Gary L. Scott
1997-10-13  0:00     ` Gary L. Scott
1997-10-13  0:00     ` Robert Munck
1997-10-13  0:00       ` Jack Rudd
1997-10-13  0:00       ` Gary L. Scott
     [not found]     ` <3442B745.5352@lmco.com>
1997-10-15  0:00       ` Gary L. Scott
1997-10-16  0:00       ` James Giles
1997-10-16  0:00         ` Andrew Haley
1997-10-13  0:00   ` Robert S. White
1997-10-13  0:00     ` Gary L. Scott
1997-10-13  0:00   ` Matthew Heaney
1997-10-14  0:00     ` Gary L. Scott
1997-10-13  0:00   ` David Ness
1997-10-14  0:00     ` Jan Karman
1997-10-15  0:00       ` Alan E & Carmel J Brain
1997-10-15  0:00         ` D'Arcy J.M. Cain
1997-10-15  0:00           ` Mark Stephen
1997-10-17  0:00             ` Randy MacDonald
1997-10-15  0:00           ` FRS DES
1997-10-16  0:00           ` Randy MacDonald
1997-10-16  0:00           ` Alan E & Carmel J Brain
1997-10-16  0:00             ` FRS DES
1997-10-17  0:00               ` Jerry van Dijk
1997-10-16  0:00             ` John Sullivan
1997-10-17  0:00               ` Alan E & Carmel J Brain
1997-10-17  0:00                 ` John Sullivan
1997-10-17  0:00               ` Randy MacDonald
1997-10-17  0:00             ` Randy MacDonald
1997-10-20  0:00               ` Alan E & Carmel J Brain
1997-10-20  0:00                 ` Lawrence Kirby
1997-10-20  0:00                   ` Kaz
1997-10-21  0:00                     ` Alan E & Carmel J Brain
1997-10-23  0:00                     ` Ada Readability (Re: Programming language vote - results) Ray Blaak
1997-10-21  0:00                   ` Programming language vote - results Alan E & Carmel J Brain
1997-10-20  0:00                 ` FRS DES
1997-10-21  0:00                   ` Alan E & Carmel J Brain
1997-10-21  0:00                 ` Randy MacDonald
1997-10-22  0:00                   ` Don Guinn
     [not found]                     ` <01bce1bf$5c2baaa0$95b66bcf@dkelly.ark.com>
1997-10-29  0:00                       ` FRS DES
1997-10-29  0:00                       ` Don Guinn
1997-10-29  0:00                         ` Shmuel (Seymour J.) Metz
1997-10-31  0:00                         ` Alan E & Carmel J Brain [this message]
1997-10-30  0:00                           ` Documenting Code (was:Programming language vote - results) Charles Lin
1997-10-30  0:00                             ` James L. Ryan
1997-10-31  0:00                               ` Robert Bernecky
1997-10-31  0:00                             ` Robert Bernecky
1997-11-01  0:00                           ` Randy MacDonald
1997-11-01  0:00                             ` Robert Dewar
1997-11-03  0:00                               ` Jon S Anthony
1997-10-29  0:00                     ` Programming language vote - results Randy MacDonald
1997-10-25  0:00                   ` Alan E & Carmel J Brain
1997-10-26  0:00                     ` functionality of Java (was Re: Programming language vote - results) Randy MacDonald
1997-10-23  0:00                 ` Programming language vote - results Jack Rudd
1997-10-25  0:00                   ` Alan E & Carmel J Brain
1997-10-25  0:00                     ` Kaz
1997-10-26  0:00                       ` FRS DES
1997-10-27  0:00                       ` Robert Bernecky
1997-10-27  0:00                         ` APL argument W. Wesley Groleau x4923
1997-10-28  0:00                           ` Randy MacDonald
1997-10-28  0:00                         ` Programming language vote - results Jan Karman
1997-10-28  0:00                           ` Robert Bernecky
1997-10-28  0:00                             ` James L. Ryan
1997-10-29  0:00                               ` Robert Bernecky
     [not found]                                 ` <bosworth-2910972044300001@access59.accsyst.com>
1997-10-30  0:00                                   ` Robert Bernecky
1997-10-30  0:00                                     ` James L. Ryan
1997-10-31  0:00                                       ` Robert Bernecky
1997-10-31  0:00                                         ` James L. Ryan
1997-10-29  0:00                     ` Jack Rudd
1997-10-25  0:00                 ` Peter Seebach
1997-11-18  0:00                   ` Ingemar Ragnemalm
1997-11-18  0:00                     ` firewind
1997-11-18  0:00                       ` Kevin Swan
1997-11-19  0:00                         ` Alan E & Carmel J Brain
1997-11-18  0:00                       ` Larry Elmore
1997-11-20  0:00                         ` firewind
1997-11-19  0:00                       ` Mike Smith
1997-11-19  0:00                         ` Matt
1997-11-20  0:00                         ` firewind
     [not found]                           ` <3474C71B.536B12F6@cgocable.net>
1997-11-21  0:00                             ` CVigue
1997-11-23  0:00                           ` Lawrence Kirby
1997-11-24  0:00                             ` FRS DES
1997-11-20  0:00                       ` Andy Knight
1997-11-20  0:00                         ` firewind
1997-11-20  0:00                       ` Coding for Obscurity Alan E & Carmel J Brain
1997-11-20  0:00                         ` firewind
1997-11-20  0:00                           ` Jos A. Horsmeier
1997-11-20  0:00                         ` Stephan Wilms
1997-11-21  0:00                           ` Jos A. Horsmeier
1997-11-23  0:00                           ` Alex Krol
1997-11-24  0:00                             ` Jim Johnson
1997-11-24  0:00                               ` Mark Wilden
1997-11-26  0:00                                 ` Robert S. White
1997-11-26  0:00                                   ` Mark Wilden
1997-11-26  0:00                                   ` Miguel Carrasquer Vidal
1997-12-01  0:00                                     ` ISONE
1997-12-01  0:00                                     ` ISONE
1997-11-26  0:00                                   ` Leon Jones
1997-11-26  0:00                                     ` Lawrence Kirby
1997-11-26  0:00                                     ` Ron Natalie
1997-11-27  0:00                                       ` Joerg Rodemann
1997-11-27  0:00                                   ` Richard A. O'Keefe
1997-11-23  0:00                           ` Al Christians
1997-11-24  0:00                           ` Richard A. O'Keefe
1997-11-24  0:00                             ` Samuel T. Harris
1997-11-24  0:00                               ` Jon S Anthony
1997-11-25  0:00                                 ` Samuel T. Harris
1997-11-24  0:00                             ` Matt
1997-11-24  0:00                               ` Ed Falis
1997-11-20  0:00                       ` Programming language vote - results Terry Richards
1997-11-20  0:00                         ` Andy Knight
1997-11-23  0:00                         ` Alex Krol
1997-11-25  0:00                         ` William Tanksley
1997-11-26  0:00                           ` Ron Natalie
1997-11-27  0:00                             ` William Tanksley
1997-11-27  0:00                               ` Lawrence Kirby
     [not found]                                 ` <65keij$mkd$1@nerd.apk.net>
1997-11-27  0:00                                   ` Kaz Kylheku
1997-11-28  0:00                               ` Shmuel (Seymour J.) Metz
1997-12-01  0:00                                 ` FRS DES
1997-11-18  0:00                     ` Lawrence Kirby
1997-11-24  0:00                       ` Martin M Dowie
1997-11-25  0:00                         ` Kaz Kylheku
1997-11-26  0:00                           ` Peter Seebach
1997-12-02  0:00                           ` ANDREAS LEITNER
1997-12-02  0:00                             ` Robert Dewar
1997-12-02  0:00                             ` Lawrence Kirby
1997-12-03  0:00                               ` Billy Chambless
1997-12-03  0:00                                 ` Robert Dewar
1997-12-05  0:00                             ` John Sullivan
1997-11-25  0:00                         ` Mark Wilden
1997-11-25  0:00                           ` Martin M Dowie
1997-11-26  0:00                             ` Lawrence Kirby
1997-11-26  0:00                           ` FRS DES
1997-11-18  0:00                     ` Kevin Swan
1997-11-29  0:00                       ` Ingemar Ragnemalm
1998-09-10  0:00                         ` Steven Katz
1997-11-19  0:00                     ` Alan E & Carmel J Brain
1997-11-19  0:00                     ` Peter Seebach
     [not found]           ` <01bcdad2$fa9fdf60$25a43a91@basil.omroep.nl>
1997-10-17  0:00             ` D'Arcy J.M. Cain
1997-10-17  0:00         ` Robert I. Eachus
1997-10-14  0:00     ` Randy MacDonald
1997-10-19  0:00 ` William Rapp
replies disabled

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