Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

12 couplingand cohesion-student

2,050 views

Published on

Published in: Technology
  • Be the first to comment

12 couplingand cohesion-student

  1. 1. Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
  2. 2. Characteristics of Good Design <ul><li>Component independence </li></ul><ul><ul><li>High cohesion </li></ul></ul><ul><ul><li>Low coupling </li></ul></ul><ul><li>Exception identification and handling </li></ul><ul><li>Fault prevention and fault tolerance </li></ul>
  3. 3. 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.
  4. 4. Range of Coupling High Coupling Loose Low Content Common Control Stamp Data Uncoupled
  5. 5. Content coupling <ul><li>Definition: One component references contents of another </li></ul><ul><li>Example: </li></ul><ul><ul><li>Component directly modifies another’s data </li></ul></ul><ul><ul><li>Component refers to local data of another component in terms of numerical displacement </li></ul></ul><ul><ul><li>Component modifies another’s code, e.g., jumps into the middle of a routine </li></ul></ul>
  6. 6. Example of Content Coupling-1 <ul><li>Part of program handles lookup for customer. </li></ul><ul><li>When customer not found, component adds customer by directly modifying the contents of the data structure containing customer data. </li></ul>
  7. 7. Example of Content Coupling-2 <ul><li>Part of program handles lookup for customer. </li></ul><ul><li>When customer not found, component adds customer by directly modifying the contents of the data structure containing customer data. </li></ul><ul><li>Improvement: </li></ul><ul><li>When customer not found, component calls the AddCustomer() method that is responsible for maintaining customer data. </li></ul>
  8. 8. Common Coupling <ul><li>Definition: Two components share data </li></ul><ul><ul><li>Global data structures </li></ul></ul><ul><ul><li>Common blocks </li></ul></ul><ul><li>Usually a poor design choice because </li></ul><ul><ul><li>Lack of clear responsibility for the data </li></ul></ul><ul><ul><li>Reduces readability </li></ul></ul><ul><ul><li>Difficult to determine all the components that affect a data element (reduces maintainability) </li></ul></ul><ul><ul><li>Difficult to reuse components </li></ul></ul><ul><ul><li>Reduces ability to control data accesses </li></ul></ul>
  9. 9. Example-1 <ul><li>Each source process writes directly to global data store. Each sink process reads directly from global data store. </li></ul>Process control component maintains current data about state of operation. Gets data from multiple sources. Supplies data to multiple sinks.
  10. 10. Example-2 <ul><li>Each source process writes directly to global data store. Each sink process reads directly from global data store. </li></ul><ul><li>Improvement </li></ul><ul><li>Data manager component is responsible for data in data store. Processes send data to and request data from data manager. </li></ul>Process control component maintains current data about state of operation. Gets data from multiple sources. Supplies data to multiple sinks.
  11. 11. Control Coupling <ul><li>Definition: Component passes control parameters to coupled components. </li></ul><ul><li>May be either good or bad, depending on situation. </li></ul><ul><ul><li>Bad when component must be aware of internal structure and logic of another module </li></ul></ul><ul><ul><li>Good if parameters allow factoring and reuse of functionality </li></ul></ul>
  12. 12. Example <ul><li>Acceptable : Module p calls module q and q passes back flag that says it cannot complete the task, then q is passing data </li></ul><ul><li>Not Acceptable: Module p calls module q and q passes back flag that says it cannot complete the task and, as a result, writes a specific message. </li></ul>
  13. 13. Stamp Coupling <ul><li>Definition: Component passes a data structure to another component that does not have access to the entire structure. </li></ul><ul><li>Requires second component to know how to manipulate the data structure (e.g., needs to know about implementation) </li></ul><ul><li>May be necessary due to efficiency factors: this is a choice made by insightful designer, not lazy programmer. </li></ul>
  14. 14. Example-1 <ul><li>The print routine of the customer billing accepts a customer data structure as an argument, parses it, and prints the name, address, and billing information. </li></ul>Customer billing system
  15. 15. Example-2 <ul><li>The print routine of the customer billing accepts a customer data structure as an argument, parses it, and prints the name, address, and billing information. </li></ul><ul><li>Improvement </li></ul><ul><li>The print routine takes the customer name, address, and billing information as an argument. </li></ul>Customer Billing System
  16. 16. Data Coupling <ul><li>Definition: Two components are data coupled if there are homogeneous data items. </li></ul><ul><li>Every argument is simple argument or data structure in which all elements are used </li></ul><ul><li>Good, if it can be achieved. </li></ul><ul><li>Easy to write contracts for this and modify component independently. </li></ul>
  17. 17. Key Idea in Object-Oriented Programming <ul><li>Object-oriented designs tend to have low coupling. </li></ul>
  18. 18. Cohesion <ul><li>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. </li></ul><ul><li>Internal glue with which component is constructed </li></ul><ul><li>All elements of component are directed toward and essential for performing the same task </li></ul><ul><li>High is good </li></ul>
  19. 19. Range of Cohesion High Cohesion Low Functional Informational Sequential Communicational Procedural Temporal Logical Coincidental
  20. 20. Coincidental Cohesion <ul><li>Definition: Parts of the component are only related by their location in source code </li></ul><ul><li>Elements needed to achieve some functionality are scattered throughout the system. </li></ul><ul><li>Accidental </li></ul><ul><li>Worst form </li></ul>
  21. 21. Example <ul><li>Print next line </li></ul><ul><li>Reverse string of characters in second argument </li></ul><ul><li>Add 7 to 5 th argument </li></ul><ul><li>Convert 4 th argument to float </li></ul>
  22. 22. Logical Cohesion <ul><li>Definition: Elements of component are related logically and not functionally. </li></ul><ul><li>Several logically related elements are in the same component and one of the elements is selected by the client component. </li></ul>
  23. 23. Example-1 <ul><li>A component reads inputs from tape, disk, and network. All the code for these functions are in the same component. </li></ul><ul><li>Operations are related, but the functions are significantly different. </li></ul>
  24. 24. Example-2 <ul><li>A component reads inputs from tape, disk, and network. All the code for these functions are in the same component. Operations are related, but the functions are significantly different. </li></ul><ul><li>Improvement </li></ul><ul><li>A device component has a read operation that is overridden by sub-class components. The tape sub-class reads from tape. The disk sub-class reads from disk. The network sub-class reads from the network. </li></ul>
  25. 25. Temporal Cohesion <ul><li>Definition: Elements of a component are related by timing. </li></ul><ul><li>Difficult to change because you may have to look at numerous components when a change in a data structure is made. </li></ul><ul><li>Increases chances of regression fault </li></ul><ul><li>Component unlikely to be reusable. </li></ul>
  26. 26. Example-1 <ul><li>A system initialization routine: this routine contains all of the code for initializing all of the parts of the system. Lots of different activities occur, all at init time. </li></ul>
  27. 27. Example-2 <ul><li>A system initialization routine: this routine contains all of the code for initializing all of the parts of the system. Lots of different activities occur, all at init time. </li></ul><ul><li>Improvement </li></ul><ul><li>A system initialization routine sends an initialization message to each component. </li></ul><ul><li>Each component initializes itself at component instantiation time. </li></ul>
  28. 28. Procedural Cohesion <ul><li>Definition: Elements of a component are related only to ensure a particular order of execution. </li></ul><ul><li>Actions are still weakly connected and unlikely to be reusable </li></ul>
  29. 29. Example <ul><li>... </li></ul><ul><li>Read part number from data base </li></ul><ul><li>update repair record on maintenance file. </li></ul><ul><li>... </li></ul><ul><li>May be useful to abstract the intent of this sequence. Make the data base and repair record components handle reading and updating. Make component that handles more abstract operation. </li></ul>
  30. 30. Communicational Cohesion <ul><li>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 </li></ul>
  31. 31. Example <ul><li>Update record in data base and send it to the printer. </li></ul><ul><li>database.Update (record). </li></ul><ul><li>record.Print(). </li></ul>
  32. 32. Sequential Cohesion <ul><li>The output of one component is the input to another. </li></ul><ul><li>Occurs naturally in functional programming languages </li></ul><ul><li>Good situation </li></ul>
  33. 33. Informational Cohesion <ul><li>Definition: Module performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data. </li></ul><ul><li>Different from logical cohesion </li></ul><ul><ul><li>Each piece of code has single entry and single exit </li></ul></ul><ul><ul><li>In logical cohesion, actions of module intertwined </li></ul></ul><ul><li>ADT and object-oriented paradigm promote </li></ul>
  34. 34. Functional Cohesion <ul><li>Definition: Every essential element to a single computation is contained in the component. </li></ul><ul><li>Every element in the component is essential to the computation. </li></ul><ul><li>Ideal situation. </li></ul>
  35. 35. Examples of Cohesion-1 Function A Function B Function D Function C Function E Coincidental Parts unrelated Function A Function A’ Function A’’ logic Logical Similar functions Time t 0 Time t 0 + X Time t 0 + 2X Temporal Related by time Function A Function B Function C Procedural Related by order of functions
  36. 36. Examples of Cohesion-2 Function A part 1 Function A part 2 Function A part 3 Functional Sequential with complete, related functions Function A Function B Function C Communicational Access same data Function A Function B Function C Sequential Output of one is input to another
  37. 37. p q t r s u 1 2 3 4 5 6 No. In Out Interface Description p, t, u access the same database in update mode Problem: Define coupling between pairs of modules. 1 Aircraft type Status flag 2 ----- List of aircraft parts 3 Function code ----- 4 ----- List of aircraft parts 5 Part number Part manufacturer 6 Part number Part name
  38. 38. Coupling between pairs of modules q r s t u p q --- --- r --- --- s --- t
  39. 39. Problem: Classify cohesion for each module <ul><li>Compute average daily temperatures at various sites </li></ul><ul><li>Initialize sums and open files </li></ul><ul><li>Create new temperature record </li></ul><ul><li>Store temperature record </li></ul><ul><li>Close files and print average temperatures </li></ul><ul><li>Read in site, time, and temperature </li></ul><ul><li>Store record for specific site </li></ul><ul><li>Edit site, time, or temperature field </li></ul>
  40. 40. In Class <ul><li>P1: What is the effect of cohesion on maintenance? </li></ul><ul><li>P2: What is the effect of coupling on maintenance? </li></ul><ul><li>P3: Produce an example of each type of cohesion. Justify your answers. </li></ul><ul><li>P4: Produce an example of each type of coupling. Justify your answers. </li></ul>

×