Information Technology Topic
Program Documentation
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- Documentation explains a program to the people who use it or who will change it later; it is completed after testing and does not change what the program does
- Internal documentation is inside the source code: mnemonic (meaningful) names, comments, indentation and white space
- External documentation is separate from the code, such as a user manual that tells users how to start the program, enter data and read the results
Section 1What documentation is and why it matters
Documentation is the explanation that goes with a program. It is written for people, not for the computer. The compiler does not need any of it to translate the program. So why bother? Because programs are read far more often than they are written. A program in a Mandeville business may be used for years. Someone has to fix a fault, add a new price or change a rule, and that person may be a different programmer, or you, a year later who has forgotten how it worked. A program with good documentation costs less to maintain, because the reader can see what each part does without working it out from scratch. Documentation is kept up from the start of a project, but it is completed after the program has been tested, so it describes the program as it really behaves. It should also keep a record of the changes made to the program. Here's the trick: write for a reader who is clever but has never seen your program.
Section 2Internal and external documentation
There are two kinds. Internal documentation is inside the source code. It has four features: mnemonic names, comments, indentation and white space. External documentation is separate from the code. The example in the syllabus is the user manual. Easy to mix up: internal documentation is read by the programmer looking at the code. External documentation is read by the user, who may never see the code.
Section 3Mnemonic names
A mnemonic name hints at what it stands for. A variable that holds the total of the fines should be called totalFine, not t or x. Compare these Pascal-style lines. They do exactly the same thing: c := a * b cost := tickets * price The second line can be understood without reading anything else. Choose names that say what the storage holds, and use the same name every time. A named constant such as finePerDay is better than typing 20 in many places, because the name says what the 20 means.
Section 4Comments
A comment is text in the program that the compiler ignores. It is there for human readers only, so it has no effect on the output. Pascal-style comments can be written in three ways: { this comment can run over several lines } (* so can this one *) // this one runs to the end of the line The first two are the older forms. The // form came in later with Delphi and Free Pascal accepts it, so if a compiler rejects //, use { }. Use comments to say what the program is for (usually in a block at the top), to summarise a section, and to explain a step that is not obvious. A comment can sit on the line before the code it describes or at the end of the same line. Do not comment the obvious. { add 1 to count } beside count := count + 1 tells the reader nothing new. { one more pupil has passed } says why. Watch out: a comment that is no longer true after the code changes is worse than no comment. And a { comment with no closing } swallows the code after it, so the compiler reports a syntax error. Code inside a comment does not run: count := 1; { count := 5; } writeln(count) This Pascal-style piece displays 1.
Section 5Indentation and white space
Indentation means starting lines further to the right so the structure shows: the statements inside a loop or between begin and end are pushed in. White space means spaces and blank lines: spaces round := and +, one statement per line, and a blank line between the declarations and the main part. The compiler ignores layout, so a program with no layout works exactly like a neat one. This Pascal-style program is correct but very hard to read: program p; var a:integer; b:integer; c:integer; begin readln(a); readln(b); c:=a*b; writeln('Cost: ',c) end. With the inputs 4 and 350 it displays Cost: 1400, and so does the neat version in the next note. The poor version has one-letter names, no comments, no indentation and no blank lines. It is hard to see what a, b and c mean or where the main part begins.
Section 6A fully documented program
Here is the same job written with all four internal features, in Pascal-style code: { Program: TicketCost Purpose: reads the number of tickets and the price of one ticket, then shows the total cost. } program TicketCost; var tickets : integer; price : integer; cost : integer; begin readln(tickets); // how many tickets are bought readln(price); // price of one ticket in dollars cost := tickets * price; writeln('Cost: ', cost) end. Mnemonic names: tickets, price, cost. Comments: a block at the top that says what the program is for, and two end-of-line comments. Indentation: the declarations and the statements between begin and end are pushed in. White space: a blank line separates the declarations from the main part, and there are spaces round := and *. With 4 tickets at 350 dollars it displays Cost: 1400, the same as the poor version.
Section 7External documentation: the user manual
A user manual is written for the people who use the program, in plain language. It stays separate from the source code, and the user may never see the code at all. A useful user manual says: - what the program is for - what is needed to run it, and how to start it - how to enter the data, step by step, with an example - what the results look like and what they mean - what the error messages mean and what to do about them For a ticket-selling program in Montego Bay, the manual would tell the clerk how to start the program, what to type for the number of tickets and the price, and how to read the cost on the screen. The developer completes the documentation after testing the program, so it describes how the program really works.
Examples
A clerk in Montego Bay sells tickets. This Pascal-style program is written for her. { Program: TicketCost Purpose: reads the number of tickets and the price of one ticket, then shows the total cost. } program TicketCost; var tickets : integer; price : integer; cost : integer; begin readln(tickets); // how many tickets are bought readln(price); // price of one ticket in dollars cost := tickets * price; writeln('Cost: ', cost) end. (a) Identify one example of each of the four features of internal documentation. (b) The values typed are 4 and 350. State what is displayed. (c) State whether deleting all the comments would change the output, and why. (d) State one piece of external documentation the clerk would need.
(a) Mnemonic names: tickets, price and cost. Comments: the block at the top, or // how many tickets are bought. Indentation: the lines inside var and between begin and end are pushed in. White space: the blank line before begin, and the spaces round := and *. (b) Cost: 1400, because 4 * 350 = 1400. (c) No. The compiler ignores comments, so the program does exactly the same. They are there for human readers. (d) A user manual that tells the clerk how to start the program, what to type for the number of tickets and the price, and how to read the cost.
A school library in Kingston adds up fines for three overdue books at 20 dollars per day late. A pupil wrote this Pascal-style program and typed 2, 5 and 3 as the days late. program f; var d:integer; t:integer; n:integer; begin t:=0; for n:=1 to 3 do begin readln(d); t:=t+d*20 end; writeln(t) end. (a) State three problems that make the program hard to read. (b) State what the program displays. (c) Rewrite the program with internal documentation and say what it displays. (d) State two items the librarian's user manual should contain.
(a) The names f, d, t and n say nothing about what they hold. There are no comments to say what the program is for or what 20 means. There is no indentation and no blank line, so it is hard to see what the loop repeats. (b) 200, because (2 + 5 + 3) * 20 = 200. (c) One possible rewrite: { Program: LibraryFines Purpose: adds up the fines for 3 overdue books at 20 dollars per day late. } program LibraryFines; const finePerDay = 20; var book : integer; daysLate : integer; totalFine : integer; begin totalFine := 0; for book := 1 to 3 do begin readln(daysLate); // days this book is overdue totalFine := totalFine + daysLate * finePerDay end; writeln(totalFine) end. It also displays 200, because documentation does not change what a program does. (d) For example: how to start the program and what to type for each book's days late, and what the number shown at the end means.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 3, Specific Objective 11 · Syllabus scope
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 1, Comments (1.2; the forms { }, (* *) and // are discarded by the compiler), checked against (https://www.freepascal.org/docs-html/ref/refse2.html) · Facts
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Chapter 3, An Informal Introduction to Python (language-neutral: a comment is text the language processor ignores and it can follow code on the same line), checked against (https://docs.python.org/3/tutorial/introduction.html) · Facts
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Chapter 4, Intermezzo: Coding Style (language-neutral: indentation shows which statements belong to which block and blank lines separate larger blocks), checked against (https://docs.python.org/3/tutorial/controlflow.html) · Facts
- Navy Electricity and Electronics Training Series (NEETS), Module 22, Introduction to Digital Computers (NAVEDTRA 14194, September 1998) · US Navy (Naval Education and Training Professional Development and Technology Center) · Chapter 3, Documentation (pp. 3-21 to 3-22; documentation is a continuing record of the program, its test data and results and a history of changes, plus operator instructions, and is completed before the program goes into use; 1998 US Navy text), checked against (https://vtda.org/bits/other/US_Navy/NEETS/US%20Navy%20NEETS%20-%20Module%2022-Introduction%20to%20Digital%20Computers%20NAVEDTRA%2014194.pdf) · Facts