Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS540 Software Design Lecture 3 & 4 1 Lecture 3 & 4: Modularity, Coupling, Cohesion and Abstraction Anita S. Malik Adapted from Schach.

Similar presentations


Presentation on theme: "CS540 Software Design Lecture 3 & 4 1 Lecture 3 & 4: Modularity, Coupling, Cohesion and Abstraction Anita S. Malik Adapted from Schach."— Presentation transcript:

1 CS540 Software Design Lecture 3 & 4 1 Lecture 3 & 4: Modularity, Coupling, Cohesion and Abstraction Anita S. Malik anitamalik@umt.edu.pk Adapted from Schach (2002) Chapter 7

2 CS540 Software Design 2Lecture 3 & 4 Modules What is a module? What is a module? A lexically contiguous sequence of program statements, bounded by boundary elements, with an aggregate identifier A lexically contiguous sequence of program statements, bounded by boundary elements, with an aggregate identifier Examples of boundary elements are begin…end pairs or {…} pairs Examples of boundary elements are begin…end pairs or {…} pairs Procedures, functions in classical paradigm are modules. In object-oriented paradigm an object is a module and so is the method within an object Procedures, functions in classical paradigm are modules. In object-oriented paradigm an object is a module and so is the method within an object

3 CS540 Software Design 3Lecture 3 & 4 Example - Design of Computer Lets consider an example to understand the importance of modularization Lets consider an example to understand the importance of modularization

4 CS540 Software Design 4Lecture 3 & 4 Design of Computer (contd) A competent architect designs 3 silicon chips as shown in the diagram A competent architect designs 3 silicon chips as shown in the diagram

5 CS540 Software Design 5Lecture 3 & 4 Design of Computer (contd) A highly incompetent computer architect decides to build an ALU, shifter and 16 registers with AND, OR, and NOT gates, rather than NAND or NOR gates.A highly incompetent computer architect decides to build an ALU, shifter and 16 registers with AND, OR, and NOT gates, rather than NAND or NOR gates. Resulting “masterpiece”Resulting “masterpiece”

6 CS540 Software Design 6Lecture 3 & 4 Computer Design (contd) The two designs are functionally equivalent The two designs are functionally equivalent Second design is Second design is Hard to understand Hard to understand Hard to locate faults Hard to locate faults Difficult to extend or enhance Difficult to extend or enhance Cannot be reused in another product Cannot be reused in another product Software Modules must be like the first design Software Modules must be like the first design Maximal relationships within modules and minimal relationships between modules Maximal relationships within modules and minimal relationships between modules

7 CS540 Software Design 7Lecture 3 & 4 Composite/Structured Design Method for breaking up a product into modules for Method for breaking up a product into modules for Maximal interaction within module, and Maximal interaction within module, and Minimal interaction between modules Minimal interaction between modules Module cohesion Module cohesion Degree of interaction within a module Degree of interaction within a module Module coupling Module coupling Degree of interaction between modules Degree of interaction between modules

8 CS540 Software Design 8Lecture 3 & 4 Function, Logic, and Context of module In C/SD, the name of a module is its function In C/SD, the name of a module is its function Example Example Module computes square root of double precision integers using Newton’s algorithm. Module is named compute square root Module computes square root of double precision integers using Newton’s algorithm. Module is named compute square root

9 CS540 Software Design 9Lecture 3 & 4 Cohesion Degree of interaction within a module Degree of interaction within a module Seven categories or levels of cohesion (non-linear scale) Seven categories or levels of cohesion (non-linear scale)

10 CS540 Software Design 10Lecture 3 & 4 1.Coincidental Cohesion A module has coincidental cohesion if it performs multiple, completely unrelated actions A module has coincidental cohesion if it performs multiple, completely unrelated actions Example Example print next line, reverse string of characters comprising second parameter, add 7 to fifth parameter, convert fourth parameter to floating point print next line, reverse string of characters comprising second parameter, add 7 to fifth parameter, convert fourth parameter to floating point Arise from rules like Arise from rules like “Every module will consist of between 35 and 50 statements” “Every module will consist of between 35 and 50 statements”

11 CS540 Software Design 11Lecture 3 & 4 Why is Coincidental Cohesion so Bad? Degrades maintainability Degrades maintainability Modules are not reusable Modules are not reusable This is easy to fix This is easy to fix Break into separate modules each performing one task Break into separate modules each performing one task

12 CS540 Software Design 12Lecture 3 & 4 2.Logical Cohesion A module has logical cohesion when it performs a series of related actions, one of which is selected by the calling module A module has logical cohesion when it performs a series of related actions, one of which is selected by the calling module Example 1 Example 1 function code = 7; new operation (op code, dummy 1, dummy 2, dummy 3); // dummy 1, dummy 2, and dummy 3 are dummy variables, // not used if function code is equal to 7 Example 2 Example 2 Module performing all input and output Module performing all input and output Example 3 Example 3 One version of OS/VS2 contained logical cohesion module performing 13 different actions. Interface contained 21 pieces of data One version of OS/VS2 contained logical cohesion module performing 13 different actions. Interface contained 21 pieces of data

13 CS540 Software Design 13Lecture 3 & 4 Why is Logical Cohesion so Bad? The interface is difficult to understand The interface is difficult to understand Code for more than one action may be intertwined Code for more than one action may be intertwined Difficult to reuse Difficult to reuse

14 CS540 Software Design 14Lecture 3 & 4 Why is Logical Cohesion so Bad? (contd) A new tape unit is installed A new tape unit is installed What is the effect on the laser printer? What is the effect on the laser printer?

15 CS540 Software Design 15Lecture 3 & 4 3.Temporal Cohesion A module has temporal cohesion when it performs a series of actions related in time A module has temporal cohesion when it performs a series of actions related in time Example Example open old master file, new master file, transaction file, print file, initialize sales district table, read first transaction record, read first old master record (a.k.a. perform initialization) open old master file, new master file, transaction file, print file, initialize sales district table, read first transaction record, read first old master record (a.k.a. perform initialization)

16 CS540 Software Design 16Lecture 3 & 4 Why is Temporal Cohesion so Bad? Actions of this module are weakly related to one another, but strongly related to actions in other modules. Actions of this module are weakly related to one another, but strongly related to actions in other modules. Consider sales district table Consider sales district table Not reusable Not reusable

17 CS540 Software Design 17Lecture 3 & 4 4.Procedural Cohesion A module has procedural cohesion if it performs a series of actions related by the procedure to be followed by the product A module has procedural cohesion if it performs a series of actions related by the procedure to be followed by the product Example Example read part number and update repair record on master file read part number and update repair record on master file

18 CS540 Software Design 18Lecture 3 & 4 Why is Procedural Cohesion so Bad? Actions are still weakly connected, so module is not reusable Actions are still weakly connected, so module is not reusable

19 CS540 Software Design 19Lecture 3 & 4 5.Communicational Cohesion A module has communicational cohesion if it performs a series of actions related by the procedure to be followed by the product, but in addition all the actions operate on the same data A module has communicational cohesion if it performs a series of actions related by the procedure to be followed by the product, but in addition all the actions operate on the same data Example 1 Example 1 update record in database and write it to audit trail update record in database and write it to audit trail Example 2 Example 2 calculate new coordinates and send them to terminal calculate new coordinates and send them to terminal

20 CS540 Software Design 20Lecture 3 & 4 Why is Communicational Cohesion so Bad? Still lack of reusability Still lack of reusability

21 CS540 Software Design 21Lecture 3 & 4 7.Informational Cohesion A module has informational cohesion if it performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data structure A module has informational cohesion if it performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data structure

22 CS540 Software Design 22Lecture 3 & 4 Why is Informational Cohesion so Good? Essentially, this is an abstract data type Essentially, this is an abstract data type

23 CS540 Software Design 23Lecture 3 & 4 7.Functional Cohesion Module with functional cohesion performs exactly one action Module with functional cohesion performs exactly one action Example 1 Example 1 get temperature of furnace get temperature of furnace Example 2 Example 2 compute orbital of electron compute orbital of electron Example 3 Example 3 write to floppy disk write to floppy disk Example 4 Example 4 calculate sales commission calculate sales commission

24 CS540 Software Design 24Lecture 3 & 4 Why is Functional Cohesion so Good? More reusable More reusable Corrective maintenance easier Corrective maintenance easier Fault isolation Fault isolation Fewer regression faults Fewer regression faults Easier to extend product Easier to extend product

25 CS540 Software Design 25Lecture 3 & 4 Cohesion Case Study

26 CS540 Software Design 26Lecture 3 & 4 Coupling Degree of interaction between two modules Degree of interaction between two modules Five categories or levels of coupling (non-linear scale) Five categories or levels of coupling (non-linear scale)

27 CS540 Software Design 27Lecture 3 & 4 1.Content Coupling Two modules are content coupled if one directly references contents of the other Two modules are content coupled if one directly references contents of the other Example 1 Example 1 Module a modifies statement of module b Module a modifies statement of module b Example 2 Example 2 Module a refers to local data of module b in terms of some numerical displacement within b Module a refers to local data of module b in terms of some numerical displacement within b Example 3 Example 3 Module a branches into local label of module b Module a branches into local label of module b

28 CS540 Software Design 28Lecture 3 & 4 Why is Content Coupling so Bad? Almost any change to b, even recompiling b with new compiler or assembler, requires change to a Almost any change to b, even recompiling b with new compiler or assembler, requires change to a Warning Warning Content coupling can be implemented in Ada through use of overlays implemented via address clauses Content coupling can be implemented in Ada through use of overlays implemented via address clauses

29 CS540 Software Design 29Lecture 3 & 4 2.Common Coupling Two modules are common coupled if they have write access to global data Two modules are common coupled if they have write access to global data Example 1 Example 1 Modules cca and ccb can access and change value of global variable Modules cca and ccb can access and change value of global variable

30 CS540 Software Design 30Lecture 3 & 4 2.Common Coupling (contd) Example 2 Example 2 Modules cca and ccb both have access to same database, and can both read and write same record Modules cca and ccb both have access to same database, and can both read and write same record Example 3 Example 3 FORTRAN common FORTRAN common COBOL common (nonstandard) COBOL common (nonstandard) COBOL-80 global COBOL-80 global

31 CS540 Software Design 31Lecture 3 & 4 Why is Common Coupling so Bad? Contradicts the spirit of structured programming Contradicts the spirit of structured programming The resulting code is virtually unreadable The resulting code is virtually unreadable

32 CS540 Software Design 32Lecture 3 & 4 Why is Common Coupling so Bad? (contd) Modules can have side-effects Modules can have side-effects This affects their readability This affects their readability Entire module must be read to find out what it does Entire module must be read to find out what it does Difficult to reuse Difficult to reuse Module exposed to more data than necessary Module exposed to more data than necessary

33 CS540 Software Design 33Lecture 3 & 4 3.Control Coupling Two modules are control coupled if one passes an element of control to the other Two modules are control coupled if one passes an element of control to the other Example 1 Example 1 Operation code passed to module with logical cohesion Operation code passed to module with logical cohesion Example 2 Example 2 Control-switch passed as argument Control-switch passed as argument

34 CS540 Software Design 34Lecture 3 & 4 Why is Control Coupling so Bad? Modules are not independent; module b (the called module) must know internal structure and logic of module a. Modules are not independent; module b (the called module) must know internal structure and logic of module a. Affects reusability Affects reusability Associated with modules of logical cohesion Associated with modules of logical cohesion

35 CS540 Software Design 35Lecture 3 & 4 4.Stamp Coupling Some languages allow only simple variables as parameters Some languages allow only simple variables as parameters part number part number satellite altitude satellite altitude degree of multiprogramming degree of multiprogramming Many languages also support passing of data structures Many languages also support passing of data structures part record part record satellite coordinates satellite coordinates segment table segment table

36 CS540 Software Design 36Lecture 3 & 4 4.Stamp Coupling (contd) Two modules are stamp coupled if a data structure is passed as a parameter, but the called module operates on some but not all of the individual components of the data structure Two modules are stamp coupled if a data structure is passed as a parameter, but the called module operates on some but not all of the individual components of the data structure

37 CS540 Software Design 37Lecture 3 & 4 Why is Stamp Coupling so Bad? It is not clear, without reading the entire module, which fields of a record are accessed or changed It is not clear, without reading the entire module, which fields of a record are accessed or changed Example Example calculate withholding (employee record) Difficult to understand Difficult to understand Unlikely to be reusable Unlikely to be reusable More data than necessary is passed More data than necessary is passed Uncontrolled data access can lead to computer crime Uncontrolled data access can lead to computer crime There is nothing wrong with passing a data structure as a parameter, provided all the components of the data structure are accessed and/or changed There is nothing wrong with passing a data structure as a parameter, provided all the components of the data structure are accessed and/or changed invert matrix (original matrix, inverted matrix); print inventory record (warehouse record);

38 CS540 Software Design 38Lecture 3 & 4 5.Data Coupling Two modules are data coupled if all parameters are homogeneous data items [simple parameters, or data structures all of whose elements are used by called module] Two modules are data coupled if all parameters are homogeneous data items [simple parameters, or data structures all of whose elements are used by called module] Examples Examples display time of arrival (flight number); display time of arrival (flight number); compute product (first number, second number); compute product (first number, second number); get job with highest priority (job queue); get job with highest priority (job queue);

39 CS540 Software Design 39Lecture 3 & 4 Why is Data Coupling so Good? The difficulties of content, common, control, and stamp coupling are not present The difficulties of content, common, control, and stamp coupling are not present Maintenance is easier Maintenance is easier

40 CS540 Software Design 40Lecture 3 & 4 Coupling Case Study

41 CS540 Software Design 41Lecture 3 & 4 Coupling Case Study (contd) Interface description Interface description

42 CS540 Software Design 42Lecture 3 & 4 Coupling Case Study (contd) Coupling between all pairs of modules Coupling between all pairs of modules

43 CS540 Software Design 43Lecture 3 & 4 Coupling Case Study (contd) Good design has high cohesion and low coupling Good design has high cohesion and low coupling What else characterizes good design? What else characterizes good design?


Download ppt "CS540 Software Design Lecture 3 & 4 1 Lecture 3 & 4: Modularity, Coupling, Cohesion and Abstraction Anita S. Malik Adapted from Schach."

Similar presentations


Ads by Google