Ch 7: From Modules to Objects CSCI 4320 What is a Module? • Sequence of program statements, bounded by boundary elements, having an aggregate identifier – “Boundary elements” • {...} • begin . . . end – “Aggregate identifier” • A name for the entire module • Examples – Header files – Class – Procedure/ Method Why Use Modules? • • • • Understandability Corrective Maintenance Enhancements Reuse Operation, Logic & Context of Modules • Operation: – WHAT a module does – “Compute the Square root of the argument” • Logic – HOW the module performs its operation – “Use Newton’s method of computing the square root” • Context – SPECIFIC USE of a module – Module m is used to compute the square root of an integer Design of Computer • A computer architect decides to build an ALU, shifter, and 16 registers with AND, OR, and NOT gates. Figure 7.1 Design of Computer (contd) • The architect designs 3 silicon chips Figure 7.2 Design of Computer (contd) • Redesign with one gate type per chip Figure 7.3 Computer Design (contd) • The two designs are functionally equivalent – The second design is • • • • Hard to understand Hard to locate faults Difficult to extend or enhance Cannot be reused in another product • Modules must be like the first design – Maximal relationships within modules, and – Minimal relationships between modules Structured Design for Modules • Design your modules to have HIGH cohesion and LOW coupling • Cohesion – Degree of interaction WITHIN a module • Coupling – Degree of interaction BETWEEN two modules Levels of Cohesion • • • • • • • Coincidental (bad) Logical Temporal Procedural Communicational Functional Information (good) Coincidental Cohesion • A module performs multiple, completely unrelated operations • May result when smaller modules are lumped together • Difficult to maintain & enhance • Cannot reuse func A (p1, p2, p3, p4) { 1. Print p1 2. Reverse characters of p2 3. Add 7 to p3 4. Convert p4 to a float } Logical Cohesion • A module performs a series of related operations, one of which is selected by calling the module • Interface is difficult to understand • Code for more than one module may be intertwined func B (opt, p1, p2, p3) { x:=0 if opt = 1 or 2 x := p1 + p2 if op = 2 x := x + p3 if op = 3 x := p3 if op = 4 x := 4 } Example: A module that performed 13 different operations; its interface contained 21 pieces of data Temporal Cohesion • A modules performs a Initialization Procedure() series of operations { related in time • The operations are related 1. Open old master file weakly to one another but 2. Print File more strongly to 3. Initialize District Table operations in other modules 4. Read Sales Table • High chance of regression } fault : Changing Sales Table structure affects multiple modules It is better to have Read_sale_table Operation with all other methods that use the Sales Table Procedural Cohesion • A module performs a series of operations related by the sequence of steps • Better than temporal because the actions have something to do with each other other than the fact they are performed at the same time. • However, the connection is still weak Procedure Update() { 1. Read part number from Database 2. Update repair record in Maintenance File } Communicational Cohesion A module 1. 2. Performs a series of operations related by the sequence of steps to be followed All the operations are performed on the same data Procedure Save (Record) { 1. Update Record in Database 2. Write Record to Audit Trail } Functional Cohesion – A module that performs exactly one operation or achieves a single goal – Maintenance is easy to perform Proc GetTemp() { Get Temperature of Furnace } Informational Cohesion A module – Performs a number of operations, each with its own entry point, – Independent code for each operation, all performed on the same data structure. – Example: – Abstract Data Type – Class Coupling The degree of interaction BETWEEN two modules – Content Coupling (bad) – Common Coupling – Control Coupling – Stamp Coupling – Data Coupling (Good) Content Coupling – One module directly references the contents of another – Example: – Uses GOTOs to reference different parts of modules • Result – : Almost any change to module q, even recompiling q with a new compiler or assembler, requires a change to module p Common Coupling – Both modules have access to the same global data – Does not communicated by passing arguments – A module can change the value of global data and it affects the other module – Difficult to reuse since the same list of global values must be used in future products – A module may be exposed to more data that strictly necessary Control Coupling – One module passes an element of control to the other module – (i.e., one module explicitly controls the logic of another) – Example – Control is passed when a function code is passed to a module with logical cohesion – DrawPicture (code) – The picture drawn depends on code – BAD CASE: If module P calls Q and Q informs P of what action to take – Q says “I am unable to complete my task” therefore write “ABC123” Stamp Coupling – A data structure is passed as an argument, but the called module only operates on some of the individual components of the structure – Example: – Passing a student record with name, address and ssn but only using Name PrintName (Student) – Passing an employee record with home # and salary but only calculating federal tax CalculateTax(Employee) • Passing more data that necessary can lead to uncontrolled data access Data Coupling – Every argument is either a single argument or a data structure in which all elements are used by the called module – DisplayArrivalTime (flightNumber) – Multiply (num1, num2, result) Data coupling is a desirable goal