Coupling and Cohesion

Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.

Characteristics of Good Design
• Component independence
– High cohesion – Low coupling

• Exception identification and handling • Fault prevention and fault tolerance

Coupling: Degree of dependence among components

No dependencies

Loosely coupled-some dependencies

Highly coupled-many dependencies

High coupling makes modifying parts of the system difficult, e.g., modifying a component affects all the components to which the component is connected.

Range of Coupling
High Coupling Content Common Control Stamp Data Uncoupled Low Loose

g. jumps into the middle of a routine .. e.Content coupling • Definition: One component references contents of another • Example: – Component directly modifies another’s data – Component refers to local data of another component in terms of numerical displacement – Component modifies another’s code.

. component adds customer by directly modifying the contents of the data structure containing customer data. When customer not found.Example of Content Coupling-1 Part of program handles lookup for customer.

When customer not found. .Example of Content Coupling-2 Part of program handles lookup for customer. Improvement: When customer not found. component adds customer by directly modifying the contents of the data structure containing customer data. component calls the AddCustomer() method that is responsible for maintaining customer data.

Common Coupling • Definition: Two components share data – Global data structures – Common blocks • Usually a poor design choice because – Lack of clear responsibility for the data – Reduces readability – Difficult to determine all the components that affect a data element (reduces maintainability) – Difficult to reuse components – Reduces ability to control data accesses .

Gets data from multiple sources. . Supplies data to multiple sinks. Each source process writes directly to global data store. Each sink process reads directly from global data store.Example-1 Process control component maintains current data about state of operation.

Processes send data to and request data from data manager. Supplies data to multiple sinks.Example-2 Process control component maintains current data about state of operation. . Each source process writes directly to global data store. Gets data from multiple sources. Improvement Data manager component is responsible for data in data store. Each sink process reads directly from global data store.

– Bad when component must be aware of internal structure and logic of another module – Good if parameters allow factoring and reuse of functionality . • May be either good or bad. depending on situation.Control Coupling • Definition: Component passes control parameters to coupled components.

. writes a specific message. as a result.Example • Acceptable: Module p calls module q and q passes back flag that says it cannot complete the task. then q is passing data • Not Acceptable: Module p calls module q and q passes back flag that says it cannot complete the task and.

. not lazy programmer.g.Stamp Coupling • Definition: Component passes a data structure to another component that does not have access to the entire structure. needs to know about implementation) • May be necessary due to efficiency factors: this is a choice made by insightful designer. • Requires second component to know how to manipulate the data structure (e..

address.Example-1 Customer billing system The print routine of the customer billing accepts a customer data structure as an argument. . parses it. and prints the name. and billing information.

address.Example-2 Customer Billing System The print routine of the customer billing accepts a customer data structure as an argument. parses it. Improvement The print routine takes the customer name. address. and billing information. . and prints the name. and billing information as an argument.

• Every argument is simple argument or data structure in which all elements are used • Good. • Easy to write contracts for this and modify component independently.Data Coupling • Definition: Two components are data coupled if there are homogeneous data items. . if it can be achieved.

.Key Idea in Object-Oriented Programming • Object-oriented designs tend to have low coupling.

Cohesion • Definition: The degree to which all elements of a component are directed towards a single task and all elements directed towards that task are contained in a single component. • Internal glue with which component is constructed • All elements of component are directed toward and essential for performing the same task • High is good .

Range of Cohesion High Cohesion Functional Informational Sequential Communicational Procedural Temporal Logical Coincidental Low .

• Accidental • Worst form .Coincidental Cohesion • Definition: Parts of the component are only related by their location in source code • Elements needed to achieve some functionality are scattered throughout the system.

Example • Print next line • Reverse string of characters in second argument • Add 7 to 5th argument • Convert 4th argument to float .

. • Several logically related elements are in the same component and one of the elements is selected by the client component.Logical Cohesion • Definition: Elements of component are related logically and not functionally.

• Operations are related.Example-1 • A component reads inputs from tape. but the functions are significantly different. . disk. All the code for these functions are in the same component. and network.

The disk sub-class reads from disk. and network. . The network sub-class reads from the network. Operations are related. The tape sub-class reads from tape. Improvement • A device component has a read operation that is overridden by sub-class components. All the code for these functions are in the same component. disk.Example-2 • A component reads inputs from tape. but the functions are significantly different.

Temporal Cohesion • Definition: Elements of a component are related by timing. • Difficult to change because you may have to look at numerous components when a change in a data structure is made. . • Increases chances of regression fault • Component unlikely to be reusable.

Lots of different activities occur.Example-1 • A system initialization routine: this routine contains all of the code for initializing all of the parts of the system. all at init time. .

Lots of different activities occur. . • Each component initializes itself at component instantiation time. all at init time.Example-2 • A system initialization routine: this routine contains all of the code for initializing all of the parts of the system. Improvement • A system initialization routine sends an initialization message to each component.

Procedural Cohesion • Definition: Elements of a component are related only to ensure a particular order of execution. • Actions are still weakly connected and unlikely to be reusable .

..Example . . • May be useful to abstract the intent of this sequence. Make the data base and repair record components handle reading and updating. Read part number from data base update repair record on maintenance file. ... Make component that handles more abstract operation.

Communicational Cohesion • Definition: Module performs a series of actions related by a sequence of steps to be followed by the product and all actions are performed on the same data .

• record.Update (record). • database.Print(). .Example • Update record in data base and send it to the printer.

Sequential Cohesion • The output of one component is the input to another. • Occurs naturally in functional programming languages • Good situation .

with independent code for each action. all performed on the same data.Informational Cohesion • Definition: Module performs a number of actions. actions of module intertwined • ADT and object-oriented paradigm promote . • Different from logical cohesion – Each piece of code has single entry and single exit – In logical cohesion. each with its own entry point.

• Ideal situation. • Every element in the component is essential to the computation.Functional Cohesion • Definition: Every essential element to a single computation is contained in the component. .

Examples of Cohesion-1 Function A Function Function B C Function Function D E logic Function A Function A’ Function A’’ Time t0 Time t0 + X Time t0 + 2X Coincidental Parts unrelated Logical Similar functions Function A Function B Function C Temporal Related by time Procedural Related by order of functions .

related functions .Examples of Cohesion-2 Function A Function B Function C Function A Function B Function C Communicational Access same data Sequential Output of one is input to another Function A part 1 Function A part 2 Function A part 3 Functional Sequential with complete.

Problem: Define coupling between pairs of modules. u access the same 6 database in update mode Interface Description . p 1 q 3 r 5 t 6 u 4 s 2 No. t. In 1 Aircraft type 2 3 4 5 ----Function code ----Part number Part number Out Status flag List of aircraft parts ----List of aircraft parts Part manufacturer Part name p.

Coupling between pairs of modules q p q r s t ----------- r s t u .

or temperature field . time. and temperature • Store record for specific site • Edit site. time.Problem: Classify cohesion for each module • Compute average daily temperatures at various sites • Initialize sums and open files • Create new temperature record • Store temperature record • Close files and print average temperatures • Read in site.

• P4: Produce an example of each type of coupling. Justify your answers. Justify your answers.In Class • P1: What is the effect of cohesion on maintenance? • P2: What is the effect of coupling on maintenance? • P3: Produce an example of each type of cohesion. .

Sign up to vote on this title
UsefulNot useful