Chapter 10 Communication between modules cohesion and coupling

Chapter 10 Communication between modules, cohesion and coupling

Objectives • To introduce communication between modules • To develop solution algorithms that pass parameters between modules • To introduce cohesion as a measure of the internal strength of a module • To introduce coupling as a measure of the extent of information interchange between modules

10. 1 Communication between modules

Communication between modules • Necessary to consider flow of information between modules • This flow of information is called ‘intermodule communication’ and can be accomplished by the scope of the variable

Communication between modules • Scope of a variable – The portion of a program in which that variable has been defined and to which it can be referenced – Variables can be global where the scope of the variable is the whole program – Scope of the variable is simple the module which it is defined

Communication between modules • Global data – Date that can be used by all the modules in a program – Every module in the program can access and change data – Lifetime of a global variable spans the execution of the whole program

Communication between modules • Local data – Variable are defined within the submodule are called local variables – The scope of a local variable is simply the module in which it is defined – The lifetime of a local variable is limited to the execution of the single submodule in which it is defined

Communication between modules • Side effects – Side effect is a form of a crosscommunication of a module with other parts of a program, – Occurs when a subordinate module alters the value of a global variable inside a module

Communication between modules • Passing parameters – Parameters are simply data items transferred from a calling module to its subordinate module at the time of calling – To pass parameters between modules, two things can happen: • The calling module must name the parameters that it wants to pass to the submodule • The submodule must be able to receive those parameters and return them to the calling module if required

Communication between modules • Formal and actual parameters – Parameters names that appear when a submodule is defined are known as formal parameters – Variables and expressions that are passed to a submodule in a particular call are called actual parameters

Communication between modules • Value and reference parameters – Parameters may have one of three function: 1. To pass information from a calling module to a subordinate module 2. To pass information from a subordinate module to its calling module 3. To fulfil a two-way communication role

Communication between modules • Value and reference parameters – Value parameters • Value parameters pass a copy of the value of a parameter from one module to another – Reference parameters • Reference parameter pass the memory address of a parameter from one module to another

Communication between modules • Hierarchy charts and parameters – Data parameters contain actual variables or data items that will be passed between modules – Status parameters act as a program flag and should contain just one of two values; true or false Data parameters Status parameters

10. 3 Module cohesion

Module cohesion • Cohesion is a measure of the internal strength of a module • It indicates how closely the elements or the statements of a module are associated with each other • The more closely the elements of a module are associated with each other, the higher the cohesion of the module

Module cohesion • Coincidental cohesion – Occurs when elements are collected into a module simply because they happen to fall together – Occur as a result of one of the following conditions: • Existing program may have been arbitrarily segmented into small modules • Existing program may have been arbitrarily subdivided to conform to a badly considered programming standard • A number of existing modules have been combined into one module

Module cohesion • Logical cohesion – Logical cohesion occurs when the element of a module are grouped together according to a certain class of activity – The element falls into some general category because they all do the same kind of thing

Module cohesion • Temporal cohesion – Occurs when the elements of a module are grouped together because they are related by time – Typical examples are initialisation and finalisation modules in which elements are placed together because they perform certain housekeeping functions at the beginning or end of a program

Module cohesion • Procedural cohesion – Occurs when the elements of a module are related because they operate according to a particular procedure – The elements are executed in a particular sequence so that the objectives of the program are achieved

Module cohesion • Communicational cohesion – Occurs when the element of a module are grouped together because they all operate on the same (central) piece of data – Are commonly found in business application because of the close relationship of a business program to the data it is processing

Module cohesion • Sequential cohesion – Occurs when a module contains elements that depend on the processing of previous elements – Contain elements in which the output data from one element serves as input data to the next

10. 4 Module coupling

Module coupling • Coupling is a measure of the extent of information interchange between modules • Tight coupling implies large dependence on the structure of one module by another • Loose coupling is the opposite of tight coupling. Modules with loose coupling are more independent and easier to maintain

Module coupling Global data • Common coupling structure – Occurs when modules reference the same global Module A Module B data structure • External coupling – Occurs when two or more modules access the same Global data variable global data variable (similar to common coupling except Module A Module B that the global data is an elementary data item, rather than a data structure)

Module coupling • Control coupling – Occurs when a module passes another module a control variable that is intended to control the other module’s logic • Stamp coupling – Occurs when one module passes a non-global data structure to another module in the form of a parameter Module A Module B Module A Data structure Module B

Module coupling • Data coupling – Occurs when a module passes a non-global data variable to another module (similar to stamp coupling except that the non-global data variable is an elementary data item, nota data structure) Module A Elementary data item Module B

Module coupling • Summary of coupling levels – If the programming language allows it, try to uncouple each module from its surroundings by 1. Passing data to a subordinate module in the form of parameters, rather than using global data 2. Writing each subordinate module as a self-contained unit

Summary • Introduced communication between modules and parameters. • Intermodule communication is the flow of information or data between modules. • Passing of parameters was introduced as a form of intermodule communication.

Summary • The differences between formal and actual parameters and value and reference parameters was explained. • Module cohesion and module coupling must be considered when designing modular programs. • Cohesion is a measure of the internal strength of a module. • Seven levels of cohesion were discussed.

Summary • Coupling is a measure of the extent of information interchange between modules. • Five levels of coupling were discussed.
- Slides: 30