home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / metric / halstead.doc < prev    next >
Encoding:
Text File  |  1988-05-03  |  34.1 KB  |  1,027 lines

  1. ::::::::::::::
  2. halstead.cnt
  3. ::::::::::::::
  4. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHSUBTYPE.DAT                      6    11
  5. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHSERIES_.DAT                      6    11
  6. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHAGG_NAM.DAT                      6    11
  7. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHCASE_AL.DAT                      6    11
  8. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHHANDLER.DAT                      6    11
  9. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHBLOCK_S.DAT                      6    11
  10. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHINNER_R.DAT                      6    11
  11. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHVARIABL.DAT                      8    12
  12. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHTYPE_DE.DAT                      8    12
  13. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHGENERIC.DAT                      6    11
  14. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]IHTASK_DE.DAT                      6    11
  15. [NOSC.RELEASES.V0302.HALSTEAD.SOURCE]HALSTEAD.ADA                      70   252
  16. [NOSC.RELEASES.V0302.HALSTEAD.TEST]SCANNERS.SPC                        15   245
  17. [NOSC.RELEASES.V0302.HALSTEAD.TEST]SCANNERS.BDY                        85   283
  18. ::::::::::::::
  19. halstead.mem
  20. ::::::::::::::
  21.  
  22.  
  23.  
  24.                         The Halstead Tool
  25.  
  26.  
  27.  
  28. COMMAND FORMAT
  29.  
  30.  
  31. -- Halstead : Computes HALSTEAD's software science formulas for Ada
  32. --            program units 
  33. -- Version 3.01-1.01
  34.  
  35.  
  36. subtype STRING_TYPE is STRING;
  37. type    STRING_LIST is array (POSITIVE range <>) of STRING_TYPE;
  38.  
  39. procedure HALSTEAD(
  40.     UNITS     : in STRING_LIST;
  41.     OUTPUT    : in STRING := "";
  42.     LIBRARY   : in STRING := "_AIELIB";
  43.     );
  44.  
  45. -- UNITS    : Names of the Ada compilation units
  46. -- OUTPUT   : Name of the report file (defaults to standard output)
  47. -- LIBRARY  : Name of the program library
  48.  
  49.  
  50.  
  51.  
  52. PARAMETERS
  53.  
  54. UNITS      This parameter is a list of string values, each of which is
  55.            the name of a unit which is to be analyzed.  This parameter
  56.            is required and the list must have at least one value.  A
  57.            subunit may be specified using a qualified name, as in
  58.            ("PARENT.SUBUNIT"), which would analyze the subunit called
  59.            SUBUNIT of the compilation unit PARENT.
  60.      
  61. OUTPUT     This is the name of the output file.  It is optional and if it
  62.            is not used, output will be directed to standard output.
  63.      
  64. LIBRARY    This parameter is a string which is the program library
  65.            containing the compilation units to be analyzed.  This
  66.            This parameter is optional and defaults to "_AIELIB"
  67.            (WARNING: in the current release, this parameter is ignored)
  68.  
  69.      As an example assume that there  exists  a  program  library
  70. called  [.AIELIB]  and  that  it  is the current directory.  Also
  71. assume that in the program library  there  exists  a  compilation
  72. unit  which  is a procedure called MAIN.  The objective is to run
  73. the HALSTEAD tool on the unit MAIN.  The following command can be
  74. used:
  75.  
  76.      $ HALSTEAD(UNITS => ("MAIN"));
  77.  
  78. This will compute the Halstead metrics on the unit MAIN and write
  79. The Halstead Tool                                          Page 2
  80.  
  81.  
  82. the report to the default output file.
  83.  
  84.      Note that an implicit specification is  constructed  by  the
  85. Byron/AIE  compiler  front end for any compilation unit that does
  86. not have an explicit specification.  The HALSTEAD tool  does  not
  87. distinguish   these   implicit   specifications   from   explicit
  88. specifications.  In  the  above  example,  the  metrics  will  be
  89. computed  for both the specification and body of MAIN, regardless
  90. of whether or not a source file containing  a  specification  for
  91. MAIN was compiled into the library.
  92.  
  93.  
  94.  
  95. DESCRIPTION
  96.  
  97.      The Halstead tool uses the number of operands and  operators
  98. in  the  program  to  compute the expected length of the program.
  99. Each symbol in the program is classified  as  an  operand  or  an
  100. operator.   Operators are mathematical functions such as +, -, *,
  101. and /, as well as parenthesis and subprogram calls.  Operands are
  102. constants  and  variables.   From  the  number  of  operands  and
  103. operators, Halstead makes predictions about the program  such  as
  104. intelligence   content,  number  of  expected  bugs,  programming
  105. effort, and the level at  which  the  program  is  written.   See
  106. [Els].
  107.  
  108.      The metrics are computed on a compilation unit  by  breaking
  109. it  down  into  its fundamental units.  These are the basic units
  110. out of which Ada programs  are  built.   These  are  subprograms,
  111. packages,  and  tasks.   The  metrics are computed in a lexically
  112. nested fashion.  For example inside a package body which contains
  113. subprogram  bodies  in it each of the subprograms has the metrics
  114. computed for it.
  115.  
  116.  
  117.  
  118. PREPARATION
  119.  
  120.      HALSTEAD computes the metrics for an ADA  program  by  using
  121. the  source  program's  DIANA  representation.   Therefore  it is
  122. necessary to have the source program's DIANA in  an  ADA  program
  123. library  before  running HALSTEAD.  This can be accomplished in a
  124. number of steps.
  125.  
  126.      1. First it is necessary to create an ADA program library which will
  127.         contain the DIANA representations of the source program which
  128.         HALSTEAD will analyze.  This is accomplished by the command:
  129.      
  130.         AIEPLIB
  131.      
  132.      
  133.         This command creates a directory called [._AIELIB] in the directory it
  134.         is executed from.
  135.      
  136.      2. Following this it is necessary to successfully run the source
  137. The Halstead Tool                                          Page 3
  138.  
  139.  
  140.         program through ADASEM, which is semantics phase of the ADA
  141.         compiler. This generates the DIANA representation for the source
  142.         program. In order to run the semantics phase execute the command:
  143.  
  144.         ADASEM SOURCE.ADA
  145.  
  146.  
  147.  
  148.  
  149. RESTRICTIONS
  150.  
  151.      It  is  important  to  note  that   Halstead   places   some
  152. restrictions  on  the program whose estimates are being computed.
  153. It must be a "pure" program.  Halstead defines a pure program  to
  154. be one that does not contain any of the following constructions.
  155.      
  156.       1. No complementary operators.
  157.      
  158.          There cannot be successive applications of two comple-
  159.          mentary operations to the same operand. For example:
  160.      
  161.          z - t + t -> r
  162.      
  163.      
  164.       2. There cannot be ambigous operands.
  165.      
  166.          A given operand cannot refer to different objects at
  167.          different times in the program.  For instance in the
  168.          following example r represents first a sum and then a
  169.          product.
  170.      
  171.      
  172.          p + q -> r   r * r -> r
  173.      
  174.          Should be written as:
  175.      
  176.          p + q -> t   t * t -> r
  177.      
  178.      
  179.       3. There cannot be synonomous operands.
  180.      
  181.          Two different names for the same object.  There can only
  182.          exist one name for one object.
  183.      
  184.      
  185.          p + q -> t1  p + q -> t2  t1 * t2 -> r
  186.      
  187.          Should be written as:
  188.      
  189.          p + q -> t1  t1 * t1 -> r
  190.      
  191.      
  192.       4. Common subexpressions cannot exist.
  193.      
  194.          Any expression which is used more than once must be
  195. The Halstead Tool                                          Page 4
  196.  
  197.  
  198.          assigned to a variable.
  199.      
  200.          (p + q) * (p + q) -> r
  201.      
  202.          Should be written as:
  203.      
  204.          (p + q) -> t1  t1 ** 2 -> r
  205.      
  206.      
  207.       5. No unwarranted assignments.
  208.      
  209.          If an assignment is made and the value used only once
  210.          the assignment is unnecessary.
  211.      
  212.          p + q -> t1   t1 ** 2 -> r
  213.      
  214.          Should be written as:
  215.      
  216.          (p + q) ** 2 -> r
  217.      
  218.      
  219.       6. Expressions should be factored.
  220.      
  221.          p*p + 2*p*q + q*q
  222.      
  223.          Should be written as:
  224.      
  225.          (p + q) ** 2
  226.      
  227.  
  228.  
  229.  
  230. METRICS COMPUTED
  231.  
  232.      Halstead begins by defining the following symbols:
  233.      
  234. 1.  NUMBER OF UNIQUE OPERATORS
  235.      
  236.     n1
  237.      
  238.      
  239. 2.  NUMBER OF UNIQUE OPERANDS
  240.      
  241.     n2
  242.      
  243.      
  244. 3.  THE VOCABULARY
  245.      
  246.     n = n1 + n2
  247.      
  248.      
  249. 4.  TOTAL NUMBER OF OPERATORS
  250.      
  251.     N1
  252.      
  253. The Halstead Tool                                          Page 5
  254.  
  255.  
  256.      
  257. 5.  TOTAL NUMBER OF OPERANDS
  258.      
  259.      
  260.     N2
  261.      
  262.      
  263. 6.  LENGTH OF PROGRAM
  264.      
  265.     N = N1 + N2
  266.      
  267.      
  268. 7.  ESTIMATED PROGRAM LENGTH:
  269.      
  270.     N^ = n1*log2(n1) + n2*log2(n2)
  271.      
  272.     Where n1 and n2 are the number of distinct operators and
  273.     operands.  N^ is an estimator for program length.  
  274.      
  275. 8.  PROGRAM VOLUME:
  276.      
  277.     V = N * log2(n)
  278.      
  279.     This is the size of the program in bits.  Where N is program
  280.     length and n is the vocabulary.  This is because we need
  281.     log2(n) bits for each symbol and there are N symbols.
  282.      
  283.      
  284. 9.  POTENTIAL VOLUME
  285.      
  286.     V* = (2 + n2*) * log2(2 + n2*)
  287.      
  288.     This represents the size of the algorithm if it existed as a
  289.     built-in function or a subprogram.  It is the most compact
  290.     form of the algorithm.  The 2 is the minimum number of opera-
  291.     tors the procedure name and a grouping symbol needed to per-
  292.     form the function. The n2* represents the minimum number of
  293.     operands needed if the algorithm existed as a built-in func-
  294.     tion.  The n2* can be viewed as the minimum number of
  295.     input/output parameters for the algorithm if it existed as a
  296.     builtin function.
  297.      
  298.      
  299. 10. PROGRAM LEVEL
  300.      
  301.     L = (V* / V)
  302.      
  303.     This measures the level at which the program was written.
  304.     The the closer the volume V is to the potential volume V* the
  305.     higher the level.
  306.      
  307.      
  308. 11. PROGRAM LEVEL APPROXOMATION
  309.      
  310.     L^ = (n1* / n1) * (n2 * N2)
  311. The Halstead Tool                                          Page 6
  312.  
  313.  
  314.      
  315.     This is an approximation for program level.
  316.      
  317.      
  318. 12. INTELLIGENCE CONTENT
  319.      
  320.     I = L^ * V
  321.      
  322.     This is the amount of information contained in a program.
  323.      
  324.      
  325. 13. PROGRAMMING EFFORT
  326.      
  327.     E = ( V / L )
  328.      
  329.     The effort of programming increases as the volume of the pro-
  330.     gram increases and decreases as the level increases.  This
  331.     means the larger a program the more difficult the effort and
  332.     the higher the level of implemenation the the easier the
  333.     effort.  This effort also can be viewed as the number of ele-
  334.     mentary discriminations necessary to complete a program.
  335.     [Fitz Lov]
  336.      
  337.      
  338. 14. PROGRAMMING TIME
  339.      
  340.     T^ = E / S = (V ** 2) / (S * V*)
  341.      
  342.     The amount of time required to implement an algorithm is
  343.     directly proportional to the programing effort E divided by a
  344.     constant S.  S is the speed of the programmer, or the number
  345.     of mental discriminations per second.  Halstead calculated S
  346.     equaled 18.  This relates to the work of Stroud, a psycholo-
  347.     gist,  who estimates S between 5 and 20. [Str], [Hal]
  348.      
  349.      
  350. 15. LANGUAGE LEVEL
  351.      
  352.     lambda = L * V*
  353.      
  354.     lambda = L**2 * V
  355.      
  356.     This measure enables us to rank languages in terms of their
  357.     expressive power.  The higher the measure the higher the
  358.     levle of the langusage.  For example Pascal has a lambda
  359.     greater than the lambda for Fortran.
  360.      
  361.      
  362. 16. NUMBER OF DELIVERED ERRORS
  363.      
  364.      
  365.     B = (E ** 2/3) / E0
  366.      
  367.     B^ = (E ** 2/3) / 3000
  368.      
  369. The Halstead Tool                                          Page 7
  370.  
  371.  
  372.     B^ = V / 3000
  373.      
  374.     E0 is the mean number of elementary discriminations between
  375.     errors. E0 has been estimated to be 3000.
  376.      
  377.      
  378. 17. Modularization
  379.      
  380.     n log2 (n/2) = M((n-n*)/M + n*) log2 (( (n-n*) / M ) /2)
  381.      
  382.     M is the number of modules the program should be divided
  383.     into.
  384.       
  385.  
  386.  
  387.  
  388. OUTPUT FORMAT
  389.  
  390.      The output format will be organized by Ada  units  contained
  391. in  compilation  unit  specified.   The metrics computed for each
  392. nested unit will  be  displayed  in  reverse  linear  elaboration
  393. order.   This  means  the  most deeply nested units will have the
  394. metrics computed for them appear first in the report.  The output
  395. format  below  has  each  column representing a different metric.
  396. The  numbers  heading  each  column  correspond  to  the  metrics
  397. described  above.   An  example  output  for library unit appears
  398. below.
  399.  
  400.      The following is a sample input and output from HALSTEAD:
  401. SAMPLE INPUT:
  402.  
  403. ----------------------------------------------------------------
  404. package SCANNERS is
  405.  
  406.     .
  407.     .
  408.     .
  409.  
  410. procedure scan_Delimited(       --| Scan string delimited by a single character
  411.     Scanner: in out scanner_Type;
  412.     S: in string
  413.     )
  414. is
  415.     quote: character;
  416.  
  417. begin
  418.     Scanner.First := Scanner.Index;
  419.     if Scanner.Index <= Scanner.Max_Index then
  420.         quote := S(Scanner.Index);
  421.         Scanner.Index := Scanner.Index + 1;
  422.         Scanner.First := Scanner.Index;
  423.         while Scanner.Index <= Scanner.Max_Index 
  424.               and then S(Scanner.Index) /= quote
  425.         loop
  426.           Scanner.Index := Scanner.Index + 1;
  427. The Halstead Tool                                          Page 8
  428.  
  429.  
  430.         end loop;
  431.     end if;
  432.     Scanner.Length := Scanner.Index - Scanner.First;
  433.     Scanner.Last := Scanner.Index - 1;
  434.     if Scanner.Index <= Scanner.Max_Index
  435.     and then S(Scanner.Index) = quote then      -- Null string?
  436.         Scanner.Index := Scanner.Index + 1;
  437.     end if;
  438.  
  439. end scan_Delimited;
  440.  
  441. ----------------------------------------------------------------
  442.  
  443.  
  444. SAMPLE OUTPUT:
  445.  
  446.  
  447.  PROCEDURE BODY OF SCANNERS.SCAN_DELIMITED AT LINE         205
  448.  
  449. UNIQUE OPERATORS                 17     UNIQUE OPERANDS                11
  450. TOTAL OPERATORS                  60     TOTAL OPERANDS                 61
  451. VOCABULARY                       28
  452. PROGRAM LENGTH                  121     ESTIMATED LENGTH              107.54
  453. PROGRAM VOLUME                  579.68  POTENTIAL VOLUME               15.50
  454. PROGRAM LEVEL                      .03  ESTIMATED LEVEL                78.94
  455. INTELLIGENCE CONTENT          45760.71  PROGRAMMING EFFORT          21674.78
  456. PROGRAMMING TIME               4334.96  LANGUAGE LEVEL                   .41
  457. DELIVERED ERRORS                  4.17  ESTIMATED ERRORS                 .19
  458.  
  459.  
  460.  
  461.  
  462. REFERENCES
  463.  
  464.      
  465.      
  466. [Els]   Elshoff, James, L., "Measuring Commercial PL/1 Programs
  467.         Using Halstead's Criteria", SIGPLAN Notices, May 76, pp38-46
  468.      
  469. [Fitz Lov]   Fitzsimmons, A., Love, T. "A Review and Evaluation of
  470.         Software Science", Computing Surveys, Vol. 10, No. 1, March 
  471.         1978, pp3-18
  472.      
  473. [Hal]   Halstead, M. H., Elements of Software Science, Elsevier North
  474.         Holland, Inc., Operating and Proramming Systems Series, New 
  475.         York, 1977
  476.      
  477. [Str]   Stroud, John, M., The Fine Structure of Psychological Time,
  478.         "Annals of New York Academy of Sciences", 1966, pp623-631
  479. The Halstead Tool                                          Page 9
  480.  
  481.  
  482. APPENDIX A:  INSTALLATION AND MODIFICATION
  483.  
  484. Note: This version of HALSTEAD ignores the LIBRARY parameter.  The
  485. directory containing the AIE program library must be your default
  486. directory when you run HALSTEAD.  This is a limitation of the AIE
  487. Program Library Manager which should be corrected at some time in the
  488. future.
  489.  
  490. To use the HALSTEAD tool you must:
  491.  
  492.  1. Be a "HIF User".  See the Byron/AIE Programmers Reference Manual
  493.     for details.  This requires that certain logical symbols be defined
  494.     and that you have run the ADDUSER tool to make yourself known to
  495.     the HIF database.
  496.  
  497.  2. Define a symbol HALSTEAD so that VMS knows what executable to run
  498.     when you type the HALSTEAD command.  For example:
  499.  
  500.       $ HALSTEAD :== $USER1:[NOSC.TOOLS]HALSTEAD.EXE
  501.  
  502.      NOTE: The full path name of the executable is required in the 
  503.      definition of the symbol.   The pathname given here is just an
  504.      example and will be different on your system.
  505.  
  506.  3. Create a Byron/AIE program library and use the Byron/AIE compiler
  507.     front end to analyze source code into it.  The AIEPLIB and AIECOMP
  508.     commands are used respectively.
  509.  
  510.  4. Enter a command with appropriate parameters.  For example,
  511.  
  512.       $ HALSTEAD(("MYPROG", "TEST.OUT"));
  513.  
  514.     Entering the command with no parameters gives a brief description
  515.     of how to use the tool.
  516.  
  517.  
  518. To build the HALSTEAD tool:
  519.  
  520.  1. Compile all the abstractions into a program library (see READ.ME in
  521.     abstractions directory for details).  
  522.  
  523.  2. Compile everything named in the HALSTEAD.CO file into the program library
  524.     containing the abstractions or a sublibrary whose parent library contains 
  525.     all the abstractions.  HALSTEAD.CO lists file names in a correct 
  526.     compilation order.
  527.  
  528.  3. Link HALSTEAD with the program library where everything was compiled.
  529.     To do this using the DEC Ada compiler type:
  530.  
  531.     $ ACS LINK HALSTEAD
  532.  
  533.  
  534. Files contained in the HALSTEAD.SOURCE directory:
  535.  
  536. BLOCKU.SPC      BLOCKU.BDY      COMLIN.SPC
  537. The Halstead Tool                                         Page 10
  538.  
  539.  
  540. COMLIN.BDY      DEFS.SPC        COUNTYPE.SPC    COUNT.SPC
  541. COUNT.BDY       COUNTYPE.BDY    DEFS.BDY        HDB.SPC
  542. HDB.BDY         SCTYPESP.SPC    SCNAMEEX.SPC
  543. SCCONSTRA.SPC   IHSUBTYPE.DAT   OBJ.SPC OBJ.BDY
  544. IHSERIES.DAT    IHAGGNAM.DAT    SCCHOICE.SPC
  545. SCAGGCOM.SPC    SCAGGCOM.BDY    IHCASEAL.DAT
  546. IHHANDLER.DAT   SCSTM.SPC       SCITEM.SPC
  547. SCALTERNA.SPC   SCALTERNA.BDY   IHBLOCKS.DAT
  548. SRCUTIL.SPC     SCBLOCKS.SPC    SCBLOCKS.BDY
  549. IHINNERR.DAT    SCCHOICE.BDY    SCCOMPUN.SPC
  550. SCCOMPUN.BDY    SCGENERAL.SPC   SCCONSTRA.BDY
  551. IHVARIABL.DAT   IHTYPEDE.DAT    IDUTILS.SPC
  552. IDUTILS.BDY     SCDEFID.SPC     SCDEFID.BDY
  553. SCGENERAL.BDY   IHGENERIC.DAT   SCGENERIC.SPC
  554. SCGENERIC.BDY   SCHEADER.SPC    SCHEADER.BDY
  555. SCVARIANT.SPC   SCINNERR.SPC    SCINNERR.BDY
  556. IHTASKDE.DAT    SCPKGDEF.SPC    SCOBJECT.SPC
  557. SCSUBPDE.SPC    SCITEM.BDY      SCITERATI.SPC
  558. SCITERATI.BDY   SCNAMEEX.BDY    SCOBJECT.BDY
  559. SCPKGDEF.BDY    SCSTM.BDY       SCSUBPDE.BDY
  560. SCTYPESP.BDY    SCVARIANT.BDY   SRCUTIL.BDY
  561. HALSTEAD.ADA
  562.  
  563.  
  564. To change the assignemnt of a token as either an operator, operand, or
  565. neither, see COUNT.BDY.  To change the computation of any of the HALSTEAD
  566. measures, see HDB.BDY.  The file HALSTEAD.ADA contains the driver program.  
  567.  
  568.  
  569. To test the HALSTEAD tool, create an AIE program library using AIEPLIB,
  570. compile [.TEST]SCANNERS.SPC and [.TEST]SCANNERS.BDY into the AIE program
  571. library using AIECOMP, make that library your default directory, and run
  572. HALSTEAD, as follows:
  573.  
  574.     $ AIEPLIB [.DEMOLIB]
  575.     $ AIECOMP SCANNERS.SPC [.DEMOLIB]
  576.     $ AIECOMP SCANNERS.BDY [.DEMOLIB]
  577.     $ SET DEFAULT [.DEMOLIB]
  578.     $ HALSTEAD(("SCANNERS"), "SCANNERS.OUT");
  579.  
  580. Then, verify that the file SCANNERS.OUT is the same as that shipped with
  581. this release.
  582. ::::::::::::::
  583. halstead.rno
  584. ::::::::::::::
  585. .RM 65        ! right margin makes page ~65 chars and almost centered
  586. .PS ,65        ! lines page numbers up with right margin
  587. .STHL ,,,0,,,,,    ! turns off section numbering
  588. .KEEP        ! Keep blank lines in literal text
  589. .T The Halstead Tool
  590.  
  591. .C
  592. The Halstead Tool
  593. .HL Command Format
  594. .LT
  595.  
  596. -- Halstead : Computes HALSTEAD's software science formulas for Ada
  597. --            program units 
  598. -- Version 3.01-1.01
  599.  
  600.  
  601. subtype STRING_TYPE is STRING;
  602. type    STRING_LIST is array (POSITIVE range <>) of STRING_TYPE;
  603.  
  604. procedure HALSTEAD(
  605.     UNITS     : in STRING_LIST;
  606.     OUTPUT    : in STRING := "";
  607.     LIBRARY   : in STRING := "_AIELIB";
  608.     );
  609.  
  610. -- UNITS    : Names of the Ada compilation units
  611. -- OUTPUT   : Name of the report file (defaults to standard output)
  612. -- LIBRARY  : Name of the program library
  613.  
  614. .EL
  615.  
  616. .HL PARAMETERS
  617. .LT
  618. UNITS      This parameter is a list of string values, each of which is
  619.            the name of a unit which is to be analyzed.  This parameter
  620.            is required and the list must have at least one value.  A
  621.            subunit may be specified using a qualified name, as in
  622.            ("PARENT.SUBUNIT"), which would analyze the subunit called
  623.            SUBUNIT of the compilation unit PARENT.
  624.      
  625. OUTPUT     This is the name of the output file.  It is optional and if it
  626.            is not used, output will be directed to standard output.
  627.      
  628. LIBRARY    This parameter is a string which is the program library
  629.            containing the compilation units to be analyzed.  This
  630.            This parameter is optional and defaults to "_AIELIB"
  631.        (WARNING: in the current release, this parameter is ignored)
  632. .EL
  633. .P     
  634. As an example assume that there exists a program library called
  635. [._AIELIB] and that it is the current directory.  Also assume that in
  636. the program library there exists a compilation unit which is a procedure
  637. called MAIN.  The objective is to run the HALSTEAD tool on the unit
  638. MAIN.  The following command can be used:
  639. .lt     
  640.  
  641.      $ HALSTEAD(UNITS => ("MAIN"));
  642.  
  643. .el     
  644. This will compute the Halstead metrics on the unit MAIN and write
  645. the report to the default output file.
  646. .p     
  647. Note that an implicit specification is constructed by the Byron/AIE
  648. compiler front end for any compilation unit that does not have an
  649. explicit specification.  The HALSTEAD tool does not distinguish these
  650. implicit specifications from explicit specifications.  In the above
  651. example, the metrics will be computed for both the specification and
  652. body of MAIN, regardless of whether or not a source file containing a
  653. specification for MAIN was compiled into the library.
  654.  
  655. .HL DESCRIPTION
  656. .p     
  657. The Halstead tool uses the number of operands and operators in the
  658. program to compute the expected length of the program.  Each symbol in
  659. the program is classified as an operand or an operator.  Operators are
  660. mathematical functions such as +, -, *, and /, as well as parenthesis
  661. and subprogram calls.  Operands are constants and variables.  From the
  662. number of operands and operators, Halstead makes predictions about the
  663. program such as intelligence content, number of expected bugs,
  664. programming effort, and the level at which the program is written.  See
  665. [Els].
  666. .p     
  667. The metrics are computed on a compilation unit by breaking it down into
  668. its fundamental units. These are the basic units out of which Ada
  669. programs are built.  These are subprograms, packages, and tasks.  The
  670. metrics are computed in a lexically nested fashion.  For example inside
  671. a package body which contains subprogram bodies in it each of the
  672. subprograms has the metrics computed for it.
  673. .hl PREPARATION
  674. .p     
  675. HALSTEAD computes the metrics for an ADA program by using the
  676. source program's DIANA representation.  Therefore it is necessary
  677. to have the source program's DIANA in an ADA program library
  678. before running HALSTEAD.  This can be accomplished in a number of
  679. steps.
  680. .lt     
  681.  
  682.      1. First it is necessary to create an ADA program library which will
  683.         contain the DIANA representations of the source program which
  684.         HALSTEAD will analyze.  This is accomplished by the command:
  685.      
  686.         AIEPLIB
  687.      
  688.      
  689.         This command creates a directory called [._AIELIB] in the directory it
  690.         is executed from.
  691.      
  692.      2. Following this it is necessary to successfully run the source
  693.         program through ADASEM, which is semantics phase of the ADA
  694.         compiler. This generates the DIANA representation for the source
  695.         program. In order to run the semantics phase execute the command:
  696.  
  697.         ADASEM SOURCE.ADA
  698.  
  699. .el
  700.  
  701. .HL RESTRICTIONS
  702. .p     
  703. It is important to note that Halstead places some restrictions on
  704. the program whose estimates are being computed.  It must be a
  705. "pure" program.  Halstead defines a pure program to be one that
  706. does not contain any of the following constructions.
  707. .LT     
  708.      
  709.       1. No complementary operators.
  710.      
  711.          There cannot be successive applications of two comple-
  712.          mentary operations to the same operand. For example:
  713.      
  714.          z - t + t -> r
  715.      
  716.      
  717.       2. There cannot be ambigous operands.
  718.      
  719.          A given operand cannot refer to different objects at
  720.          different times in the program.  For instance in the
  721.          following example r represents first a sum and then a
  722.          product.
  723.      
  724.      
  725.          p + q -> r   r * r -> r
  726.      
  727.          Should be written as:
  728.      
  729.          p + q -> t   t * t -> r
  730.      
  731.      
  732.       3. There cannot be synonomous operands.
  733.      
  734.          Two different names for the same object.  There can only
  735.          exist one name for one object.
  736.      
  737.      
  738.          p + q -> t1  p + q -> t2  t1 * t2 -> r
  739.      
  740.          Should be written as:
  741.      
  742.          p + q -> t1  t1 * t1 -> r
  743.      
  744.      
  745.       4. Common subexpressions cannot exist.
  746.      
  747.          Any expression which is used more than once must be
  748.          assigned to a variable.
  749.      
  750.          (p + q) * (p + q) -> r
  751.      
  752.          Should be written as:
  753.      
  754.          (p + q) -> t1  t1 ** 2 -> r
  755.      
  756.      
  757.       5. No unwarranted assignments.
  758.      
  759.          If an assignment is made and the value used only once
  760.          the assignment is unnecessary.
  761.      
  762.          p + q -> t1   t1 ** 2 -> r
  763.      
  764.          Should be written as:
  765.      
  766.          (p + q) ** 2 -> r
  767.      
  768.      
  769.       6. Expressions should be factored.
  770.      
  771.          p*p + 2*p*q + q*q
  772.      
  773.          Should be written as:
  774.      
  775.          (p + q) ** 2
  776.      
  777. .el     
  778. .hl METRICS COMPUTED     
  779. .p     
  780. Halstead begins by defining the following symbols:
  781. .lt     
  782.      
  783. 1.  NUMBER OF UNIQUE OPERATORS
  784.      
  785.     n1
  786.      
  787.      
  788. 2.  NUMBER OF UNIQUE OPERANDS
  789.      
  790.     n2
  791.      
  792.      
  793. 3.  THE VOCABULARY
  794.      
  795.     n = n1 + n2
  796.      
  797.      
  798. 4.  TOTAL NUMBER OF OPERATORS
  799.      
  800.     N1
  801.      
  802.      
  803. 5.  TOTAL NUMBER OF OPERANDS
  804.      
  805.      
  806.     N2
  807.      
  808.      
  809. 6.  LENGTH OF PROGRAM
  810.      
  811.     N = N1 + N2
  812.      
  813.      
  814. 7.  ESTIMATED PROGRAM LENGTH:
  815.      
  816.     N^ = n1*log2(n1) + n2*log2(n2)
  817.      
  818.     Where n1 and n2 are the number of distinct operators and
  819.     operands.  N^ is an estimator for program length.  
  820.      
  821. 8.  PROGRAM VOLUME:
  822.      
  823.     V = N * log2(n)
  824.      
  825.     This is the size of the program in bits.  Where N is program
  826.     length and n is the vocabulary.  This is because we need
  827.     log2(n) bits for each symbol and there are N symbols.
  828.      
  829.      
  830. 9.  POTENTIAL VOLUME
  831.      
  832.     V* = (2 + n2*) * log2(2 + n2*)
  833.      
  834.     This represents the size of the algorithm if it existed as a
  835.     built-in function or a subprogram.  It is the most compact
  836.     form of the algorithm.  The 2 is the minimum number of opera-
  837.     tors the procedure name and a grouping symbol needed to per-
  838.     form the function. The n2* represents the minimum number of
  839.     operands needed if the algorithm existed as a built-in func-
  840.     tion.  The n2* can be viewed as the minimum number of
  841.     input/output parameters for the algorithm if it existed as a
  842.     builtin function.
  843.      
  844.      
  845. 10. PROGRAM LEVEL
  846.      
  847.     L = (V* / V)
  848.      
  849.     This measures the level at which the program was written.
  850.     The the closer the volume V is to the potential volume V* the
  851.     higher the level.
  852.      
  853.      
  854. 11. PROGRAM LEVEL APPROXOMATION
  855.      
  856.     L^ = (n1* / n1) * (n2 * N2)
  857.      
  858.     This is an approximation for program level.
  859.      
  860.      
  861. 12. INTELLIGENCE CONTENT
  862.      
  863.     I = L^ * V
  864.      
  865.     This is the amount of information contained in a program.
  866.      
  867.      
  868. 13. PROGRAMMING EFFORT
  869.      
  870.     E = ( V / L )
  871.      
  872.     The effort of programming increases as the volume of the pro-
  873.     gram increases and decreases as the level increases.  This
  874.     means the larger a program the more difficult the effort and
  875.     the higher the level of implemenation the the easier the
  876.     effort.  This effort also can be viewed as the number of ele-
  877.     mentary discriminations necessary to complete a program.
  878.     [Fitz Lov]
  879.      
  880.      
  881. 14. PROGRAMMING TIME
  882.      
  883.     T^ = E / S = (V ** 2) / (S * V*)
  884.      
  885.     The amount of time required to implement an algorithm is
  886.     directly proportional to the programing effort E divided by a
  887.     constant S.  S is the speed of the programmer, or the number
  888.     of mental discriminations per second.  Halstead calculated S
  889.     equaled 18.  This relates to the work of Stroud, a psycholo-
  890.     gist,  who estimates S between 5 and 20. [Str], [Hal]
  891.      
  892.      
  893. 15. LANGUAGE LEVEL
  894.      
  895.     lambda = L * V*
  896.      
  897.     lambda = L**2 * V
  898.      
  899.     This measure enables us to rank languages in terms of their
  900.     expressive power.  The higher the measure the higher the
  901.     levle of the langusage.  For example Pascal has a lambda
  902.     greater than the lambda for Fortran.
  903.      
  904.      
  905. 16. NUMBER OF DELIVERED ERRORS
  906.      
  907.      
  908.     B = (E ** 2/3) / E0
  909.      
  910.     B^ = (E ** 2/3) / 3000
  911.      
  912.     B^ = V / 3000
  913.      
  914.     E0 is the mean number of elementary discriminations between
  915.     errors. E0 has been estimated to be 3000.
  916.      
  917.      
  918. 17. Modularization
  919.      
  920.     n log2 (n/2) = M((n-n*)/M + n*) log2 (( (n-n*) / M ) /2)
  921.      
  922.     M is the number of modules the program should be divided
  923.     into.
  924.       
  925. .el
  926. .HL OUTPUT FORMAT
  927. .p     
  928. The output format will be organized by Ada units contained in
  929. compilation unit specified.  The metrics computed for each nested
  930. unit will be displayed in reverse linear elaboration order.  This
  931. means the most deeply nested units will have the metrics computed
  932. for them appear first in the report.  The output format below has
  933. each column representing a different metric.  The numbers heading
  934. each column correspond to the metrics described above. An example
  935. output for library unit appears below.
  936. .P
  937. The following is a sample input and output from HALSTEAD:
  938. .LT
  939. SAMPLE INPUT:
  940.  
  941. ----------------------------------------------------------------
  942. package SCANNERS is
  943.  
  944.     .
  945.     .
  946.     .
  947.  
  948. procedure scan_Delimited(    --| Scan string delimited by a single character
  949.     Scanner: in out scanner_Type;
  950.     S: in string
  951.     )
  952. is
  953.     quote: character;
  954.  
  955. begin
  956.     Scanner.First := Scanner.Index;
  957.     if Scanner.Index <= Scanner.Max_Index then
  958.     quote := S(Scanner.Index);
  959.     Scanner.Index := Scanner.Index + 1;
  960.     Scanner.First := Scanner.Index;
  961.     while Scanner.Index <= Scanner.Max_Index 
  962.           and then S(Scanner.Index) /= quote
  963.     loop
  964.       Scanner.Index := Scanner.Index + 1;
  965.     end loop;
  966.     end if;
  967.     Scanner.Length := Scanner.Index - Scanner.First;
  968.     Scanner.Last := Scanner.Index - 1;
  969.     if Scanner.Index <= Scanner.Max_Index
  970.     and then S(Scanner.Index) = quote then    -- Null string?
  971.     Scanner.Index := Scanner.Index + 1;
  972.     end if;
  973.  
  974. end scan_Delimited;
  975.  
  976. ----------------------------------------------------------------
  977.  
  978.  
  979. SAMPLE OUTPUT:
  980.  
  981.  
  982.  PROCEDURE BODY OF SCANNERS.SCAN_DELIMITED AT LINE         205
  983.  
  984. UNIQUE OPERATORS                 17     UNIQUE OPERANDS                11
  985. TOTAL OPERATORS                  60     TOTAL OPERANDS                 61
  986. VOCABULARY                       28
  987. PROGRAM LENGTH                  121     ESTIMATED LENGTH              107.54
  988. PROGRAM VOLUME                  579.68  POTENTIAL VOLUME               15.50
  989. PROGRAM LEVEL                      .03  ESTIMATED LEVEL                78.94
  990. INTELLIGENCE CONTENT          45760.71  PROGRAMMING EFFORT          21674.78
  991. PROGRAMMING TIME               4334.96  LANGUAGE LEVEL                   .41
  992. DELIVERED ERRORS                  4.17  ESTIMATED ERRORS                 .19
  993.  
  994. .EL
  995.  
  996. .HL REFERENCES
  997. .LT     
  998.      
  999.      
  1000. [Els]    Elshoff, James, L., "Measuring Commercial PL/1 Programs
  1001.     Using Halstead's Criteria", SIGPLAN Notices, May 76, pp38-46
  1002.      
  1003. [Fitz Lov]   Fitzsimmons, A., Love, T. "A Review and Evaluation of
  1004.     Software Science", Computing Surveys, Vol. 10, No. 1, March 
  1005.     1978, pp3-18
  1006.      
  1007. [Hal]    Halstead, M. H., Elements of Software Science, Elsevier North
  1008.     Holland, Inc., Operating and Proramming Systems Series, New 
  1009.     York, 1977
  1010.      
  1011. [Str]    Stroud, John, M., The Fine Structure of Psychological Time,
  1012.     "Annals of New York Academy of Sciences", 1966, pp623-631
  1013. .el     
  1014.     
  1015. .PG
  1016. .HL Appendix A: Installation and Modification
  1017.  
  1018. .NF
  1019. .RM 80
  1020. .REQUIRE "[-]read.me"
  1021. ::::::::::::::
  1022. release.nts
  1023. ::::::::::::::
  1024. 28-Feb-85    Initial release.  Does not support named program libraries.
  1025.         The LIBRARY parameter on the command line is ignored.
  1026.  
  1027.