1-6 PROGRAM LAYOUT - THE ART OF MAKING PROGRAMS READABLE
*********************************************************
(Thanks to Arne Vajhoej and Clive Page for the good suggestions
and comments, Kenneth Plotkin for the good comments, and of
course to Craig Burley)
Program layout is the art of arranging program code in a READABLE
and EASY TO EDIT way.
Some practical methods to improve layout are:
1) Indenting control structures
2) Separating functional units
3) Choosing good identifiers
4) Adding comments, procedure headings etc
Indentation of control structures
---------------------------------
Control constructs should be properly indented to reveal the
internal structure. Avoid an excessive indentation step that
may take you too quickly to the last allowed column.
Recommended step sizes are in the range 2-4.
This form:
DO 200 I = 1, 100
DO 100 J = 1, 100
WRITE(*,*) I, J
100 CONTINUE
200 CONTINUE
Is surely more readable than:
DO 200 I = 1, 100
DO 100 J = 1, 100
WRITE(*,*) I, J
100 CONTINUE
200 CONTINUE
It is advisable (for good programming style and aiding automatic
optimization) to avoid statement labels and GOTOs as much as possible,
if you must have them, make the label numbers strictly increasing.
Maybe the time has come to use DO ... END DO and other semi-standard
constructs? Most FORTRAN 77 compilers support them, and of course
all the Fortran 90 compilers.
If you must ensure absolute code portability, always use CONTINUE as
the terminal statement and only _one_ loop per terminal CONTINUE
statement, as shown above.
It is easy to say that you should avoid too deep nesting of control
structures inside each other and getting close to the last allowed
column. It is not always possible to follow such advice.
Separating functional units
---------------------------
An important concept that is not used in the Fortran standards,
nor in many Fortran texts, but is a popular term in the compiler
arena, is the BASIC BLOCK.
A BASIC BLOCK is a sequence of consecutive language statements
with the following properties:
1) Program flow doesn't jump into or from that block except
maybe to the first statement or from the last statement.
A simple example of such a jump is executing any GOTO
statement, but IF and DO statements are really constructed
out of similar implied jumps.
2) The sequence is maximal in the sense that adding one
more statement at the beginning of the block, or one
more statement at the end, will make the previous
requirement false.
When you examine the way program control flows, the basic blocks
are the natural building blocks, so making their boundaries stand
out will make the code easier to follow.
Simple ways to mark basic block boundaries are:
1) Using a separator line composed of:
{'*' or 'C' or '!' in column 1} // {6*SPACE} //
{dash|equal|asterisk line from column 7 to column 72}
2) An empty line
However, the basic block concept should be used only as a guide,
you may add additional separator lines if beneficial, or drop them.
In some cases the separator lines creates visual clutter, and
should be dropped:
1) Block IF statements actually contain two basic blocks,
with implicit conditional jumps selecting between them.
However if the statements are properly indented the
indentation does the job, and there is no need for
separating lines.
IF (...) THEN
.......
ELSE
.......
ENDIF
2) Similar considerations apply to nested DO loops.
DO (...)
DO (...)
.......
END DO
END DO
Limitations on identifiers
--------------------------
FORTRAN 77 imposes a limit of six characters, many FORTRAN 77
compilers (and of course fortran 90) allow longer names.
It is not likely that new programs will be ported to a compiler
that doesn't support longer names, so maybe it's time to forget
this old restriction.
It is interesting to compare FORTRAN 77 with the C Standard that
came out much later, in that standard you can use identifiers at
least 509 characters long but:
1) Names used only internally (by the compiler) have to
be distinct in the first 31 characters
2) Names used externally (also seen by the linker) have
to be distinct in the first 6 characters, and be so
even ignoring the case of letters (sounds familiar?)
The '6 characters with no case' limit ensures that linkers on
all systems (remember that the linker is basic system software
and NOT specific to the language you use) can do their job.
Another common restriction is that names should begin with a
letter and contain alphanumeric characters (underscore is
considered a letter in C).
Choosing good identifiers
-------------------------
Identifiers should be meaningful and describe the variable, constant,
procedure, etc that they represent.
Inventing good names may be difficult, even if you can use long
identifiers, when you try to specify the role of a certain variable
you run a lot of times into 4-word (and more) names, for example:
input_buffer_start_index, input_buffer_end_index
Of course you cannot put many such identifiers into the [7,72]
column range of FORTRAN 77, and you don't really want to.
Highly modular programs usually declare few variables in each
procedure, and have less need for long and complicated identifiers
to characterize them.
Classical abbreviation method
-----------------------------
A possible solution is combining together two different methods
each of them capable of shortening a long name:
1) Separately truncate/abbreviate each part of
the name, e.g.
input --> in
output --> out
buffer --> buf
begin --> beg
index --> ix (a little drastic, but still a mnemonic)
number --> num
error --> err
2) Drop 'non-essential' letters (a, e, i, o, u, y)
next --> nxt
keyword --> kwrd
format --> frmt
The result of both transformations is:
in_buf_beg_ix, in_buf_end_ix
The underscores are usually omitted in names composed
of two parts:
inbuf, begix
The identifier can be further shortened if you use
capitalization instead of underscores:
InBufBegIx, InBufEndIx
These methods are usually supplemented by some
conventions:
1) A prefix 'n' means 'number of' or 'size of'
e.g. nbits - number of bits
2) The letters I, J, K are reserved for loop
control variables, array indexes and
sub-string indexes
A tentative naming convention
-----------------------------
Naming conventions are beneficial, because they provide programs with
a sense of continuity and style.
Many C programmers use the Hungarian naming convention which is highly
oriented towards the computer architecture, and encodes mainly info
about the variable's data type.
However, FORTRAN has a different character, it is more problem oriented,
so a FORTRAN naming convention MUST BE SPECIFICALLY ADAPTED to the
specific requirements of every program.
A possible starting point may be the following:
Syntax: XYY_TEXTn
Where: X = G General-purpose
I Input
O Output
S System
U User-supplied (Interactively)
Y = B Buffer
F Format (Or file-name?)
G General-purpose
P Array index (Pointer)
T Text
_TEXTn modifier e.g. len, top, bot, with
possibly one digit at the end.
(if lacking identifier is general purpose)
Case usage
----------
FORTRAN KEYWORDS (IF, SUBROUTINE)
constants (pi, solar_const)
Variables (IP_Start, IP_End)
Global Names (COMMON /Blk1/, CALL MySub)
Remember that the only global names in FORTRAN are procedure and
common block names (and logical unit numbers).
Using this naming convention the two 4-word identifiers we had
above will become:
IP_Start, IP_End
Some more layout techniques (FORTRAN 77 oriented)
-------------------------------------------------
A small example program:
PROGRAM IDEXMP
INTEGER BUS_NUM
BUS_NUM = 99
WRITE(*,*) ' TAKE BUS ', BUS_NUM
END
can be transformed into:
C +-----------------------------------------------------------------
C | Program: Bus number advice
C | Author: Abraham Agay
C | Date: 28.11.1995
C +-----------------------------------------------------------------
PROGRAM IdExmp
C ------------------------------------------------------------------
INTEGER
* Bus_Num
C ------------------------------------------------------------------
Bus_Num = 99
WRITE(*,*) ' Take bus ', Bus_Num
C ------------------------------------------------------------------
END
For a small program this layout technique just adds "visual clutter",
however in a large program with long variable lists and many code
sections it may really help.
Using continuation lines in declarations makes it easy to edit
variable lists (The FORTRAN standard allows up to 19 continuation
lines - 20 lines total).
The separating comment lines end at column 72, so you can see at
a glance if some line is longer than the FORTRAN maximum of 72
columns. Amateur programmers do that mistake a lot and sometimes
it's hard to trace (when professionals do it, it's even harder...).
Many compilers allow code extending beyond column 72 (usually with
a suitable compiler option), but it is not standard. By default
characters beyond column 72 are ignored by most standard-conforming
compilers.
By the way, the presence of TAB characters may create confusion
when counting characters (e.g. to determine the line width),
editing programs usually interpret TABs as taking place up to
the next tab-stop, and display them accordingly.
You can define some key combination that will make your
editor insert such a separating line in your code.
Separating lines
----------------
You can use various types of separating lines to mark
the the executable part of the code, important loops etc.
C +-----------------------------------------------------------------
C | Program: Bus number advice
C | Author: Abraham Agay
C | Date: 28.11.1995
C +-----------------------------------------------------------------
PROGRAM IdExmp
C ------------------------------------------------------------------
INTEGER
* Bus_Num
C ==================================================================
Bus_Num = 99
WRITE(*,*) ' Take bus ', Bus_Num
C ==================================================================
END
Adding comments
---------------
A recommended way to add little comments to a FORTRAN program
is putting an '!' at column 73 and typing the comment after it.
Using '!' to begin a comment is recognized by many compilers,
and anything typed after column 72 is supposed to be ignored.
However, a strict-checking compiler (maybe with some option)
may flag such practice as a warning-level error.
Your editor should be enabled to display lines longer than 80
characters. Some people don't like to work with longer lines
because on text terminals you usually get smaller fonts.
This commenting style is portable IF YOU CONVERT ALL TABS TO SPACES.
programming styles
------------------
Most FORTRAN 77 compilers (and all Fortran 90) accept lowercase
letters, and treat them as equivalent to the corresponding
uppercase ones. This is useful, as lowercase letters are more
readable than uppercase.
You can write Fortran code in a way that reminds one of
C code:
integer function strlen(st)
integer i
character st*(*)
i = len(st)
do while (st(i:i) .eq. ' ')
i = i - 1
enddo
strlen = i
return
end
integer function strchr(st,ch)
integer len, i, strlen
character st*(*), ch*1
external strlen
len = strlen(st)
i = 1
do while ((i .le. len) .and. (st(i:i) .ne. ch))
i = i + 1
enddo
strchr = i
return
end
+------------------------------------------+
| IMPROVING PROGRAM LAYOUT IS IMPORTANT! |
+------------------------------------------+
A much more important factor in the social movement than those already mentioned was the ever-increasing influence of women. This probably stood at the lowest point to which it has ever fallen, during the classic age of Greek life and thought. In the history of Thucydides, so far as it forms a connected series of events, four times only during a period of nearly seventy years does a woman cross the scene. In each instance her apparition only lasts for a moment. In three of the four instances she is a queen or a princess, and belongs either to the half-barbarous kingdoms of northern Hellas or to wholly barbarous Thrace. In the one remaining instance208— that of the woman who helps some of the trapped Thebans to make their escape from Plataea—while her deed of mercy will live for ever, her name is for ever lost.319 But no sooner did philosophy abandon physics for ethics and religion than the importance of those subjects to women was perceived, first by Socrates, and after him by Xenophon and Plato. Women are said to have attended Plato’s lectures disguised as men. Women formed part of the circle which gathered round Epicurus in his suburban retreat. Others aspired not only to learn but to teach. Arêtê, the daughter of Aristippus, handed on the Cyrenaic doctrine to her son, the younger Aristippus. Hipparchia, the wife of Crates the Cynic, earned a place among the representatives of his school. But all these were exceptions; some of them belonged to the class of Hetaerae; and philosophy, although it might address itself to them, remained unaffected by their influence. The case was widely different in Rome, where women were far more highly honoured than in Greece;320 and even if the prominent part assigned to them in the legendary history of the city be a proof, among others, of its untrustworthiness, still that such stories should be thought worth inventing and preserving is an indirect proof of the extent to which feminine influence prevailed. With the loss of political liberty, their importance, as always happens at such a conjuncture, was considerably increased. Under a personal government there is far more scope for intrigue than where law is king; and as intriguers women are at least the209 equals of men. Moreover, they profited fully by the levelling tendencies of the age. One great service of the imperial jurisconsults was to remove some of the disabilities under which women formerly suffered. According to the old law, they were placed under male guardianship through their whole life, but this restraint was first reduced to a legal fiction by compelling the guardian to do what they wished, and at last it was entirely abolished. Their powers both of inheritance and bequest were extended; they frequently possessed immense wealth; and their wealth was sometimes expended for purposes of public munificence. Their social freedom seems to have been unlimited, and they formed combinations among themselves which probably served to increase their general influence.321 The old religions of Greece and Italy were essentially oracular. While inculcating the existence of supernatural beings, and prescribing the modes according to which such beings were to be worshipped, they paid most attention to the interpretation of the signs by which either future events in general, or the consequences of particular actions, were supposed to be divinely revealed. Of these intimations, some were given to the whole world, so that he who ran might read, others were reserved for certain favoured localities, and only communicated through the appointed ministers of the god. The Delphic oracle in particular enjoyed an enormous reputation both among Greeks and barbarians for guidance afforded under the latter conditions; and during a considerable period it may even be said to have directed the course of Hellenic civilisation. It was also under this form that supernatural religion suffered most injury from the great intellectual movement which followed the Persian wars. Men who had learned to study the constant sequences of Nature for themselves, and to shape their conduct according to fixed principles of prudence or of justice, either thought it irreverent to trouble the god about questions on which they were competent to form an opinion for themselves, or did not choose to place a well-considered scheme at the mercy of his possibly interested responses. That such a revolution occurred about the middle of the fifth century B.C., seems proved by the great change of tone in reference to this subject which one perceives on passing from Aeschylus to Sophocles. That anyone should question the veracity of an oracle is a supposition which never crosses the mind of the elder dramatist. A knowledge of augury counts among the greatest benefits222 conferred by Prometheus on mankind, and the Titan brings Zeus himself to terms by his acquaintance with the secrets of destiny. Sophocles, on the other hand, evidently has to deal with a sceptical generation, despising prophecies and needing to be warned of the fearful consequences brought about by neglecting their injunctions. The stranger had a pleasant, round face, with eyes that twinkled in spite of the creases around them that showed worry. No wonder he was worried, Sandy thought: having deserted the craft they had foiled in its attempt to get the gems, the man had returned from some short foray to discover his craft replaced by another. “Thanks,” Dick retorted, without smiling. When they reached him, in the dying glow of the flashlight Dick trained on a body lying in a heap, they identified the man who had been warned by his gypsy fortune teller to “look out for a hidden enemy.” He was lying at full length in the mould and leaves. "But that is sport," she answered carelessly. On the retirement of Townshend, Walpole reigned supreme and without a rival in the Cabinet. Henry Pelham was made Secretary at War; Compton Earl of Wilmington Privy Seal. He left foreign affairs chiefly to Stanhope, now Lord Harrington, and to the Duke of Newcastle, impressing on them by all means to avoid quarrels with foreign Powers, and maintain the blessings of peace. With all the faults of Walpole, this was the praise of his political system, which system, on the meeting of Parliament in the spring of 1731, was violently attacked by Wyndham and Pulteney, on the plea that we were making ruinous treaties, and sacrificing British interests, in order to benefit Hanover, the eternal millstone round the neck of England. Pulteney and Bolingbroke carried the same attack into the pages of The Craftsman, but they failed to move Walpole, or to shake his power. The English Government, instead of treating Wilkes with a dignified indifference, was weak enough to show how deeply it was touched by him, dismissed him from his commission of Colonel of the Buckinghamshire Militia, and treated Lord Temple as an abettor of his, by depriving him of the Lord-Lieutenancy of the same county, and striking his name from the list of Privy Councillors, giving the Lord-Lieutenancy to Dashwood, now Lord Le Despencer. "I tell you what I'll do," said the Deacon, after a little consideration. "I feel as if both Si and you kin stand a little more'n you had yesterday. I'll cook two to-day. We'll send a big cupful over to Capt. McGillicuddy. That'll leave us two for to-morrer. After that we'll have to trust to Providence." "Indeed you won't," said the Surgeon decisively. "You'll go straight home, and stay there until you are well. You won't be fit for duty for at least a month yet, if then. If you went out into camp now you would have a relapse, and be dead inside of a week. The country between here and Chattanooga is dotted with the graves of men who have been sent back to the front too soon." "Adone do wud that—though you sound more as if you wur in a black temper wud me than as if you pitied me." "Wot about this gal he's married?" "Don't come any further." "Davy, it 'ud be cruel of us to go and leave him." "Insolent priest!" interrupted De Boteler, "do you dare to justify what you have done? Now, by my faith, if you had with proper humility acknowledged your fault and sued for pardon—pardon you should have had. But now, you leave this castle instantly. I will teach you that De Boteler will yet be master of his own house, and his own vassals. And here I swear (and the baron of Sudley uttered an imprecation) that, for your meddling knavery, no priest or monk shall ever again abide here. If the varlets want to shrieve, they can go to the Abbey; and if they want to hear mass, a priest can come from Winchcombe. But never shall another of your meddling fraternity abide at Sudley while Roland de Boteler is its lord." "My lord," said Edith, in her defence, "this woman has sworn falsely. The medicine I gave was a sovereign remedy, if given as I ordered. Ten drops would have saved the child's life; but the contents of the phial destroyed it. The words I uttered were prayers for the life of the child. My children, and all who know me, can bear witness that I have a custom of asking His blessing upon all I take in hand. I raised my eyes towards heaven, and muttered words; but, my lord, they were words of prayer—and I looked up as I prayed, to the footstool of the Lord. But it is in vain to contend: the malice of the wicked will triumph, and Edith Holgrave, who even in thought never harmed one of God's creatures, must be sacrificed to cover the guilt, or hide the thoughtlessness of another." "Aye, Sir Treasurer, thou hast reason to sink thy head! Thy odious poll-tax has mingled vengeance—nay, blood—with the cry of the bond." HoME古一级毛片免费观看
ENTER NUMBET 0017 www.nihe9.net.cn zumao0.net.cn maoni2.net.cn lajue7.net.cn www.jueni7.net.cn laoer4.net.cn www.76756.com.cn www.aqbe.com.cn 008fei.net.cn www.520ka.com.cn