George Blank University Lecturer. Object Oriented Code Inspection For Classroom Inspections in CIS 602 Java Programming.

Slides:



Advertisements
Similar presentations
Programming Paradigms and languages
Advertisements

Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
George Blank University Lecturer.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Testing Without Executing the Code Pavlina Koleva Junior QA Engineer WinCore Telerik QA Academy Telerik QA Academy.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Introduction To System Analysis and Design
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Software Testing and Quality Assurance
Lecture 4 Class Responsibility Collaboration Cards
© 2005 Prentice Hall8-1 Stumpf and Teague Object-Oriented Systems Analysis and Design with UML.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
COMS S1007 Object-Oriented Programming and Design in Java August 7, 2007.
SOS OOP Fall 2001 Object Oriented Programming in Java Week 1 Read a design Design a small program Extract a design Run a VAJ program Change that program,
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
George Blank University Lecturer.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Examining the Code [Reading assignment: Chapter 6, pp ]
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Introduction To System Analysis and design
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
1 Shawlands Academy Higher Computing Software Development Unit.
Object Oriented Programming
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction To System Analysis and Design
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Refactoring Improving the structure of existing code Refactoring1.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Refactoring1 Improving the structure of existing code.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Software Development
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
The Software Development Process
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Object Oriented Programming
Exceptions and Assertions Chapter 15 – CSCI 1302.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
Refactoring1 Improving the structure of existing code.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Further Investigations into the Development and Evaluation of Reading Techniques for Object-Oriented Code Inspection Alastair Dunsmore, Marc Roper and.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Software Testing.
Types for Programs and Proofs
Chapter 3: Using Methods, Classes, and Objects
Improving the structure of existing code
Anatomy of a Java Program
COP 3330 Object-oriented Programming in C++
Refactoring.
Presentation transcript:

George Blank University Lecturer

Object Oriented Code Inspection For Classroom Inspections in CIS 602 Java Programming

Code inspections Code inspections were a common way of finding defects in procedural programming. Inspections are difficult in object-oriented programming because issues like inheritance, dynamic binding, non-local effects and polymorphism make the structure of object oriented code non-linear and difficult to inspect.

Automated Inspection There are some tools available for the automated inspection of Java code. One such tool is the Code Inspection plug-in for IntelliJ IDEA. See ml ml See a list of tools available at source.net/open-source/code-analyzers source.net/open-source/code-analyzers Automated and manual inspections have some overlapping and some unique values.

XP inspections In eXtreme Programming, inspections are seen as a virtue. The philosophy is “if inspections are good, do them all the time.” The way this is accomplished in XP is through pair programming, in which all coding is done in pairs, with one person writing code and the other inspecting.

Value of Inspections In my personal experience in Bell Labs, manual inspections had two major benefits: They were excellent tools to improve quality by finding defects. They were also excellent for training programmers. It is for the second reason that I will use them in classroom sections of this course.

Distance Learning Students Since inspections require co-location, I cannot require them for DL students. But I do require you to prepare documents for them the same as classroom students, and I encourage you to get together with each other to do inspections and learn from them.

Preparing for Inspection Advance preparation makes inspections more effective. The following documents are required: A print out of the code to be inspected Planning documents for the classes and methods Abstract specifications for the classes and methods A summary of problems encountered while developing the code and their solutions.

Planning documents Any of the following planning documents are acceptable for this course: –A fully dressed use case –An interaction diagram –A JUnit test suite (test-first programming) –Story cards and Class Responsibility Collaboration (CRC) cards (XP) –Operation contracts (usually for methods) Choose the planning documents most appropriate for your code.

Fully Dressed Use Case

Interaction Diagrams There are two interaction diagrams—sequence diagrams and communication diagrams. They are equivalent—if you do one in a tool like Rational Rose, it can automatically draw the other. A sequence diagram is better for showing the order of operations, while a communication diagram is better for showing the messages for a particular object.

Sequence Diagram

Communication Diagram

Partial JUnit Test Suite for Queue

Story and CRC Cards Story cards and CRC cards are informal tools, usually handwritten, often used in eXtreme Programming. XP depends on having a SME (subject matter expert) available to provide additional details to the developers as they code. Story cards, like use cases, describe a particular scenario for the application. CRC cards, one per class, describe the class, its responsibilities, and tells what other classes it collaborates with.

Story Card Each payroll period, a check and check stub has to be created for each employee. Timesheets Supply hours worked and overtime for hourly workers plus sick time and holidays for all workers. Taxes are calculated from tax details, and deductions are computed for Medical Plan, Dental Plan, Retirement Plan, and 401K Plan. A permanent Record of the Paycheck is maintained in the Pay Ledger.

CRC (Class Responsibility Collaboration) Cards Class Name Superclasses Sub-classes ResponsibilitiesCollaborators Employee Person Manager, Salaried Worker, Hourly Worker Calculate PayTimesheet, Paycheck, Pay Ledger Tax Details Manage benefitsMedical Plan, Dental Plan. Retirement Plan, 401K Plan Manage TrainingTraining Record, Certification, Professional License Maintain recordsPosition, Employment History, Dependent

Operation Contract

Abstract Specifications Write an abstract specification for each class and method to be inspected, as well as any classes and methods called from within the class. The abstract specification should be brief and contain –A statement of the purpose of the class or method –A list of all state changes and outputs in terms of inputs and prior state.

Abstract Specification Class: Employee Purpose: Maintain information on an employee to be used for payroll, benefits, promotions, training, and legally required records Methods: newHire: create new Employee instance, change hire_date to date hired, add name, address, phone, position, social security number, create new Tax Details instance, create association with Employee instance addDependent: create new Dependent instance, create association with Employee instance (… etc)

Roles in Code Inspections Moderator – controls the process and timing. Does not ask questions or identify defects. Reader – the author of the code, who reads and explains each line, and responds to questions. Inspector – (can be several) asks questions and identifies defects. Recorder – (may be moderator) documents findings. Does not ask questions or identify defects.

Sample Inspection Report Date: November 17, 2005 Begin 2:30 pm End 4:00 pm Participants:Moderator G.Blank Reader T. Smith Inspectors P.Shah, A.Patel Recorder (moderator) Material Reviewed Size: 6 classes Author: T.Smith Name and Description: Multi Plotter, Homework # 7.3 for CIS 602 Inspection Outcome: No ReWorkReWork XX ReWork & Reinspection Defects Detected 12 Type 3Algorithm errors 1 Wrong Message …

Reading Java Code A Java application will begin in a method declared as: public static void main(String[] args) { The main method must be declared in an accessible public class. It will be in a class that has the same name as it’s class file (not a private or subordinate class.) Control will be transferred to other classes using the new class name with parameters.

Starting an Applet Applets cannot have a main method. Every applet needs at least one of these methods: init, start, or paint. The browser uses init to notify the applet that it has been loaded and start to tell the applet that it can begin executing. Anything displayed in the applet must use a paint method. If there is no init, the applet starts in start. If no start, then it starts in paint.

Starting a Thread A Java thread starts with a run method, usually by overriding the run method that is a default part of the thread class. Event handling can be confusing when you read code. Specific events trigger handlers specified by action listener methods.

Inspection Checklist The next few pages list a series of appropriate questions to ask at an inspection. There are many such checklists available on the web. I recommend that you use one in class.

Specification and design Has all functionality specified been implemented? Is there any excess functionality not specified? Does all of the specified functionality execute correctly?

Initialization and declarations Are all variables properly initialized before use? Are all variables and class members of the proper type and mode? Are all variables declared in the proper scope? Is a constructor called when a new object is required?

Naming conventions Are organizational standards being followed for naming classes, class members, variables, and parameters? Are the names properly meaningful, self- documenting, and appropriate?

Method Calls Are parameters listed in the proper order? Is the correct method called (or is there another one with a similar name) ? Are method return values used correctly?

Arrays Are there any off-by-one errors in array indexing? Are array indices protected against going out- of-bounds? Are constructors called when a new array item is required?

Comparing Objects Are all objects compared with.equals and not ‘==‘ ?

Formatting output Are there any spelling or grammar errors? Are new lines, spacing, and layout correct? Are applets the right size? Do applets use appropriate display and paint methods? Do applets display correctly?

Algorithms Are the order of operations, operator precedence, and parentheses correct? Is division by zero possible? Is there any integer division subject to improper rounding or truncation? Is there any implicit type conversion? Are comparison and Boolean operators correct?

Exceptions and Errors Is all necessary code encapsulated in try and catch blocks? Are all relevant exceptions caught? Are all caught exceptions handled properly? Can any legitimate operations be mischaracterized as errors or exceptions?

Flow of Control Are there any switch cases that are not properly terminated by break or return? Do all switch statements have a default branch? Are all loops properly formed with correct initialization, increment and termination expressions?

Files Are all files properly declared and opened? Are all files closed properly, even after an error? Are EOF conditions detected and properly handled? Are all file exceptions caught?

Security Are policy files provided where socket, port, or file access require them? Are policies limited by host, port, and directory, or do they give too much access? Is permission given for the access port (such as 80 for html) where needed?

Refactoring Is the code properly reusable where appropriate? Are classes, and methods of an appropriate size (not too small or large)? Are there any specific refactorings that should be performed? Is high cohesion and low coupling maintained? Is any code unnecessarily complex?

Typical Defects Improper use of class library Inheritance implementation error Wrong message Wrong object Override error Data flow error Misuse local variable Scope boundary error Domain knowledge Algorithm error Need refactoring Missing operation Unnecessary code Syntax error

Bad smells in code It takes significant coding experience to identify appropriate refactorings. Refactoring is based on “bad smells in code,” the recognition of practices that should be avoided. Some typical refactorings (from my Refactoring lecture) are listed on the next slide. All are worth understanding for effective code inspections.

A few Refactorings Add Parameter Change Association Reference to value Value to reference Collapse hierarchy Consolidate conditionals Procedures to objects Decompose conditional Encapsulate collection Encapsulate downcast Encapsulate field Extract class Extract Interface Extract method Extract subclass Extract superclass Form template method Hide delegate Hide method Inline class Inline temp Introduce assertion Introduce explain variable Introduce foreign method

Bibliography Alastair Dunsmore, Marc Roper, and Murray Wood, Practical Code Inspection for Object Oriented Systems, RoperWood.pdf RoperWood.pdf Martin Fowler, Refactoring, Addison Wesley, 1999