Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC309 Lecture 02

CSC309 Lecture 02

Software Engineering II
Software Metrics: Code Quality
(202301)

Javier Gonzalez-Sanchez
PRO

January 12, 2023
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSC 309
    Software Engineering II
    Lecture 02:
    Software Metrics: Code Quality
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    www.javiergs.com
    Building 14 -227
    Office Hours: By appointment

    View Slide

  2. jgs
    Previously …

    View Slide

  3. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 3
    Key Ideas CSC 309
    idea
    requirements
    architecture
    design
    code
    unit testing
    integration
    testing
    system testing
    operation
    validation
    validation
    verification
    verification
    quality measure

    View Slide

  4. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 4
    Key Ideas CSC 309
    idea
    requirements
    architecture
    design
    code
    quality measure

    View Slide

  5. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 5
    code
    Key Ideas CSC 309
    idea
    requirements
    architecture
    design
    quality measure

    View Slide

  6. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 6
    Metrics
    Size
    LOC
    eLOC
    lLOC
    Understandability
    Comments
    Whitespaces
    Complexity
    Average, Max, Min LOC
    Interface Complexity
    Cyclomatic Complexity
    Software Metrics

    View Slide

  7. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 7
    § Knowing what is inside your source code is the first step in assessing the
    quality of the software product.
    § Knowing the quantity of work performed in generating the source code is
    the first step in determining the productivity of your software team.
    Software Metrics

    View Slide

  8. jgs
    Software Metrics
    Size

    View Slide

  9. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 9
    § LOC – Lines of Code Metric. Including lines of a single brace or
    parenthesis
    § LOC are used to create time and cost estimates.
    § LOC are a tracking tool to measure the degree of progress on a module or
    project.
    § An experienced developer can gage a LOC estimate based upon
    knowledge of past productivity on projects.
    Size Metrics

    View Slide

  10. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 10
    § eLOC – effective Lines of Code Metric. Only code statements
    § An effective line of code or eLOC is the measurement of all lines that are not
    comments, blanks or standalone braces or parenthesis. These can
    inflate LOC metrics by 20 to 40 percent.
    § This metric more closely represents the quantity of work performed.
    Size Metrics

    View Slide

  11. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 11
    § lLOC – logical Lines of Code Metric.
    § These statements are terminated with a semi-colon.
    § The control line for the "for" loop contain two semi-colons but accounts for
    only one semi colon.
    Size Metrics

    View Slide

  12. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 12
    Example

    View Slide

  13. jgs
    Software Metrics
    Understandability

    View Slide

  14. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 14
    § Comment Line and Comment Percent Metric
    § The degree of commenting within the source code measures the care taken
    by the programmer to make the source code and algorithms
    understandable.
    § Poorly commented code makes the maintenance phase of the software life
    cycle an extremely expensive adventure.
    § Comments can occur by themselves on a physical line or be co-mingled
    with source code. The sum of the lines of code, comments and blank lines
    often exceeds the physical line count. This is expected a when comments
    are co-mingled with source code.
    § Comment Percent = Comment Line Count / (LOC) x 100
    Understandability Metrics

    View Slide

  15. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 15
    § Blank Line and White Space Percent Metric
    § The number of blank lines within source code determines the readability of
    the product. White space accents the logical grouping of constructs and
    variables. Programs which use few blank lines are difficult to read and
    more expensive to maintain.
    § It counts the spaces and characters within the source code. The white
    space percentage metric is another measure of readability for the source
    product.
    § White Space Percentage =
    (Number of spaces / Number of spaces and characters) * 100
    Understandability Metrics

    View Slide

  16. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 16
    § Average LOC per Function Metric. An accepted industry standard of 200
    LOC per function is desired as the average LOC per function
    § Maximum LOC per Function Metric.
    § Minimum LOC per Function Metric. A minimum LOC per function of 2 or
    less can indicate functions that may have been prototype but not yet
    complete..
    Function Metrics

    View Slide

  17. jgs
    Software Metrics
    Complexity

    View Slide

  18. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 18
    § Cyclomatic Complexity. It is a quantitative measure of the number of
    linearly independent paths.
    § Paths occur when "while,” "for,” "if,” "case," and "goto" keywords appear
    within the function.
    § If the source code contained no control flow statements (conditionals or
    decision points), the complexity would be 1
    § If the code had one single-condition IF statement, there would be two paths
    through the code: one where the IF statement evaluates to TRUE and
    another one where it considers to be FALSE.
    § Two nested single-condition IFs, or one block with two conditions, would
    produce a complexity of 3.
    Function Metrics

    View Slide

  19. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 19
    Cyclomatic Complexity
    CC = Edge - Node + 2
    Or
    CC = ConditionalNodes + 1

    View Slide

  20. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 20
    i = 0; n=4;
    while (ij = i + 1;
    while (jif (A[i]swap(A[i], A[j]);
    }
    i=i+1;
    }
    // CC = 9 - 7 + 2 = 4
    // CC = 3 + 1 = 4 (Condition nodes are 1,2 and 3 nodes)
    // A set of possible execution path of a program
    // 1, 7
    // 1, 2, 6, 1, 7
    // 1, 2, 3, 4, 5, 2, 6, 1, 7
    // 1, 2, 3, 5, 2, 6, 1, 7
    Cyclomatic Complexity

    View Slide

  21. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 21
    Complexity Number Meaning
    1-10 Structured and well written code
    High Testability
    Cost and Effort is less
    10-20 Complex Code
    Medium Testability
    Cost and effort is Medium
    20-40 Very complex Code
    Low Testability
    Cost and Effort are high
    >40 Not at all testable
    Very high Cost and Effort
    Cyclomatic Complexity

    View Slide

  22. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 22
    Example A
    § 3 files
    § 24 methods
    § 394 lines
    § 326 LOC
    § 285 eLOC
    § 182 lLOC
    § 20 Lcomments
    § Comments 5.1%
    § Blank lines 12.2%
    § Spaces: 21.0% (79% code)
    § Max CC: 6
    § Average CC: 1.46
    Test Yourselves
    Example B
    • 5 files
    • 21 methods
    • 522 lines
    • 412 LOC
    • 356 eLOC
    • 276 lLOC
    • 72 Lcomments
    • Comments 13.8%
    • Blank lines 7.3%
    • Spaces: 26.7%
    • Max CC: 7
    • Average CC: 2.24

    View Slide

  23. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 23
    Example Sprint 2

    View Slide

  24. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 24
    Example Sprint 3

    View Slide

  25. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 25
    What can you say?
    • Complexity of the Product
    • Work/Time/Payment to the team ?
    • Do you hire them?
    • Understandability of the code?

    View Slide

  26. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 26
    Chapter 24
    Reference

    View Slide

  27. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 27
    Questions

    View Slide

  28. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 28
    Office Hours
    Tuesday and Thursday 3 - 5 pm
    But an appointment required
    Sent me an email – [email protected]

    View Slide

  29. jgs

    View Slide

  30. jgs
    CSC 309
    Software Engineering II
    Lab 02:
    RSM Tool
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    Building 14 -227
    Office Hours: By appointment

    View Slide

  31. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 31
    Your CSC 308 Project
    Box
    Name2
    class Name1 {
    aaa
    bbb
    }
    class Name2 {
    method(){
    Name1
    Cat
    }
    }
    class Cat
    extends Name1 {
    Car
    hello(){
    }
    move(){
    }
    }
    Cat
    Car hello
    move
    Name1
    aaa
    bbb

    View Slide

  32. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 32
    Think about this
    Calculate metrics for your final
    project in CSC 308

    View Slide

  33. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 33
    Resource Standard Metrics (RSM) is a source code metrics and quality
    analysis tool for ANSI C, ANSI C++, C# and Java for use on all Windows* and
    UNIX operating systems.
    1. Download RSM 7.75 Trial (Windows) here:
    http://msquaredtechnologies.com/
    RSM-Download.html
    * Install the Windows version. It includes a easy to use friendly interface (RSM
    Wizard).
    Tool

    View Slide

  34. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 34
    2. Review the user manual here
    http://msquaredtechnologies.com
    /m2wizard/RSM_Wizard_Manual.htm
    Disclaimer:
    It is a FREE tool…
    Issues counting LOCs, eLOCs,
    Issues counting conditions (syntaxis analysis)
    BUT ±0..9 does not affect in biggest projects 🧐
    Resource Standard Metrics

    View Slide

  35. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 35
    Resource Standard Metrics

    View Slide

  36. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 36
    Resource Standard Metrics

    View Slide

  37. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 37
    For your CSC 308 project, calculate and report the following in a table:
    § Total Number of Files,
    § Total Number of methods,
    § Average Number of methods per File
    § Total Number of lines
    § LOC, eLOC, lLOC, Lines of comments (Total and Average per File)
    § % Comments
    § %Blank lines
    § %Spaces,
    § Max Cyclomatic Complexity (CC)
    § Max Cyclomatic Complexity (CC)
    § Average Cyclomatic Complexity (CC)
    § Parameters (Total, Max, Average)
    § Return points (Total, Max, Average)
    Write a conclusion in 2 or 3 paragraphs explaining
    the qualities of your project, both positive ones and those that
    you would consider improving after reading these numbers.
    Work this with your team, and only one team member will submit it.
    Add all your names to the document (file) that you are submitting.
    Homework

    View Slide

  38. jgs
    Javier Gonzalez-Sanchez | CSC 309 | Winter 2023 | 38
    Let’s Work

    View Slide

  39. jgs
    CSC 309 Software Engineering II
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Winter 2023
    Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly.
    They cannot be distributed or used for another purpose.

    View Slide