CS540 Software Design Lecture 3 & 4 1
Lecture 3 & 4: Lecture 3 & 4: Modularity, Coupling, Modularity, Coupling,
Cohesion and Cohesion and AbstractionAbstraction
Anita S. MalikAnita S. [email protected]@umt.edu.pk
Adapted fromAdapted from Schach Schach (2002) Chapter 7 (2002) Chapter 7
CS540 Software Design 2Lecture 3 & 4
ModulesModules What is a module?What is a module?
A lexically contiguous sequence of A lexically contiguous sequence of program statements, bounded by boundary program statements, bounded by boundary elements, with an aggregate identifierelements, with an aggregate identifier
Examples of boundary elements are Examples of boundary elements are begin…end pairs or {…} pairsbegin…end pairs or {…} pairs
Procedures, functions in classical Procedures, functions in classical paradigm are modules. In object-oriented paradigm are modules. In object-oriented paradigm an object is a module and so is paradigm an object is a module and so is the method within an objectthe method within an object
CS540 Software Design 3Lecture 3 & 4
Example - Design of Example - Design of ComputerComputer
Lets consider an Lets consider an example to example to understand the understand the importance of importance of modularizationmodularization
CS540 Software Design 4Lecture 3 & 4
Design of Computer (contd)Design of Computer (contd)
A competent architect A competent architect designs 3 silicon chips as designs 3 silicon chips as shown in the diagramshown in the diagram
CS540 Software Design 5Lecture 3 & 4
Design of Computer (contd)Design of Computer (contd)
• A highly A highly incompetent incompetent computer architect computer architect decides to build an decides to build an ALUALU, shifter and 16 , shifter and 16 registers with registers with ANDAND, , OROR, and , and NOTNOT gates, gates, rather than rather than NANDNAND or or NORNOR gates. gates.
• Resulting Resulting “masterpiece”“masterpiece”
CS540 Software Design 6Lecture 3 & 4
Computer Design (contd)Computer Design (contd) The two designs are functionally The two designs are functionally
equivalentequivalent Second design isSecond design is
Hard to understandHard to understand Hard to locate faultsHard to locate faults Difficult to extend or enhanceDifficult to extend or enhance Cannot be reused in another productCannot be reused in another product
Software Modules must be like the first Software Modules must be like the first design design Maximal relationships within modules and Maximal relationships within modules and
minimal relationships between modulesminimal relationships between modules
CS540 Software Design 7Lecture 3 & 4
Composite/Structured Composite/Structured DesignDesign
Method for breaking up a product into Method for breaking up a product into modules formodules for Maximal interaction within module, and Maximal interaction within module, and Minimal interaction between modulesMinimal interaction between modules
Module cohesionModule cohesion Degree of interaction within a moduleDegree of interaction within a module
Module couplingModule coupling Degree of interaction between modulesDegree of interaction between modules
CS540 Software Design 8Lecture 3 & 4
Function, Logic, and Context Function, Logic, and Context of moduleof module
In C/SD, the name of a module is its In C/SD, the name of a module is its functionfunction
ExampleExample Module computes square root of double Module computes square root of double
precision integers using Newton’s precision integers using Newton’s algorithm. Module is named algorithm. Module is named compute compute square root square root
CS540 Software Design 9Lecture 3 & 4
CohesionCohesion Degree of interaction within a moduleDegree of interaction within a module Seven categories or levels of cohesion (non-Seven categories or levels of cohesion (non-
linear scale)linear scale)
CS540 Software Design 10Lecture 3 & 4
1.1. Coincidental CohesionCoincidental Cohesion
A module has coincidental cohesion if it A module has coincidental cohesion if it performs multiple, completely unrelated performs multiple, completely unrelated actionsactions
ExampleExample print next line, reverse string of characters print next line, reverse string of characters
comprising second parameter, add 7 to fifth comprising second parameter, add 7 to fifth parameter, convert fourth parameter to floating pointparameter, convert fourth parameter to floating point
Arise from rules likeArise from rules like ““Every module will consist of between 35 and Every module will consist of between 35 and
50 statements” 50 statements”
CS540 Software Design 11Lecture 3 & 4
Why is Coincidental Why is Coincidental Cohesion so Bad?Cohesion so Bad?
Degrades maintainabilityDegrades maintainability Modules are not reusableModules are not reusable This is easy to fixThis is easy to fix
Break into separate modules each Break into separate modules each performing one taskperforming one task
CS540 Software Design 12Lecture 3 & 4
2.2. Logical CohesionLogical Cohesion A module has logical cohesion when it performs a A module has logical cohesion when it performs a
series of related actions, one of which is selected by series of related actions, one of which is selected by the calling modulethe calling module
Example 1Example 1function code = 7;function code = 7;new operation (op code, dummy 1, dummy 2, new operation (op code, dummy 1, dummy 2, dummy 3);dummy 3);// dummy 1, dummy 2, and dummy 3 are dummy // dummy 1, dummy 2, and dummy 3 are dummy variables,variables,// not used if function code is equal to 7// not used if function code is equal to 7
Example 2Example 2 Module performing all input and outputModule performing all input and output
Example 3Example 3 One version of OS/VS2 contained logical cohesion One version of OS/VS2 contained logical cohesion
module performing 13 different actions. Interface module performing 13 different actions. Interface contained 21 pieces of datacontained 21 pieces of data
CS540 Software Design 13Lecture 3 & 4
Why is Logical Cohesion so Why is Logical Cohesion so Bad?Bad?
The interface is difficult to The interface is difficult to understandunderstand
Code for more than one action may Code for more than one action may be intertwinedbe intertwined
Difficult to reuseDifficult to reuse
CS540 Software Design 14Lecture 3 & 4
Why is Logical Cohesion so Bad? Why is Logical Cohesion so Bad? (contd)(contd)
A new tape unit is installedA new tape unit is installed What is the effect on the laser printer?What is the effect on the laser printer?
CS540 Software Design 15Lecture 3 & 4
3.3. Temporal CohesionTemporal Cohesion
A module has temporal cohesion A module has temporal cohesion when it performs a series of actions when it performs a series of actions related in timerelated in time
ExampleExample open old master file, new master file, transaction file, open old master file, new master file, transaction file,
print file, initialize sales district table, read first print file, initialize sales district table, read first transaction record, read first old master record transaction record, read first old master record (a.k.a. perform initialization)(a.k.a. perform initialization)
CS540 Software Design 16Lecture 3 & 4
Why is Temporal Cohesion Why is Temporal Cohesion so Bad?so Bad?
Actions of this module are weakly Actions of this module are weakly related to one another, but strongly related to one another, but strongly related to actions in other modules. related to actions in other modules. Consider Consider sales district tablesales district table
Not reusableNot reusable
CS540 Software Design 17Lecture 3 & 4
4.4. Procedural CohesionProcedural Cohesion
A module has procedural cohesion if A module has procedural cohesion if it performs a series of actions related it performs a series of actions related by the procedure to be followed by by the procedure to be followed by the productthe product
ExampleExample read part number and update repair record on read part number and update repair record on
master filemaster file
CS540 Software Design 18Lecture 3 & 4
Why is Procedural Cohesion Why is Procedural Cohesion so Bad?so Bad?
Actions are still weakly connected, Actions are still weakly connected, so module is not reusable so module is not reusable
CS540 Software Design 19Lecture 3 & 4
5.5. Communicational Communicational CohesionCohesion
A module has communicational cohesion A module has communicational cohesion if it performs a series of actions related if it performs a series of actions related by the procedure to be followed by the by the procedure to be followed by the product, but in addition all the actions product, but in addition all the actions operate on the same dataoperate on the same data
Example 1Example 1 update record in database and write update record in database and write itit to audit trail to audit trail
Example 2Example 2 calculate new coordinates and send calculate new coordinates and send themthem to to
terminalterminal
CS540 Software Design 20Lecture 3 & 4
Why is Communicational Why is Communicational Cohesion so Bad?Cohesion so Bad?
Still lack of reusabilityStill lack of reusability
CS540 Software Design 21Lecture 3 & 4
7.7. Informational CohesionInformational Cohesion
A module has informational cohesion A module has informational cohesion if it performs a number of actions, if it performs a number of actions, each with its own entry point, with each with its own entry point, with independent code for each action, all independent code for each action, all performed on the same data structureperformed on the same data structure
CS540 Software Design 22Lecture 3 & 4
Why is Informational Cohesion Why is Informational Cohesion so Good?so Good?
Essentially, this is an abstract data type Essentially, this is an abstract data type
CS540 Software Design 23Lecture 3 & 4
7.7. Functional CohesionFunctional Cohesion Module with functional cohesion performs exactly Module with functional cohesion performs exactly
one actionone action Example 1Example 1
get temperature of furnaceget temperature of furnace Example 2Example 2
compute orbital of electroncompute orbital of electron Example 3Example 3
write to floppy diskwrite to floppy disk Example 4Example 4
calculate sales commissioncalculate sales commission
CS540 Software Design 24Lecture 3 & 4
Why is Functional Cohesion Why is Functional Cohesion so Good?so Good?
More reusableMore reusable Corrective maintenance easierCorrective maintenance easier
Fault isolationFault isolation Fewer regression faultsFewer regression faults
Easier to extend productEasier to extend product
CS540 Software Design 25Lecture 3 & 4
Cohesion Case StudyCohesion Case Study
CS540 Software Design 26Lecture 3 & 4
CouplingCoupling Degree of interaction between two modulesDegree of interaction between two modules Five categories or levels of coupling (non-linear scale)Five categories or levels of coupling (non-linear scale)
CS540 Software Design 27Lecture 3 & 4
1.1. Content CouplingContent Coupling Two modules are content coupled if one directly Two modules are content coupled if one directly
references contents of the other references contents of the other
Example 1Example 1 Module a modifies statement of module bModule a modifies statement of module b
Example 2Example 2 Module a refers to local data of module b in Module a refers to local data of module b in
terms of some numerical displacement within bterms of some numerical displacement within b
Example 3Example 3 Module a branches into local label of module bModule a branches into local label of module b
CS540 Software Design 28Lecture 3 & 4
Why is Content Coupling Why is Content Coupling so Bad?so Bad?
Almost any change to Almost any change to bb, even , even recompiling recompiling bb with new compiler or with new compiler or assembler, requires change to assembler, requires change to aa
WarningWarning Content coupling can be implemented in Content coupling can be implemented in
Ada through use of overlays implemented Ada through use of overlays implemented via address clausesvia address clauses
CS540 Software Design 29Lecture 3 & 4
2.2. Common CouplingCommon Coupling Two modules are common coupled if they have write Two modules are common coupled if they have write
access to global dataaccess to global data
Example 1Example 1 Modules cca and ccb can access Modules cca and ccb can access and changeand change
value of global variablevalue of global variable
CS540 Software Design 30Lecture 3 & 4
2.2. Common Coupling Common Coupling (contd)(contd)
Example 2Example 2 Modules Modules ccacca and and ccbccb both have access to both have access to
same database, and can both read same database, and can both read andand writewrite same recordsame record
Example 3Example 3 FORTRAN FORTRAN commoncommon COBOL COBOL commoncommon (nonstandard) (nonstandard) COBOL-80 COBOL-80 globalglobal
CS540 Software Design 31Lecture 3 & 4
Why is Common Coupling Why is Common Coupling so Bad?so Bad?
Contradicts the spirit of structured programming Contradicts the spirit of structured programming The resulting code is virtually unreadableThe resulting code is virtually unreadable
CS540 Software Design 32Lecture 3 & 4
Why is Common Coupling Why is Common Coupling so Bad? (contd)so Bad? (contd)
Modules can have side-effectsModules can have side-effects This affects their readabilityThis affects their readability
Entire module must be read to find Entire module must be read to find out what it doesout what it does
Difficult to reuseDifficult to reuse Module exposed to more data than Module exposed to more data than
necessarynecessary
CS540 Software Design 33Lecture 3 & 4
3.3. Control CouplingControl Coupling
Two modules are control coupled if Two modules are control coupled if one passes an element of control to one passes an element of control to the other the other
Example 1Example 1 Operation code passed to module with Operation code passed to module with
logical cohesionlogical cohesion Example 2Example 2
Control-switch passed as argumentControl-switch passed as argument
CS540 Software Design 34Lecture 3 & 4
Why is Control Coupling so Why is Control Coupling so Bad?Bad?
Modules are not independent; moduleModules are not independent; module b b (the called module) must know internal (the called module) must know internal structure and logic of modulestructure and logic of module a a. . Affects reusabilityAffects reusability
Associated with modules of logical Associated with modules of logical cohesioncohesion
CS540 Software Design 35Lecture 3 & 4
4.4. Stamp CouplingStamp Coupling
Some languages allow only simple Some languages allow only simple variables as parametersvariables as parameters part numberpart number satellite altitudesatellite altitude degree of multiprogrammingdegree of multiprogramming
Many languages also support Many languages also support passing of data structures passing of data structures part recordpart record satellite coordinatessatellite coordinates segment tablesegment table
CS540 Software Design 36Lecture 3 & 4
4.4. Stamp Coupling (contd)Stamp Coupling (contd)
Two modules are stamp coupled if a Two modules are stamp coupled if a data structure is passed as a data structure is passed as a parameter, but the called module parameter, but the called module operates on some but not all of the operates on some but not all of the individual components of the data individual components of the data structurestructure
CS540 Software Design 37Lecture 3 & 4
Why is Stamp Coupling so Why is Stamp Coupling so Bad?Bad? It is not clear, without reading the entire module, It is not clear, without reading the entire module,
which fields of a record are accessed or changedwhich fields of a record are accessed or changed ExampleExample
calculate withholding (employee record)calculate withholding (employee record) Difficult to understandDifficult to understand Unlikely to be reusable Unlikely to be reusable More data than necessary is passedMore data than necessary is passed
Uncontrolled data access can lead to computer Uncontrolled data access can lead to computer crimecrime
There is nothing wrong with passing a data structure There is nothing wrong with passing a data structure as a parameter, provided as a parameter, provided allall the components of the the components of the data structure are accessed and/or changeddata structure are accessed and/or changed
invert matrix (original matrix, inverted matrix);invert matrix (original matrix, inverted matrix);print inventory record (warehouse record);print inventory record (warehouse record);
CS540 Software Design 38Lecture 3 & 4
5.5. Data CouplingData Coupling
Two modules are data coupled if all Two modules are data coupled if all parameters are homogeneous data parameters are homogeneous data items [simple parameters, or data items [simple parameters, or data structures all of whose elements are structures all of whose elements are used by called module]used by called module]
ExamplesExamples 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);
CS540 Software Design 39Lecture 3 & 4
Why is Data Coupling so Why is Data Coupling so Good?Good?
The difficulties of content, The difficulties of content, common, control, and stamp common, control, and stamp coupling are not presentcoupling are not present
Maintenance is easierMaintenance is easier
CS540 Software Design 40Lecture 3 & 4
Coupling Case StudyCoupling Case Study
CS540 Software Design 41Lecture 3 & 4
Coupling Case Study Coupling Case Study (contd)(contd)
Interface descriptionInterface description
CS540 Software Design 42Lecture 3 & 4
Coupling Case Study Coupling Case Study (contd)(contd)
Coupling between all pairs of modulesCoupling between all pairs of modules
CS540 Software Design 43Lecture 3 & 4
Coupling Case Study Coupling Case Study (contd)(contd)
Good design has high cohesion and Good design has high cohesion and low couplinglow coupling What else characterizes good design?What else characterizes good design?