📄 metre.doc
字号:
beginning of this section are the names that one can use in the -mcommand-line option, e.g., -mfnp. This option allows one to specifywhich metrics to print. If none are specified, all are printed. If oneor more are specified, only those that are specified are printed. The -moption can be specified more than once on the command line. For example,-mcyc -mexc, would print only the cyclomatic and extended complexitymetrics.The Halstead metrics are only printed when the -H command-line option isspecified. Use the -s option on the command line to only display thesummaries (module summary, and if more than one module, projectsummary). In other words, this suppresses the function-specificinformation.Metre prints a warning if it encounters a goto or continue statement ormultiple return statements in a function. Metre also prints a warning ifit encounters indention accomplished by both spaces _and_ tabs.By specifying the -h command-line option, Metre prints the followinghelp text which identifies the command-line options and their function.The text also explains the output notation, such as "<min >max ~avgtotal".METRE Version 2.3 Copyright (c) 1993-1995 by Paul Long All rights reserved.Syntax: METRE [ option | file ]...-Dxxx=[xxx] Define identifier -oxxx Name of output file-Sxxx Substitute file name -C Copy input to output-w Suppress warnings -h This help-fxxx Response file-s Summaries only -H Halstead metrics-a Adjust function points -TN Stroud number [18]-c Comma-separated value -mxxx Select specific metricNotation and Abbreviations:< Minimum Cyclomatic Cyclomatic complexity> Maximum Max Depth Max control-structure depth~ Average Exec Executable(nothing) Total Decl DeclarationLang Language PP PreprocessorFor -m option: bll cyc cdl cml dcs exc exs fnc fnp inc idc idl llv mdc mcdpef pln plv pps ptm pvl pvc scl If you think that some of the diagnostics Metre generates areunreasonable, such as warning about gotos, do not worry--you can easilychange how Metre behaves by modifying the source (see the ModifyingMetre section in the accompanying install.txt file).CSV OutputThe -c option causes Metre to generate its output incomma-separated-value format (CSV) in the same order as the above,human-readable output. This allows the data to be more easily used byanother program or imported into a database or spreadsheet. You willprobably also want to suppress warnings with the -w option so that theywill not be interspersed with the CSV data.All the data for each function appears on one line, all the data foreach module appears on one line, and all the data for the projectappears on one line (if present). The first field for each type of linecontains a type identifier--fcn, mod, or prj, respectively. The secondfield for a function or module line is the name of the module. The thirdfield for a function line is the name of the function. There are no namefields for the project line. The numeric data follows for each type ofline. There are no intervening spaces in a line. Just like thehuman-readable output, the first line is always the Metre copyrightnotice. Here is a shortened example of CSV output. Ellipses show datathat I have removed which is normally there.METRE Version 2.3 Copyright (c) 1993-1995 by Paul Long All rights reserved.fcn,metrules.c,rules,41,67,799,154, ... ,12,4,313,2,28,10fcn,metrules.c,print_help,1,1,92,28, ... ,0,0,31,6,21,8fcn,metrules.c,init_project_stats,2,2,233,33, ... ,0,1,102,2,13,8mod,metrules.c,1,50,8,1,67,9,5, ... ,426,94,25,5,2268,1,30,8,24fcn,trerules.c,cmpstr,1,1,8,8, ... ,0,0,3,3,7,5fcn,trerules.c,library_call,1,1,320,162, ... ,1,0,9,4,7,6fcn,trerules.c,lookup_function,2,3,29,16, ... ,1,1,13,4,8,6mod,trerules.c,1,10,4,1,13,4,8, ... ,185,70,35,3,636,1,23,8,20prj,1,50,6,1,67,7,5,1412,158,5, ... ,441,295,611,164,60,7,2904,1,30,8,44,2The options that effect whether certain data is printed also work forthe CSV output. For example, if -H is not specified, there is noHalstead data in the output, including the commas--there is never anempty field, i.e., ",,".ComplexityMetre recognizes each of these constructs as representing a singledecision point: conditional operator, ?: if statement while statement do-while statement for statement with a conditional expression, e.g., for (;x;) adjacent cases in a switch statementThere are a few situations in C where the presence of a decision pointmay be debatable. Metre counts the conditional expression (?:) as adecision point, it counts adjacent cases as one decision point, and itcounts decision points in a switch statement as if it were simulated bya series of cascading if-then-else statements. For an example ofadjacent cases, the following switch statement contains two decisionpoints, not three. switch (a) { case 0: dothis(); break; case 1: case 2: dothat(); break; }Metre interprets decision points in this way so as to minimallyinfluence the programmer's choice of constructs. If it did not count theconditional expression as a decision point, the programmer might beinfluenced to use confusing conditional expressions instead of ifstatements in order to achieve a lower cyclomatic complexity. Likewise,adjacent cases are analogous to the logical "or" operator (||). If Metrecounted each case as a separate decision point, the programmer mightavoid the switch statement in favor of an if statement with anexpression that uses the or operator. For example, if (a == 1 || a == 2)McCabe's cyclomatic complexity metric works out to be the number ofdecision points in a function plus one. McCabe established an upperlimit of 10. As Capers Jones says in his book, _Applied SoftwareMeasurement_, Empirical studies reveal that programs with cyclomatic complexities of less than 5 are generally considered simple and easy to understand. Cyclomatic complexities of 10 or less are considered not too difficult. When the cyclomatic complexity is more than 20, the complexity is perceived as high. When the McCabe number exceeds 50, the software for practical purposes becomes untestable.In the same book, the author describes how cyclomatic complexity isrelated to maintainability. "The phrase 'bad fix' refers to an erroraccidentally inserted into a program or system whle trying to fix aprevious error." As you can see from this table, which summarizes hisexposition, highly complex programs are expensive to maintain. Ascyclomatic complexity approaches 100, your fix is more likely tointroduce another error as not. Cyclomatic Probability Complexity of Bad Fix ------------------------------ less than 10 5 20-30 20 greater than 50 40 approaching 100 60A general concern I have with counting decision points in a switchstatement (also known as a case statement) comes from the comment thatMcCabe makes in a footnote in "A Complexity Measure." He says that, "Forthe CASE construct with N cases use N-1 for the number of conditions.Notice, once again, that a simulation of case with IF's will have N-1conditions." I do not understand this and have not found anyone who canexplain it to me. I assume he is referring to a case statement in alanguage unfamiliar to me where all possible values _must_ be tested. Inthat case, one of the tests would be analogous to the final "else" ifthe case statement were simulated with cascading if-then-elsestatements.Glenford Myers defined an extension to McCabe's cyclomatic complexitymetric (although McCabe anticipated it). In addition to counting thesame decision points as McCabe, he counts compound decisions. In C,these are constructed with logical operators and adjacent cases. Forexample, foo() { if (a == b && c == 0) x; }would have a cyclomatic complexity of 2 but an extended complexity of 3.Myers would even write this as 2:3. Metre counts logical operatorswherever they occur in a function, not just in control-flow statements.For example, the logical operators in the following assignmentstatement are counted. a = b || c && d == e;With Myers' extended complexity, cases each count as one, regardless ofwhether they are adjacent. For example, foo() { switch (a) { case 0: dothis(); break; case 1: case 2: dothat(); break; } }has a cyclomatic complexity of 3 and an extended complexity of 4, or 3:4.Halstead MetricsMetre considers the following lexemes as operators for the Halsteadmetrics. Keywords: break case continue default do else for goto if return sizeof switch while Operators: ( [ . -> ++ -- , sizeof & * + - ~ ! / % << >> < > <= >= == != ^ | && || ? : = *= /= %= += -= <<= >>= &&= ^= ||= ; { ... (Basically, all operators except '}', ')', and ']' because the paired tokens, such as '(' and ')', are considered to be one operator.)Metre considers these as operands for Halstead. Identifiers Typedef type names Numeric constants StringsNote: A label and its terminating colon are not counted because Halsteadsays that labels are considered to be comments.Declarations are not considered in computing the Halstead metrics--onlyprogram logic. Exceptions to this are the initializers within compoundstatements. Metre considers them to be logic that coincidentally appearin declarations--they are equivalent to assignment statements.Therefore, Metre considers the object name, the assignment operator('='), and the initializer expression for the Halstead metrics. Theoperator that terminates the expression, such as comma or semicolon, isnot considered. The assumption is that this lexeme belongs to thedeclaration and not to the logic.For example, these two functions are equivalent. foo() { int a = 3; } foo() { int a; a = 3; }(The Halstead metrics are slightly different for them, however. This isbecause the second function has an extra semicolon operator.)These are the equations that Metre uses to calculate the Halstead metrics. Given: unique operators n1 unique operands n2 total operators N1 total operands N2 Stroud number S = 18 moments / second seconds-to-hours factor f = 60 * 60 Program Length N = N1 + N2 Program Vocabulary n = n1 + n2 Program Volume V = N * log2(n) Program Level L^ = (2 / n1) * (n2 / N2) Programming Effort E = V / L^ Programming Time (in hours) T^ = E / (S * f) Intelligence Content I = L^ * V Language Level L' = L^ * L^ * VA few words about notation in these equations: The circumflex character(^) indicates an estimated measure of an observed parameter. InHalstead's book, the circumflex appears over the symbol rather thanfollowing it. Also, the Greek symbol, lambda, used in Halstead's book isrepresented as L', above.The Stroud number is the highest number of "moments" per second of whichhumans can perceive. We cannot notice anything faster. This ranges from5 to 20 moments per second, maybe a little less. Halstead uses 18 in hisbook, _Elements of Software Science_; however, I do not know what hisrationale is for this particular value. It could have even beenarbitrary. Regardless, by default, Metre uses a Stroud number of 18 forits calculation of Programming Time. You can specify another value withthe -T command-line option. For example, -T10 specifies a Stroud numberof 10.Note: The Halstead metrics are not normally calculated by Metre. Youmust specify the -H command-line option for them. This is because of theextra processing required to calculate these metrics. On my machine,calculating the Halstead metrics slows down Metre by about 25 per cent.Line ClassificationsMetre considers a line to be a blank line if it contains only whitespacecharacters, a code line if it contains at least one non-whitespacecharacter that is not part of a comment, and a comment line if itcontains all or part of a comment and there is not also code on theline.Note: Metre calculates lines of comments. However, if you run yourprogram through a preprocessor, the preprocessor will have most likelystripped all comments from its output. Metre cannot count what is notthere. What is the solution? Well, some preprocessors provide acommand-line option that passes comments through. If yours has thiscapability, you probably want to use it.Statement-Counting StandardMetre uses the SPR Source Code Counting Rules described in the book,_Applied Software Measurement_, by Capers Jones, as its guide indeciding what language constructs are considered to be statements.Additional clarification has been provided by Mr. Jones through privatecommunications regarding specifics of the C language.Executable-Statement DefinitionAn executable statement, as counted by Metre, is an if, switch, while,do-while, for, expression, goto, continue, break, or return statement.An executable statement can also be a function definition or a label.The "empty" statement (";") is not an executable statement.Declaration-Statement DefinitionA declaration statement is any declaration construct that ends with asemicolon. For example, there are five declaration statements in thisexample: struct { int a; char b; struct foo {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -