Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coupling and Cohesion 1.

Similar presentations


Presentation on theme: "Coupling and Cohesion 1."— Presentation transcript:

1 Coupling and Cohesion 1

2 Desired Class/Object Interaction
Maximize internal interaction (Cohesion) Easier to understand Easier to test Minimize external interaction (Coupling) Can be used independently Easier to replace

3 Characteristics of Good Design
Component independence High Cohesion Low Coupling

4 What is Coupling? Coupling is a measure of the independence of components Coupling is related to Cohesion It is an indication of the strength of the inter-connections between the components in a design

5 Highly Coupled These types of systems have multiple inter-connections with components dependent upon each other Component A Component B Component C Component D

6 Loosely Coupled Loosely coupled systems are made up of components which are independent or almost independent Component A Component B Component C Component D

7 Uncoupled Uncoupled components have NO interconnections
No dependencies Component A Component B Component C Component D

8 The range of coupling measures
High Coupling Content Coupling Common Coupling Control Coupling Stamp Coupling Loose Coupling Data Coupling Uncoupled Low Coupling

9 Content Coupling A component directly references (shares) the content of another module Component p modifies a statement of component q Component p refers to local data of component q Content coupled components are inextricably interlinked Changes to component p requires a change to component q (including recompilation) Reusing component p requires using component q also

10 Common Coupling Using global variable (i.e. global coupling)
All components have read/write access to a global data block Components exchange data using the global data block (instead of arguments) A single component with write access where all other components have read access is not Common Coupling

11 Common Coupling (cont.)
Have to look at many components to determine the current state of a variable Side-effects require looking at all of the code in a function to see if there are any global effects Changes in one component to the declaration requires changes in all other components Identical list of global variables must be declared for component to be reused Component is exposed to more data than is needed

12 Control Coupling A component passes control parameters to coupled components May be good or bad depending upon the situation Bad when the component must be aware of internal structure and logic of another component Good if parameters allow factoring and reuse of functionality

13 Control Coupling Example
Acceptable A sort function that accepts a comparison function as an argument Component p calls component q and q returns a flag indicating an error (if any) occurred Unacceptable Component p calls component q and q returns a string indicating the error (if any) that occurred

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

15 Stamp Coupling Example
Address verification Address verification accepts a Customer data structure as an argument, “parses it” and verifies the address bool VerifyAddress (Customer customer) {…} Better bool VerifyAddress ( String address1, String address2, String city, String state, String zipcode) {…}

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

17 Cohesion 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 a component are directed toward and essential for performing the same task High is good

18 The range of Cohesion Functional High Cohesion Informational
Sequential Communicational Procedural Temporal Logical Coincidental Low Cohesion

19 Coincidental Cohesion
Parts of the component performs multiple, completely unrelated actions May be based on factors outside of the design Skillset or interest of developers Avoidance of small components No reusability Difficult corrective maintenance or enhancement Elements needed to achieve some functionality are scattered throughout the system Example: a “Utilities” class

20 Logical Cohesion Elements of components are related logically and not functionally Several logically related elements are in the same component and one of the elements is selected by the caller May include both high and low-level actions in the same class May include unused parameters for certain uses Interface is difficult to understand In order to do something you have to wade through a lot of unrelated possible actions Example: grouping all output (print, export) routines

21 Temporal Cohesion Elements of a component are related by timing – grouped together and used during the same phase of execution 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 Often happens in initialization or shutdown Example: a function which is called after catching an exception which closes an open file, creates an error log and notifies the user

22 Procedural Cohesion Elements of a component are related only to ensure a particular order of execution – procedures (methods) that are called one after another are kept together Actions are still weakly connected and unlikely to be reusable Changes to the ordering of steps or purpose of steps requires changing the component abstraction Example: a function which checks file permissions and then opens the file

23 Communicational Cohesion
Component 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 – procedures that access the same data are kept together Action based on the ordering of steps on all the same data Actions are related but still not completely separated Module cannot be reused

24 Sequential Cohesion Methods are together in a class because the output from one part is the input to another part Occurs naturally in functional programming Example: a function which reads data from a file and processed the data

25 Informational Cohesion
Component performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data Different from logical cohesion Each piece of code has a single entry and single exit In logical cohesion, actions of module intertwined

26 Functional Cohesion Every essential element to a single computation is contained in the component Every element in the component is essential to the computation Example: tokenizing a string of XML

27 ?

28 References Chawla, Jagnesh. “Cohesion & Coupling”
Pfleeger, S. “Software Engineering Theory and Practice” Prentice Hall 2001


Download ppt "Coupling and Cohesion 1."

Similar presentations


Ads by Google