CS4723 Software Engineering Lecture 12 Software Design Quality.

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Design Concepts and Principles
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
Requirements and Design
CS5103 Software Engineering Lecture 09 Software Design and Design Patterns.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1/19 Component Design On-demand Learning Series Software Engineering of Web Application - Principles of Good Component Design Hunan University, Software.
CS5103 Software Engineering Lecture 13 Software Refactoring Software Licenses.
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
Advanced Programing practices
Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
SOFTWARE DESIGN (SWD) Instructor: Dr. Hany H. Ammar
SOFTWARE DESIGN.
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Design engineering Vilnius The goal of design engineering is to produce a model that exhibits: firmness – a program should not have bugs that inhibit.
SE: CHAPTER 7 Writing The Program
Refactoring1 Refactoring DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY February 6, 2009.
Cohesion and Coupling CS 4311
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
Refactoring1 Improving the structure of existing code.
Software Design Design is the process of applying various techniques and principles for the purpose of defining a device, a process,, or a system in sufficient.
GRASP: Designing Objects with Responsibilities
Software Engineering Principles. SE Principles Principles are statements describing desirable properties of the product and process.
Chapter 3: Software Project Management Metrics
Refactoring: Improving the Design of Existing Code © Martin Fowler, Martin Fowler.
1 CMPT 275 High Level Design Phase Modularization.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Software Design: Principles, Process, and Concepts Getting Started with Design.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
CS5103 Software Engineering Lecture 02 More on Software Process Models.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Refactoring. Announcements HW7 due today, HW8 coming up tomorrow (I’m taking a late day on posting HW8) Grades and feedback for HW0-5 in Homework Server.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring1 Improving the structure of existing code.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Refactoring Improving code after it has been written.
Design CS 470 – Software Engineering I Sheldon X. Liang, PH.D.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Design Concepts ch-8
Principles and examples
7. Modular and structured design
CompSci 280 S Introduction to Software Development
Outline A. What is Refactoring? B. Why do we Refactor?
Lecture 9- Design Concepts and Principles
Refactoring II 21-Sep-18.
Refactoring and Code Smells
Software testing strategies 2
Refactoring.
CS223: Software Engineering
Reusability 11/29/2018© 2006 ITT Educational Services Inc.
Lecture 9- Design Concepts and Principles
Refactoring and Code Smells
Software Design Lecture : 9.
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Refactoring II 5-Feb-19.
Refactoring and Code Smells
Advanced Programing practices
Refactoring and Code Smells
Presentation transcript:

CS4723 Software Engineering Lecture 12 Software Design Quality

2 Four Approaches to Quality Assurance  Testing  Verification  Static Bug Detection  Review

3 Review  Once the major approach when testing is expensive  Design Review  Code Review

4 Software Design  Software design usually consist of two aspects  Architecture design  Detail design (component-level design)  Design patterns  Design classes  Other design issues  Refactoring  UI Design

5 Factors Affecting Software Design Quality  Fundamental software design factors  Modularity  Abstraction  Information hiding  Component independence  Fault prevention and fault tolerance

6 Modularity and Abstraction  When we consider modular solutions to any problems, many levels of abstraction can be posed At the highest level of abstraction, a solution is stated in broad terms of problem domain: architecture At the lower levels of abstraction, a more detailed description of the solution is provided: class diagrams  Modularity hides details and facilitates evolvement Each component hides a design decision from the others

7 Component Independent  We strive in most designs to make the components independent of one another, why?  We measure the degree of component independence using two concepts Low coupling High cohesion

8 Coupling and Cohesion  Coupling Two components are highly coupled when there is a great deal of dependence between them Two components are loosely coupled when they have some dependence, but the interconnections among them are weak Two components are uncoupled when they have no interconnections at all  Cohesion A component is cohesive if the internal parts of the component are related to each other and to its overall purpose

9 Decoupling  Most difficult part in design: Need tradeoff  Consider the following case:  Online and offline bookstore components  Both need shopping cart checkout  Should we have a shopping cart checkout module for both of them? Or have one, and let the two components to call the module?  Consider potential changes on overall book discount policy and specific book discount policy?  Potential solution for the dilemma: break the checkout module, still not perfect 

10 Fault defense & tolerance  Defensive design anticipates situations the might lead to problems Network Failure Data corruption Invalid user inputs  Tolerate runtime errors Exception handling Redundant components (distributed system or critical software) Timely error reporting

11 Criteria for Good Software Design  High-quality designs should have characteristics that lead to quality products Correct translation from the requirements specification Ease of understanding Ease of implementation Ease of testing Ease of modification

12 Criteria for Good Software Design  Architecture  Using suitable architectural styles or patterns  Loose-coupled components  Can be implemented in an evolutionary fashion  Classes at a suitable abstract level  Interfaces are clear and minimize the data transfer  Design using a effective notation

13 Software Design Evaluation and Validation  We check a design in two different ways Validation: the design satisfies all requirements specified by the customer Verification: the characteristics (quality) of a good design are incorporated  We use some techniques for helping us to perform verification and validation Measuring design quality

14 Measuring Software Design Quality  We check a design using a set of measures Coupling Cohesion Complexity Basic metrics: size Depths of relations Cyclomatic complexity

15 Coupling  Factors: couple (A, B)  c: number of fields of B type in A  d: number of methods in B called by A  e: number of methods refer to B  f: shared fields and methods (if A is subclass of B or vice versa)

16 Cohesion  Coupling between classes in the same module  Highest value: 1 for each pair of classes  We can measure cohesion by how far it goes to the highest possible value

17 Complexity  Cyclomatic Complexity  Measuring the complexity of a graph  M = E – N + 2P  E: edges, N: nodes, P: number of connected components  Can apply to Control flow graph, Dependence graph, Class Diagram…

18 Design Patterns  Become popular due to a book  Design Patterns: Elements of Reusable Object- Oriented Software  Gang of Four: Gamma, Erich; Richard Helm, Ralph Johnson, and John Vlissides  Provide solutions for common problems in micro-design

19 Software Refactoring: keep the design quality  So far we talked about issues to be considered during design  Principles  Measurements  Patterns  Considering these, you can try to find a best implementation for the current requirements  But requirements change quickly…

20 Software Refactoring  Definition:  Software refactoring the process of restructuring a software to improve its readability or extensibility, while keeping its behavior  Restructuring  Keeping its behavior  book  Refactoring- Improving the Design of Existing Code, Addison Wesley, Martin Fowler et al.

21 When to apply refactorings  Bad smells  Code duplication  Long method / Huge Class  Long parameter list  ….  As the software evolve, these smells automatically appears  Especially when you do extreme programming, which tries to ignore design issues at the beginning

22 Types of refactorings  Add/Remove methods / fields  Move methods / fields  Extract methods / interface  Rename  Replace temp with query  Pull up / Push down methods / fields  …

23 An example from Martin Fowler’s book  A program to calculate the bill of a video-store customer  The total amount depends on the movie renting prices, the number of movies rented and the renting dates  Class diagram

Example: Movie class public class Movie { private String title; private int priceCode; public Movie(String title, int priceCode) { this.title=title; this.priceCode = priceCode; } public String getTitle() { return this.title; } public int getPriceCode() { return this.priceCode; } public void setPriceCode(int priceCode) { this.priceCode = priceCode; }

Example: Rental class Rental public class Rental { private Movie movie; private int rentDay; public Rental(Movie movie, int rentDay) { this.movie = movie; this.rentDay = rentDay; } public int getDaysRented() { return Util.getCurrentDay() – this.rentDay ; } public Movie getMovie() { return this.movie; }

Example: Customer class Customer public class Customer { private String name; private List rentals =new ArrayList (); public Customer(String name) { this.name = name; } public void addRental(Rental rental) { this.rentals.addElement(rental); } public String getName() { return this.name; }

Example: Customer class(2) Customer public class Customer... public String statement() { double totalAmount = 0; String result = “ Rental Record for “ + getName() + “ \n ” ; for (Rental each : this.rentals) { double thisAmount = 0; // determine amounts for each line thisAmount = each.getMovie.getPriceCode() * each.getDaysRented(); totalAmount = totalAmount + thisAmount; result = result + each.getMovie.getName() + “ ” + each.getDaysRented() + “ ” + thisAmount + “ \n ” ; } // add footer lines result += “Amount owed is “+ totalAmount + “\n”; return result; }

28 Here comes the change  Add discount code for movies  Old – 80% price  Regular – 90% price  Popular – 100% price  Add a html statement for sending online bills

29 What to do?  Add conditional statements… for (Rental each : this.rentals) { double thisAmount = 0; // determine amounts for each line double discount = 1.0 if(each.getMovie().getDiscountCode() == DISCOUNT_OLD){ discount = 0.8; }else if(each.getMovie().getDiscountCode() == DISCOUNT_REG){ discount = 0.9 } thisAmount = each.getMovie().getPriceCode() * each.getDaysRented() * discount; totalAmount = totalAmount + thisAmount; result = result + each.getMovie.getName() + “ ” + each.getDaysRented() + “ ” + thisAmount + “\n”; }

30 What to do?  Copy and paste statement() and revise it to generate a htmlStatement() method  The code looks much worse now compared with the start point  Long statement method  Duplicate code

31 So after adding the two features, you plan to do refactoring  The main reason of the ugly code is ?  Statement() method is doing too many things (which are actually not parts of printing a statement)  Split it to three  Calculation of the amount on each line  Calculation of the amount sum  Print lines

32 Refactoring 1  1a: Extract method to getItemAmount()  It is actually something related to a rental  So 1b: Move it to rental, to reduce duty of Customer  1c: Rename it to getAmount() double discount = 1.0 if(each.getMovie().getDiscountCode() == DISCOUNT_OLD){ discount = 0.8; }else if(each.getMovie().getDiscountCode() == DISCOUNT_REG){ discount = 0.9 } thisAmount = each.getMovie().getPriceCode() * each.getDaysRented() * discount;

33 Refactoring 2  Extract method to getSumAmount()  It is relatively small, but since we need to copy the statement(), we should still extract this part to reduce duplication double totalAmount = 0;... totalAmount = totalAmount + thisAmount;

34 Results  So statement() becomes  Looks much cleaner, less harmful to copy  May do some further refactoring by extract the header, footer, but may cause more complexity  No perfect solutions, just trade off public String statement() { String result = “Rental Record for “ + getName() + “\n”; for (Rental each : this.rentals) { result = result + each.getMovie.getName() + “ ” + each.getDaysRented() + “ ” + each.getAmount() + “\n”; } // add footer lines result = result + “Amount owed is “+ getSumAmount() + “\n”; return result; }

35 Before applying refactoring  Finish the work at your hand  Usually bad smells come after you added a new feature or fixed a bug  Have a test suite  Make sure your code pass all test cases  Find out to what extent you can use automatic refactoring tools (which guarantees perserving behaviors)

36 Automatic refactoring  Refactoring involves lots of changes  Any change may cause bugs in the software  So, Most IDEs provide automatic refactoring tools  Make sure that the program behavior does not change after the refactoring  Use automatic refactoring whenever possible  Don’t do it by yourself

37 Behind automatic software refactoring tools  Automatic refactoring  Pre-conditions  e.g., the class you rename does not have a main method  e.g, the method you move should not have a side- effect on the class’s fields, otherwise the field must be visible by the moving destination  The transform  When pre-conditions are not satisfied, tools may refuse refactoring or give you choices

38 Review of Design Quality  Principles of Design  Measurements of Design Quality  Design Patterns  Software Refactorings