home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson17.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  30.8 KB  |  690 lines

  1. <COMMENT This is a lesson file for the Lovelace Ada tutorial>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic & other WWW browsers.>
  4.  
  5. <COMMENT  Edit the following lines. >
  6. <TUTOR NAME="Lovelace">
  7. <LESSON NUMBER=17>
  8. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  9. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  10. <COMMENT $Id$ >
  11.  
  12. <COMMENT  You'll probably want to uncomment and edit these lines: >
  13. <COMMENT  <PREVIOUS_LESSON LOCATION="URL_of_directory/" >
  14. <COMMENT  <NEXT_LESSON LOCATION="URL_of_directory/" >
  15.  
  16. <COMMENT A lesson is divided into 1 or more "sections".>
  17. <COMMENT Each section has a title; SECTION starts a new section.>
  18.  
  19. <SECTION NAME="Language-Defined Attributes">
  20. Ada predefines, for each type, a number of operations and values termed
  21. "language defined attributes".
  22. To use them, enter the name of the type (or object) the attribute
  23. applies to, a tick (a single apostrophe), and the name of the attribute.
  24. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-K.html">The
  25. LRM appendix K lists all of the language defined attributes
  26. and their definitions.</A>
  27. <P>
  28. Actually, we've already seen one attribute: 'Class.
  29. Given some type named X, the phrase X'Class
  30. refers to the class of all types that are descended from X, including X.
  31. <P>
  32. Three very common attributes are 'First, 'Last, and 'Range.
  33. Given some type named X,
  34. the phrase X'First is the <EM>first</EM> legal value of type X.
  35. Attribute 'Last refers to the <EM>last</EM> legal value of a given type.
  36. Attribute 'Range refers to the range between 'First and 'Last, including
  37. them, and is often used in loops.
  38. For example, if Repair_Status is an enumerated type with many values,
  39. you could write a loop as follows:
  40. <P>
  41. <PRE>
  42.   for I in Repair_Status'Range loop
  43.     -- <EM>Do something here.</EM>
  44.   end loop;
  45. </PRE>
  46. <P>
  47. Some attributes are actually subprograms that accept parameters,
  48. which you can call the way you'd call any other subprogram.
  49. One simple attribute is X'Image; this is a subprogram that takes
  50. a value of the type X and returns a String representing that value.
  51. X must be a scalar type (i.e. Integer, Float, or an enumerated value).
  52. Here's an example:
  53. <P>
  54. <PRE>
  55.   procedure Demo( Value : Integer) is
  56.     Text_Rep : String := Integer'Image(Value);
  57.   begin
  58.     Put(Text_Rep);
  59.   end Demo;
  60. </PRE>
  61. <P>
  62. Here are some other useful attributes:
  63. <UL>
  64. <LI>The reverse of 'Image is 'Value, which takes a String and returns
  65. its scalar value.
  66. <LI>Attribute X'Val takes an Integer and returns a value
  67. of type X whose position number is the same as the integer.
  68. This is handy for Characters.
  69. For example, since a space is position 32 in the ASCII and Latin-1
  70. character sets, Character'Val(32) is a space.
  71. The reverse of 'Val is 'Pos.
  72. <LI>Attribute X'Access, where X is a subprogram name,
  73. yields an access value ``pointing'' to X.
  74. <LI>Attributes X'Min and X'Max take two scalars of type X
  75. and return the minimum ('Min) or maximum ('Max) value.
  76. <LI>Attribute X'Round rounds a number of type X
  77. to the nearest integer; if it's exactly between two integers,
  78. it rounds away from zero.
  79. <LI>Several attributes give information on the underlying machine.
  80. These include 'Size (which gives the number of bits used to store
  81. something), 'Address (which gives the storage address),
  82. and 'Bit_Order (which gives the bit ordering).
  83. </UL>
  84. <P>
  85. There are a several other attributes as well.
  86. <P>
  87. Some attributes can be set by the programmer.
  88. This is mainly used for
  89. supporting low-level facilities, such as making variables refer to hardware
  90. interfaces (by setting 'Address) or setting the size of given type.
  91. The Ada 95 syntax for doing this can be represented as:
  92. <PRE>
  93.   for name'attribute_name use expression;
  94. </PRE>
  95. <!-- Not using BNF, since the tick may confuse the reader -->
  96. <P>
  97. For example, if your machine can read raw temperatures
  98. as an 8-bit value from address FFFF_0000, you can read temperatures
  99. just by reading the variable "Current_Temperature" by telling Ada
  100. to place Current_Temperature at a specific address.
  101. Here's an example of how to do that:
  102. <PRE>
  103.   type Temperature_Reading is 0 .. 2**8 - 1;
  104.   for Temperature_Reading'Size use 8;
  105.  
  106.   Current_Temperature : Temperature_Reading;
  107.   for Current_Temperature'Address use 16#FFFF_0000#;
  108.   pragma Volatile(Current_Temperature);  -- We haven't discussed this.
  109.   -- Now just read from "Temperature" as a variable.
  110. </PRE>
  111. <P>
  112. Although an Ada compiler can handle spaces before and after the tick ('),
  113. don't place any spaces around it.
  114. That way, tools which don't actually parse Ada (such as semi-smart
  115. editors and pretty printers) can tell the difference between
  116. attributes and character constants.
  117.  
  118. <QUESTION Type=Multiple-Choice>
  119. What would find the smallest value of two Integers A and B, using
  120. a predefined attribute?
  121. <CHOICES>
  122. <CHOICE ANS=1>Min(A,B)
  123. <CHOICE ANS=2>Integer'Min(A,B)
  124. </CHOICES>
  125. <ANSWER ANS=2>
  126. <RESPONSES>
  127. <WHEN ANS=1>
  128. Sorry.
  129. There wasn't a tick (') there, so there's no predefined attribute
  130. in use.
  131. The phrase Min(A,B) would just call some used-defined subprogram named
  132. Min, not the predefined subprogram.
  133. </RESPONSES>
  134.  
  135.  
  136. <SECTION NAME="Efficiency">
  137. In many circumstances it's important to maximize program efficiency, i.e.
  138. your program's execution time and/or memory utilization.
  139. <P>
  140. A trivial way to improve efficiency (also called performance)
  141. is to set your compiler options to aggressively optimize your program.
  142. This is an easy way to gain efficiency, since it takes only a few
  143. moments, doesn't change your source code at all, and most compilers
  144. don't turn on their most aggressive optimizations unless you request them.
  145. <P>
  146. Before using any other efficiency improvement technique,
  147. <EM>measure</EM> to see what uses most of the current resources.
  148. Most programs spend most of their time in a very small portion of the
  149. entire program - thus, if you want to improve execution time, you must
  150. spend your time working on those small portions.
  151. It's important to measure, because
  152. programmers often guess incorrectly on where most of the time is being spent.
  153. <P>
  154. The <EM>most effective efficiency improvement method</EM> is
  155. usually changing the algorithm (approach) used to solve the problem.
  156. <P>
  157. Jon Bentley has written two good books on general efficiency
  158. improvement techniques, titled
  159. <A HREF="biblio.htm#bentley1982"><EM>Writing Efficient Programs</EM></A>
  160. and
  161. <A HREF="biblio.htm#bentley1986"><EM>Programming Pearls</EM></A>.
  162. <P>
  163. Here are some Ada-specific capabilities for improving efficiency (performance):
  164. <OL>
  165. <LI><A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-06-03-02.html">Pragma
  166. Inline</A> specifies that the code for a particular subprogram should be
  167. included immediately inline
  168. rather than performing a normal subprogram call (with its associated overhead).
  169. This can be beneficial for simple subprograms with only a few lines of code.
  170. <LI><A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-02-08.html#23">Pragma
  171. Optimize</A> lets you specify if you want to optimize for speed
  172. or memory space.
  173. <LI><A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-11-05.html">Pragma
  174. Suppress</A> lets you suppress various run-time checks.
  175. It's best to make sure your program works <EM>before</EM> suppressing
  176. run-time checks.
  177. You may want to only suppress selected checks for selected types or
  178. subprograms, which will let you keep most of Ada's safety features.
  179. Make sure that your program doesn't depend on run-time checks before you
  180. use pragma Suppress.
  181. For example, don't try to handle an exception for
  182. dividing by zero if you might suppress that check - instead,
  183. check if a value is zero before using it as a divisor.
  184. <LI><A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-13-12.html">Pragma
  185. Restrictions</A> restricts the Ada program from using selected Ada capabilities.
  186. In some cases, the Ada compiler may produce faster and/or smaller
  187. code if it knows (via this pragma) that certain capabilities won't
  188. be used, as discussed in the
  189. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/Rationale/rat95html/rat95-p3-d.html#7">Ada
  190. Rationale part III, D.7</A>.
  191. <LI><A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-13-02.html">Pragma
  192. Pack</A> can be used to decrease the amount of space used by
  193. a compound type (i.e. array or record).
  194. Note, however, that on some machines pragma Pack can slow
  195. the execution of a program down (due to packing and unpacking of bit strings).
  196. <LI>Like all languages except Fortran, when using multi-dimension arrays, vary
  197. the last dimension fastest.
  198. If you want to vary the first dimension fastest (say, if you're
  199. transliterating Fortran code),
  200. use "pragma Convention(Fortran, X)" where X is the array type.
  201. <LI>Types whose sizes are known at compile time (called constrained types)
  202. can be passed around more quickly than types whose sizes are not known
  203. (these are called unconstrained types).
  204. This is because for unconstrained types the Ada compiler must pass around
  205. their bounds as well as their data.
  206. This is <EM>especially</EM> true when returning an unconstrained type
  207. as a function return value; for technical reasons this is a relatively
  208. expensive operation to perform.
  209. Examples of unconstrained types include type String and any array type
  210. that isn't given an explicit bound at compile time.
  211. Examples of constrained types include Integer, Float, any access type,
  212. fixed-size arrays, and fixed-size records.
  213. </OL>
  214. <P>
  215. Some performance improvement suggestions can be found in
  216. <A HREF="http://lglwww.epfl.ch/Ada/FAQ/programming.html#slow">the
  217. "Ada Programmer Frequently-Asked Questions (FAQ)"</A> file.
  218. Many more suggestions
  219. can be found in the
  220. <A HREF="ftp://sw-eng.falls-church.va.us/public/AdaIC/docs/style-guide/95style/ps/perf.ps">Performance
  221. chapter of the AQ&S guide</A>.
  222.  
  223. <QUESTION Type=Multiple-Choice>
  224. Which of the following techniques is likely to produce the most
  225. significant performance improvement?
  226. <CHOICES>
  227. <CHOICE ANS=1>Use pragma Optimize and compiler flags to increase optimization.
  228. <CHOICE ANS=2>Use pragma Suppress to turn off run-time checks.
  229. <CHOICE ANS=3>Change the way the problem is solved.
  230. </CHOICES>
  231. <ANSWER ANS=3>
  232. <RESPONSES>
  233. <WHEN ANS=3>
  234. Right.
  235. It's easier to say this than to do it, of course, and sometimes
  236. there simply isn't a (known) better way.
  237. In that case, judicious use of these other techniques can usually improve
  238. performance with little cost and significant results.
  239. The best results are obtained with a combination of all of these
  240. techniques - good algorithms, compiler optimizations,
  241. various hints to the compiler (like pragma Inline), and suppressing
  242. selected checks (such as pragma Suppress) where it's known that
  243. problems can't occur.
  244. </RESPONSES>
  245.  
  246. <SECTION NAME="Safety and Ada">
  247. While Ada is used in applications where safety isn't a
  248. significant concern,
  249. many safety-critical applications are developed in Ada.
  250. Even if you're not planning to build safety-critical software
  251. today, it's good to know some basics.
  252. First we'll introduce some software safety concepts, then
  253. follow them with Ada-specific items.
  254. <P>
  255. <H2>Software Safety Overview</H2>
  256. <P>
  257. Software safety involves ensuring that software executes within a
  258. system context without resulting in an unacceptable system risk. Safety
  259. is a property of an entire system, not just software, but software
  260. components can be a determiner of a system's safety.
  261. Risk may be defined as a function of (1) the probability of a mishap and
  262. (2) the severity of a mishap's effects should the mishap occur.
  263. Few things in real life can be "perfectly" safe; instead, we try to reduce
  264. the risk to some very small, acceptable level.
  265. <P>
  266. One common misconception is that a well-tested, highly reliable
  267. software system is safe. It has been demonstrated experimentally that
  268. traditional verification (testing) techniques are grossly inadequate for
  269. detecting safety-critical faults even for a simple program
  270. <A HREF="biblio.htm#gowen1994">[Gowen 1994]</A>.
  271. Thus, simply testing a system is unlikely to result in a safe system.
  272. A reliable system isn't necessarily a safe system either; a
  273. system that works most of the time but occasionally kills everyone
  274. in a large radius would usually be considered unsafe.
  275. The moderated newsgroup
  276. <A HREF="news:comp.risks">comp.risks</A>
  277. provides a continuous stream of examples of
  278. software going awry and causing serious problems.
  279. <P>
  280. Personally, I think that if you're developing software that could
  281. cause death, serious injury, or significant property damage, you
  282. should be just a little afraid.
  283. If you aren't, you probably
  284. don't understand the issues involved.
  285. That small amount of fear is healthy, because it will motivate you
  286. to learn and use techniques to reduce the overall risk.
  287. <P>
  288. Some good survey papers that give an overview of the software safety
  289. field include
  290. <A HREF="biblio.htm#place1993">Place [1993]</A>,
  291. <A HREF="biblio.htm#leveson1991a">Leveson [1991a]</A>,
  292. and
  293. <A HREF="biblio.htm#leveson1986">Leveson [1986]</A>,
  294. Note that
  295. <A HREF="ftp://ftp.sei.cmu.edu/pub/documents/93.reports/tr05.93.ps">[Place 1993]
  296. is freely available through the Internet</A>.
  297. <P>
  298. There are a number of specialized hazard analysis techniques that help to
  299. identify safety problems.
  300. Here are two of them:
  301. <OL>
  302. <LI>Software Fault Tree Analysis (FTA)
  303. is a technique for identifying potential causes of safety
  304. hazards in a system.
  305. In software FTA, a list of safety hazards (conditions to be avoided) is
  306. made; then the analyst works backwards from those hazards through the software,
  307. creating logic diagrams, to show that the safety hazards cannot occur.
  308. The technique is easy to understand and can be easily integrated
  309. with system safety work.
  310. <A HREF="biblio.htm#leveson1983">Leveson [1983]
  311. provides a general discussion on software FTA</A>, while
  312. <A HREF="biblio.htm#leveson1991b">Leveson [1991b]
  313. shows specifically how to apply software FTA to
  314. Ada programs</A>.
  315. <LI>Another set of safety-related techniques are
  316. called formal methods.
  317. Formal methods are the application
  318. of (formal) mathematical techniques for definition and possibly proof of
  319. program properties.
  320. Currently formal methods are a subject of a great deal of research,
  321. but they are not often used in practice due to the difficulty of
  322. applying them to realistic program sizes.
  323. Still, there are systems which have used formal methods to varying degrees,
  324. and there is reason to hope that their applicability will increase
  325. over time.
  326. <A HREF="http://www.yahoo.com/Science/Computer_Science/Formal_Methods/">The
  327. Internet directory YAHOO has several references to
  328. information relating to formal methods</A>.
  329. Well-known specification languages include VDM and
  330. <A HREF="http://www.cis.ohio-state.edu/hypertext/faq/usenet/z-faq/faq.html">Z
  331. (pronounced "zed")</A>.
  332. </OL>
  333. <P>
  334. One web server on safety is the
  335. <A HREF="http://www.comlab.ox.ac.uk/archive/safety.html">The
  336. World Wide Web Virtual Library: Safety-Critical Systems</A>.
  337. This web server emphasizes the formal methods aspects, but does cover
  338. others to some extent.
  339. <P>
  340. <H2>Ada and Software Safety</H2>
  341. <P>
  342. Ada is often used in safety-critical software because of its built-in
  343. capabilities.
  344. Here are some Ada capabilities specifically for safety-critical systems:
  345. <OL>
  346. <LI>In some circumstances it's important to check to see if a scalar
  347. value (such as an Integer or enumerated type) is in a legal
  348. range.
  349. For example, hardware errors and cosmic rays can cause values to change
  350. outside of software control, and foreign language interfaces can pass
  351. in out-of-range values.
  352. Ada 95 has an attribute named 'Valid to check if a value is out-of-range
  353. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-13-09-02.html">('Valid
  354. is explained in LRM 13.9.2)</A>.
  355. The expression
  356. X'Valid, where X is some scalar variable, is True if X is in-range
  357. and False otherwise.
  358. <LI>In safety systems the computer language is often restricted to constructs
  359. that are easier to show correct (either for formal methods or just to
  360. avoid potential problems).
  361. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-13-12.html">Pragma
  362. Restrictions (see LRM 13.12)</A>
  363. can be used to specify what parts of the language
  364. may not be used in a particular program.
  365. <LI>Ada 95 has an optional
  366. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-H.html">"Safety and
  367. Security" annex</A>
  368. which, if included in the compiler, adds additional operations and features
  369. to support safe and/or secure program development.
  370. For example, this annex adds pragma Normalize_Scalars, which sets
  371. uninitialized values to out-of-range values where possible (this makes
  372. it easier to detect uninitialized values).
  373. </OL>
  374. <P>
  375. Here are two examples of safety-restricted Ada subsets:
  376. <OL>
  377. <LI>SPARK is a `safe' subset of Ada 83 designed to be
  378. susceptible to formal methods, accompanied with a set of approaches and tools.
  379. Using SPARK,
  380. a developer takes a Z specification and performs a stepwise
  381. refinement from the specification to SPARK code. For each refinement
  382. step a tool is used to produce verification conditions (VC's), which are
  383. mathematical theorems. If the VC's can be proved then the refinement
  384. step will be known to be valid. However if the VC's cannot be proved
  385. then the refinement step may be erroneous.
  386. <A HREF="http://daedalus.dra.hmg.gb/hewitt/swi/swi.html">More
  387. information about SPARK is available on the WWW</A>.
  388. <LI><A HREF="biblio.htm#pyle1991">Pyle [1991]</A>
  389. discusses and compares various restrictions for safety purposes in his book.
  390. </OL>
  391.  
  392. <QUESTION Type=Multiple-Choice>
  393. Given the following two statements:
  394. <P>
  395. <OL>
  396. <LI>If a program is reliable and has been extensively tested, it's safe.
  397. <LI>The safety implications of software can be analyzed by itself,
  398. without examining how the software will be used.
  399. </OL>
  400. <P>
  401. Which is true?
  402.  
  403. <CHOICES>
  404. <CHOICE ANS=1>Statement 1.
  405. <CHOICE ANS=2>Statement 2.
  406. <CHOICE ANS=3>Both statement one and statement two.
  407. <CHOICE ANS=4>Neither statement.
  408. </CHOICES>
  409. <ANSWER ANS=4>
  410. <RESPONSES>
  411. <WHEN ANS=1>
  412. Sorry, that's a common misconception but it's not true.
  413. A program can work most of the time (be reliable), be extensively tested,
  414. and yet be unsafe:
  415. <UL>
  416. <LI>
  417. Reliability does not imply safety.
  418. For example, if there were only a one in a million chance per hour that
  419. a program would do the wrong thing, but that wrong thing would kill
  420. a million people, most would agree that the software is "unsafe".
  421. It is notoriously difficult to quantify the probability of a piece of
  422. software doing the wrong thing, since such quantification usually leaves
  423. out important possibilities, so even when someone says "only one in a million"
  424. the actual probabilities are usually much greater.
  425. <LI>
  426. "Extensive testing" is always an illusion -
  427. it's impossible to test most real programs for all possible circumstances,
  428. so testing only handles a very small subset of the actual situations the program
  429. will encounter.
  430. Trivial programs that have only ten 16-bit integers have 2^160 different
  431. possible states; such a trivial program couldn't be totally
  432. tested in the lifetime of the universe.
  433. </UL>
  434. <WHEN ANS=2>
  435. No, software must be analyzed in the context of the system it is in.
  436. The same component can be perfectly safe in one environment (for example,
  437. be part of a system that cannot cause safety problems) yet be part of a
  438. deadly hazard in another system.
  439. <WHEN ANS=4>
  440. Right.
  441. If you are asked to develop a safety-critical system,
  442. <EM>please</EM> go and learn from the literature about the
  443. field of software and system safety,
  444. the software tools you'll be using to do the job,
  445. and the general system and environment you'll be a part of.
  446. <P>
  447. Ada provides some useful mechanisms to help, but they're only a small part of
  448. the total solution; to develop successful systems you'll need to
  449. understand software safety much more deeply.
  450. </RESPONSES>
  451. <SECTION NAME="Software Inspections/ Reading Bugs Out">
  452. While Ada can prevent a number of software defects,
  453. no language can remove them all.
  454. One approach to increasing software quality, and reducing development
  455. cost and development time, is called <EM>software inspection</EM>.
  456. A software inspection is a rigorous review of a product by a small group of
  457. peers for the purpose of detecting defects.
  458. The work products reviewed are small - for code, up to about 250 lines would
  459. be considered in one inspection.
  460. <P>
  461. <CENTER><IMG ALIGN=right SRC="inspect.gif" ALT="Inspection Process" WIDTH=300 HEIGHT=300></CENTER>
  462. In an inspection,
  463. each person ("inspector") is given a role during the initial planning stage,
  464. such as "moderator" (the moderator
  465. controls the inspection and is not the author).
  466. After an optional overview of the product given by the author,
  467. each inspector prepares by
  468. carefully reading the product so they completely understand it
  469. (this generally takes 1-4 hours).
  470. The inspectors then meet together for no more than
  471. two hours to detect defects together (the list is recorded for later use).
  472. Optionally, they may meet afterwards for a "third hour" to discuss
  473. possible improvements and other things not related to detecting defects.
  474. The author then goes off to fix ("rework") the defects.
  475. After the author is done, the moderator checks to make sure the fixes are
  476. okay and a reinspection may occur if necessary.
  477. Occasionally a "causal analysis" process should occur to determine common
  478. defects, their causes, and how to eliminate those causes.
  479. <P>
  480. Obviously, this is a pretty rigorous process.
  481. The amazing thing is that many people have documented that
  482. inspections actually reduced their total time and cost, as well as increased
  483. the resulting quality, because inspections can reduce the cost
  484. of errors and rework.
  485. Inspections aren't technically glitzy, but
  486. results are usually more important than glitz.
  487. <P>
  488. One good way to find more about inspections is to buy my
  489. book on inspections (how's that for a plug?).
  490. It's titled
  491. <A HREF="biblio.htm#wheeler1996"><EM>Software Inspection:
  492. An Industry Best Practice</EM>, by
  493. David A. Wheeler, Bill Brykczynski, and Reg Meeson</A>;
  494. it's published by IEEE.
  495. Many papers are available on inspections; one oft-referenced paper is
  496. <A HREF="biblio.htm#fagan1986">Michael Fagan's 1986 paper</A>.
  497. Some information on inspections is available on-line; see the
  498. <A HREF="http://www.ics.hawaii.edu/~johnson/FTR/">WWW
  499. Formal Technical Review Archive</A>
  500. and the
  501. <A HREF="http://www.ics.hawaii.edu/~siro/">Software
  502. Inspection and Review Organization (SIRO)</A>.
  503. <P>
  504. <H2>Ada and Reading Out Bugs</H2>
  505. <P>
  506. If you're part of an inspection of Ada code,
  507. or simply reading your own code looking
  508. for likely defects, it can help to know what are the "more common" errors
  509. of Ada programmers.
  510. In an inspection such a list is called a "checklist".
  511. I'm not aware of any publically-distributable empirical data to support
  512. an Ada checklist, but based on anecdotal information
  513. (such as John B. Goodenough's list of common Ada programming errors).
  514. <!-- "A Sample of Ada Programming Errors", John B. Goodenough, -->
  515. <!-- Wang Institute of Graduate Studies, Tyng Road, Tyngsboro, MA 01879 -->
  516. <!-- The only version I've found is dated March 6, 1986 Interim Draft -->
  517. <!-- and I got it via the Internet through machines that no longer exist. -->
  518. I've come up with the following checklist.
  519. I strongly encourage you to update this checklist to your situation
  520. as you gain experience in determining common defects in your own code.
  521. <P>
  522. <B>Ada Checklist - Look For:</B>
  523. <P>
  524. <OL>
  525. <LI><I>Reading Uninitialized variables</I>.  Ada compilers often detect these,
  526.     but <EM>not</EM> always. Access values are always initialized, and
  527.     when creating your own types you can cause them to have initial values.
  528.     When declaring variables, it's often a good idea to give them a starting
  529.     value where than makes sense to avoid this problem.
  530.     There's been much discussion on whether Ada should even permit
  531.     uninitialized variables; the rationale permitting them is that
  532.     unnecessary initializing can cause a performance hit.
  533.     The safety and security annex adds pragma Normalize_Scalars, which sets
  534.     uninitialized values to out-of-range values where possible (this makes
  535.     it easier to detect uninitialized values).
  536. <LI><I>Off-by-one boundary conditions</I> (for loop conditions, array indexing,
  537.     and comparisons). This is the error of having almost, but not quite,
  538.     the right boundary. For example, did you use < when you meant <=?
  539.     Check all your comparisons and loop boundaries.
  540.     Ada will raise a run-time exception
  541.     if you attempt to access out-of-bounds array element,
  542.     which helps, but it's no help if you're only accessing an array subset.
  543. <LI><I>Access type (pointer) and storage management errors</I>
  544.     (especially boundary conditions like null lists).
  545.     Trying hiding access (pointer) values so only a small portion of your
  546.     program has to deal with them, use initialization/finalization
  547.     operations to manage your storage, and make sure you can handle "empty"
  548.     cases where you should.
  549. <LI><I>Incorrect return values handling</I>.
  550.     For example, if a function returns a range, make sure every value in the
  551.     range will be handled appropriately by your program.
  552. <LI><I>Incorrect special condition handling</I>.  Have you handled all cases?
  553.     If you're reading from a sensor, do you deal with bogus sensor values?
  554.     Do you handle all appropriate exceptions?
  555. <LI><I>Incorrect array bound handling</I>.
  556.     An array's lower bound is not always one, so use 'First, 'Last,
  557.     'Length, and 'Range when you're passed an array.
  558.     For example, passed strings may be slices, so the first element of
  559.     a String might not have the index value 1.
  560.     Do not assume that 'First and 'Last are equal for different parameters
  561.     or that they're the same as the base type,
  562.     and use S'Length (not S'Last) to find the length.
  563. <LI><I>Instantiated unconstrained arrays</I>. Arrays with large array
  564.     indices (like Integer or Positive), or records containing them, must
  565.     have their bounds set; few computers can have "Integer'Max" array elements.
  566. <LI><I>Missing "reverse" keyword in a backward "for" loop</I>.  You should say:
  567. <BR>
  568. <PRE>
  569.     for I in reverse 1..5
  570. </PRE>
  571. <LI><I>Tasks exposed to unexpected exceptions</I>.
  572.     If a task does not catch exceptions the task will terminate.
  573. <LI><I>Invalid fairness assumptions in tasking</I>.
  574.     Some tasking operations are not guaranteed to be "fair". For example,
  575.     in a selective wait with several open alternatives, Ada is free to pick
  576.     between any of them each time; it need not pick between them "fairly".
  577. </OL>
  578. <!-- Too complicated to explain: -->
  579. <!-- * When a generic library unit is instantiated inside a library -->
  580. <!-- unit or secondary unit, a pragma Elaborate should be used to ensure -->
  581. <!-- the generic unit body is elaborated before the instantiation. -->
  582. <!-- * Check uses of 'Val carefully if you use a -->
  583. <!--    representation specification; -->
  584. <!--    'Val doesn't return the -->
  585. <!--    representation specification value, but the position number. -->
  586. <P>
  587.  
  588. <QUESTION Type=Multiple-Choice>
  589. What is the purpose of an inspection?
  590. <CHOICES>
  591. <CHOICE ANS=1>Learn about a program and discuss possible improvements.
  592. <CHOICE ANS=2>Detect defects.
  593. <CHOICE ANS=3>Fix defects.
  594. <CHOICE ANS=4>Spend endless hours in useless meetings.
  595. </CHOICES>
  596. <ANSWER ANS=2>
  597. <RESPONSES>
  598. <WHEN ANS=1>
  599. No, that's not the purpose of an inspection.
  600. Those are often purposes of another review process called a
  601. <EM>walkthrough</EM>, which is not what we're talking about here.
  602. Inspectors certainly learn about a program, and improvements are often
  603. the result of an inspection, but that's not the primary purpose of
  604. an inspection.
  605. In fact, if you're spending a lot of time discussing possible improvements,
  606. you're not spending enough time to succeed at the
  607. primary purpose of an inspection.
  608. <WHEN ANS=2>
  609. Right.
  610. You may learn about a program, and possible improvements might be
  611. discussed in the "third hour" stage, but the primary purpose of an
  612. inspection is to detect defects.
  613. <WHEN ANS=3>
  614. No, not quite, though the distinction is subtle.
  615. It's true that the <EM>result</EM> of an inspection is a fixed product.
  616. However, fixing defects is considered the author's job; the inspectors do
  617. not try to figure out how to <EM>fix</EM> a given problem.
  618. You might say I'm really quibbling on this point, but there are serious
  619. problems with letting the inspectors try to fix the defects:
  620. <P>
  621. <OL>
  622. <LI>When a group tries to "fix" the problem it may focus on an obvious
  623. but not-quite-correct solution; solving problems is best left to the
  624. careful consideration of the original author.
  625. <LI>If time is spent trying to fix problems, there won't be time left
  626. to find defects in the first place.
  627. </OL>
  628. <P>
  629. If the inspectors have something to contribute to help fix the problem,
  630. it should be brought up during the "third hour" or after the main meeting
  631. as email or a one-on-one session with the author.
  632. <WHEN ANS=4>
  633. Well, if inspections are done poorly, that could very
  634. well be the result.
  635. That's not the purpose of an inspection though.
  636. Inspectors should be gathering data to show that they're actually
  637. saving time and money; if they aren't showing that, something is wrong and
  638. the process needs to be fixed.
  639. </RESPONSES>
  640.  
  641. <SECTION NAME="Other Ada Capabilities">
  642. Here are some other Ada capabilities that we haven't covered so far
  643. but which you may be interested in:
  644. <P>
  645. <OL>
  646. <LI>Ada 95 also provides a
  647. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-03-05-04.html">"modular"
  648. type</A>, which is an integer that varies
  649. from zero up to some integer <EM>modulus-1</EM>.
  650. Unlike a normal integer, if you add or subtract a value and the result
  651. doesn't fit, the result <EM>wraps around</EM>.
  652. For example, a modulus 4 value can have the values 0, 1, 2, and 3.
  653. If you added one to 3, you'd get 0 (due to wrapping around).
  654. Modular types don't need to be powers of two, but if they are the
  655. Ada compiler will select especially efficient operations to implement them.
  656. Bit manipulation operations, such as <EM>and</EM> and <EM>or</EM> can
  657. be used on modular types.
  658. <LI>Ada provides a
  659. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-03-05-09.html">"fixed
  660. point" type</A>; these are basically integers that
  661. the Ada compiler will automatically scale so that they can be used
  662. as though they were floating point numbers.
  663. This combines the exactness and speed of integers
  664. (which on many machines are faster than Floats)
  665. with the convenience of floating point numbers.
  666. <LI>Ada 95 has a number of predefined mathematical operations, including
  667. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-A-05-02.html">a
  668. random number generator</A>,
  669. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-A-05-01.html">trigonometric
  670. and logarithmic operations</A>, and
  671. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-G-01.html">complex
  672. numbers</A>.
  673. <COMMENT Technically speaking only compilers implementing the numerics annex >
  674. <COMMENT need to support complex numbers, but since the source implementing >
  675. <COMMENT them is freely available from GNAT >
  676. <COMMENT I'd expect all Ada 95 compilers to support complex numbers. >
  677. <LI>Ada 95 has a number of predefined
  678. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-A-03-02.html">Character
  679. handling</A> subprograms (to upper case, to lower case, etc) and
  680. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-A-04-02.html">string
  681. mapping</A> subprograms (to create sets of characters for translation
  682. and word edge detection).
  683. <LI>Ada 95 compilers that support the information systems annex (annex F)
  684. also support
  685. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-F-01.html">packed
  686. decimal types</A>, a type needed for many business
  687. programming problems.
  688. </OL>
  689.  
  690.