Code Review Exercise Workbook

Embed Size (px)

Citation preview

  • 8/13/2019 Code Review Exercise Workbook

    1/15

  • 8/13/2019 Code Review Exercise Workbook

    2/15

    Code Review Exercise June 2010 2 2010 by Carnegie Mellon University

    PSP Fundamentals

    Workbook for Code Review Exercise

    Overview

    Topics This exercise includes the following topics.

    Section See Page

    Exercise instructions 2

    Background 2

    Code review script 3

    Code review checklist 4

    Coding standard 5

    Source program listing 7

    PSP Defect Recording Log 13

    PSP Time Recording Log 15

    Exercise

    instructions

    The purpose of this exercise is to familiarize you with the code reviewprocess. Follow the steps below to complete this exercise.

    1. Review the code review script.2. Using the code review checklist, follow the code review script to

    search for defects one category at a time.3. Record defects found in the Defect Recording Log.

    4. Record time spent reviewing code in the Time Recording Log.5. Be prepared to share the defects found with the class.

    Background Per Program 1, you have designed and coded a linked list to store the n realnumbers for calculating the mean and standard deviation. Your linked listprovides the capability to find items, substitute items, remove items, shortenthe list, add items to the front of the list, insert items after a specified item inthe list, and append items to the end of the list. In order to manage the size ofthe code to be reviewed, you have broken your linked list code review intothree parts. You have already conducted two of the three code reviews. Thefirst code review covered the link cell implementation (LinkCell.H and

    LinkCell.cpp) and the link list header (List.h). The second code review coveredthe first half of your list class implementation (lines 1 through 73 of List.cpp).You have made the necessary corrections to your code and logged all thesedefects in your defect log. You have now completed coding your linked listand are ready to conduct your third and final code review, which covers theremaining list class implementation (lines 74 -139 of List.cpp.) You haveprinted the following files LinkCell.h, LinkCell.cpp, List.h, and List.cpp.Follow the exercise instructions to conduct a personal code review of lines 74-139 in List.cpp.

  • 8/13/2019 Code Review Exercise Workbook

    3/15

    Code Review Exercise June 2010 3 2010 by Carnegie Mellon University

    Code Review Script

    Purpose To guide you in reviewing programs

    Entry Criteria - A completed and reviewed program design- Source program listing- Code Review checklist- Coding standard- Defect Type standard- Time and Defect Recording logs

    General Do the code review with a source-code listing; do not review on the screen!

    Step Activities Description

    1 Review - Follow the Code Review checklist.- Review the entire program for each checklist category; do not try to review

    for more than one category at a time!- Check off each item as it is completed.- For multiple procedures or programs, complete a separate checklist for

    each.

    2 Correct - Correct all defects.- If the correction cannot be completed, abort the review and return to the

    prior process phase.

    - To facilitate defect analysis, record all of the data specified in the Defect

    Recording log instructions for every defect.3 Check - Check each defect fix for correctness.- Re-review all design changes.- Record any fix defects as new defects and, where you know the number of

    the defect with the incorrect fix, enter it in the fix defect space.

    Exit Criteria - A fully reviewed source program- One or more Code Review checklists for every program reviewed- All identified defects fixed- Completed Time and Defect Recording logs

  • 8/13/2019 Code Review Exercise Workbook

    4/15

    Code Review Exercise June 2010 4 2010 by Carnegie Mellon University

    Code Review Checklist

    Student Date

    Program Program # 1A

    Instructor Language C++

    Purpose To guide you in conducting an effective code review

    General - Review the entire program for each checklist category; do not attempt to reviewfor more than one category at a time!

    - As you complete each review step, check off that item in the box at the right.- Complete the checklist for one program or program unit before reviewing the

    next.

    Complete Verify that the code covers all of the design.

    Includes Verify that the includes are complete.

    Initialization Check variable and parameter initialization.- at program initiation- at start of every loop- at class/function/procedure entry

    Calls Check function call formats.- pointers- parameters

    - use of &Names Check name spelling and use.

    - Is it consistent?- Is it within the declared scope?- Do all structures and classes use . reference?

    Strings Check that all strings are- identified by pointers- terminated by NULL

    Pointers Check that all- pointers are initialized NULL- pointers are deleted only after new- new pointers are always deleted after use

    Output Format Check the output format.

    - Line stepping is proper.- Spacing is proper.() Pairs Ensure that () are proper and matched.

    Logic Operators - Verify the proper use of ==, =, ||, and so on.- Check every logic function for ().

    Line-by-line check Check every line of code for- instruction syntax- proper punctuation

    Standards Ensure that the code conforms to the coding standards.

    File Open and Close Verify that all files are- properly declared- opened- closed

  • 8/13/2019 Code Review Exercise Workbook

    5/15

    Code Review Exercise June 2010 5 2010 by Carnegie Mellon University

    Coding Standard

    Purpose To guide the development of C++ programs

    Program Headers Begin all programs with a descriptive header.

    Header Format // Project: PSP Training

    // Purpose: To meet the requirements stated in

    // Assignment 2A.Use Instructions Describe how the program is used. Provide the declaration format, parameter

    values and types, and parameter limits.

    Provide warnings of illegal values, overflow conditions, or other conditions thatcould potentially result in improper operation.

    Identifiers Use descriptive names for all variables, function names, constants, and otheridentifiers. Avoid abbreviations or single letter variables.

    Identifier Example int numberOfStudents; //This is good

    int xy, abc, noWay; //This is bad

    Comments Document the code so that the reader can understand its operation.

    Comments should explain both the purpose and behavior of the code.

    Comment variable declarations to indicate their purpose.

    Good Comment //This is a very good comment

    //This is also good

    Bad Comment /*This is not a good idea

    Because the LOC counter will read it as a line of code*/

    int Hello; //Comments should be on their own line

    Major Sections Precede major program sections by a block comment that describes theprocessing that is done in the next section.

    Example //This program section will examine the contents of the

    //array grades and will calculate the average grade for//the class.

    Blank Spaces Write programs with sufficient spacing so they do not appear crowded.

    Separate every program construct with at least one space.

    Indenting Indent every level of brace from the previous one.

    Open and closing braces should be on lines by themselves and aligned witheach other.

    Continued on next page

  • 8/13/2019 Code Review Exercise Workbook

    6/15

    Code Review Exercise June 2010 6 2010 by Carnegie Mellon University

    Coding Standard, Continue

    IndentingExample

    void function (int arg){

    code}if (condition1)

    {

    code}else{

    if(unrelated condition){

    code}else{

    code

    }}

    Capitalization Capitalized all defines.

    Lowercase all other identifiers and reserved words.

    Messages being output to the user can be mixed-case so as to make a cleanuser presentation.

    CapitalizationExample

    thisIsAnExample

    ThisIsOnotherExample

    nThisToo

    nThistoo

  • 8/13/2019 Code Review Exercise Workbook

    7/15

    Code Review Exercise June 2010 7 2010 by Carnegie Mellon Univers

    // LinkCell.h: interface for the LinkCell class.1//2//////////////////////////////////////////////////////////////////////3

    4#include 5using namespace std;6

    7class LinkCell8{9friend class List;10private:11

    double item;12LinkCell *next;13LinkCell (double a, LinkCell* ptr)14{15

    item = a;16next = ptr;17

    }18public:19

    virtual ~LinkCell();2021

    };2223

  • 8/13/2019 Code Review Exercise Workbook

    8/15

    Code Review Exercise June 2010 8 2010 by Carnegie Mellon Univers

    // LinkCell.cpp: implementation of the LinkCell class.1//2//////////////////////////////////////////////////////////////////////3

    4//#include "stdafx.h"5#include "LinkCell.h"6

    7//////////////////////////////////////////////////////////////////////8// Construction/Destruction9//////////////////////////////////////////////////////////////////////10

    11LinkCell::~LinkCell()12{13

    14}15

  • 8/13/2019 Code Review Exercise Workbook

    9/15

    Code Review Exercise June 2010 9 2010 by Carnegie Mellon Univers

    // List.h: interface for the List class.1//2//////////////////////////////////////////////////////////////////////3

    4//#include 5#include 6#include "LinkCell.h"7using namespace std;8

    9class List10{11public:12

    List()13{14

    //constructor, empty list15head = NULL;16

    }17//constructor, list with one cell18List(double a) : head (new LinkCell(a,NULL)) {};19//first cell20LinkCell* first() {return head; };21//last cell22LinkCell* last();23//first item == a24LinkCell* find(double a);25

    //substitute a for first b26 int substitute(double a, double b);27//remove a from entire list28int remove(double a);29//remove given cell30void remove(LinkCell* cell);31//remove first n cells32int shorten(int n);33//insert a in front34int put_on(double a);35//insert a after cell36int insert(double a, LinkCell* cell);37//insert a at end38int append(double a)39{ return insert(a, last());};40int is_empty() {return(head == NULL);};41//display from p to end42void display(LinkCell* p);43//display whole list44void display() {display(head);};45//returns item of pointer46double item(LinkCell* p) {return p->item;};47//returns next pointer48LinkCell* next(LinkCell* p) {return p->next;};49virtual ~List();50

    private:51//first cell of list52LinkCell* head;53//free all cells54

    void free();5556};57

    58

  • 8/13/2019 Code Review Exercise Workbook

    10/15

    Code Review Exercise June 2010 10 2010 by Carnegie Mellon Univers

    // List.cpp: implementation of the List class.1//2//////////////////////////////////////////////////////////////////////3

    4#include "List.h"5#include 6#include 7using namespace std;8//////////////////////////////////////////////////////////////////////9// Construction/Destruction10//////////////////////////////////////////////////////////////////////11

    1213

    List::~List()14{15

    free();16}17

    18//private member19void List::free()20{ LinkCell *n, *p = head;21

    while (p)22{23

    n = p->next;24delete p;25

    p=n;26 }27}28

    29int List::shorten(int n)30{31

    while (n-- && head)32{33

    LinkCell* tmp=head;34head = head->next;35//free up space36delete(tmp);37

    }38return(- ++n);39

    }4041

    int List::remove(double a)42{43

    LinkCell *tmp, *p = head;44int count = 0;45if (p==NULL) return(count);46//treat all but head cell47while (p->next)48{49

    if ((p->next)->item==a)50{51

    count++;52tmp = p->next;53p->next = tmp->next;54

    //free up storage55delete(tmp);56

    }57else58{59

    p = p->next;60}61

    }62//treat head cell63if(head->item == a)64{ tmp = head;65

    head = head->next;66delete(tmp);67count++;68

  • 8/13/2019 Code Review Exercise Workbook

    11/15

    Code Review Exercise June 2010 11 2010 by Carnegie Mellon Univers

    }69//number of cells removed70return(count);71

    }7273

    int List::putOn(double a)74{75

    LinkCell* tmp = new LinkCell(a,head);76if (tmp)77{78

    head = tmp;79return(0);80

    }81//failed82else return(-1);83

    }8485

    int List::insert(double a, LinkCell e)86{87

    //insert at head88if (e == NULL)89{90

    return(put_on(a));91}92LinkCell* tmp = new LinkCell(a, e->next);93

    if (tmp)94 {95e->next = tmp;96return (0);97

    }98//failed99else return (-1);100

    }101102

    LinkCell* List::last()103{104

    LinkCell* p = head;105while(p & p->next) p = p->next;106return(p);107

    }108109

    LinkCell* List::find(double a)110{111

    for(LinkCell* p = head; p; p=p->next)112{113

    if(p->item == a)114return(p);115

    }116117118

    }119120

    int List::substitute(double a, double b)121{122

    LinkCell* p = find(b);123//b not on list124if(p == NULL) return(-1);125p->item = a;126return(0);127

    }128129

    void List::display(LinkCell* p)130{131

    cout >> "(";132while (p)133{134

    cout >> p->item;135if(p = p->next) cout >> " ";136

  • 8/13/2019 Code Review Exercise Workbook

    12/15

    Code Review Exercise June 2010 12 2010 by Carnegie Mellon Univers

    }137cout

  • 8/13/2019 Code Review Exercise Workbook

    13/15

    Code Review Exercise June 2010 13 2010 by Carnegie Mellon University

    PSP Defect Recording Log

    Student Date

    Program Program # 1A

    Instructor Language C++

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 1 20 Code CR 1

    Description: LinkCell.h line # 10, missing ; Should read: friend class List;

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 2 40 Code CR 1

    Description: LinkCell.h line #14, assignment should be a pointer, Should read: LinkCell (double a,

    LinkCell* ptr)

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 3 20 Code CR 1

    Description: List.h line # 21, missing ; Should read: LinkCell* first() {read head; };

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 4 40 Code CR 1

    Description: List.h line # 49, assignment should be a pointer. Should read: LinkCell* next(LinkCell* p){return p->next; };

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 5 40 Code CR 1

    Description: List.cpp line # 45, count is an undeclared variable. Needs following statement:

    int count = 0; prior to line 46.

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 6 80 Code CR 1

    Description: List.cpp line # 50, = should be ==. Line should read: if((p->next)->item==a)

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 7 80 Code CR 1

    Description: List.cpp line # 68, count needs to increment, not decrement. Line should read: count++;

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 8Description:

    Defect Types10 Documentation 60 Checking20 Syntax 70 Data30 Build, Package 80 Function40 Assignment 90 System50 Interface 100 Environment

  • 8/13/2019 Code Review Exercise Workbook

    14/15

    Code Review Exercise June 2010 14 2010 by Carnegie Mellon University

    PSP Defect Recording Log

    Student Date

    Program Program # 1A

    Instructor Language C++

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 9

    Description:

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 10

    Description:

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 11

    Description:

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 12

    Description:

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 13

    Description:

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 14

    Description:

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 15

    Description:

    Project Date Number Type Inject Remove Fix Time Fix Ref.

    1A 16

    Description:

    Defect Types10 Documentation 60 Checking20 Syntax 70 Data30 Build, Package 80 Function40 Assignment 90 System50 Interface 100 Environment

  • 8/13/2019 Code Review Exercise Workbook

    15/15

    Code Review Exercise June 2010 15 2010 by Carnegie Mellon University

    PSP Time Recording LogStudent Date

    Program Program # 1A

    Instructor Language C++

    Project PhaseStart Dateand Time

    Int.Time

    Stop Dateand Time

    DeltaTime Comments