Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Coupling and Cohesion
Lecture Slides By Adil Aslam
My Email Address
adilaslam5959@gmail.com
Software Design Basics
• Software design is a process to transform user requirements
into some suitable form, which helps ...
Software Design Levels
•Software Design Levels
•Software design yields three levels of results:
•Architectural Design: The...
Example of Architectural Design
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Software Design Levels
•Software Design Levels
•Software design yields three levels of results:
•High-level Design: The hi...
Software Design Levels
•Software Design Levels
•Software design yields three levels of results:
•Detailed Design: Detailed...
Modularization
•Modularization is a technique to divide a software
system into multiple discrete and independent
modules, ...
Advantage of Modularization
•Smaller components are easier to maintain
•Program can be divided based on functional aspects...
Concurrency
•Back in time, all software are meant to be executed
sequentially. By sequential execution we mean that
the co...
Concurrency
•It is necessary for the programmers and designers
to recognize those modules, which can be made
parallel exec...
Coupling and Cohesion
•When a software program is modularized, its tasks
are divided into several modules based on some
ch...
Characteristics of Good Design
•Component
independence
•High cohesion
•Low coupling
•Exception identification
and handling...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
No dependencies Loosely coupled-some dependencies
Highly co...
Coupling
•“The degree of interdependence between two
modules”
•Coupling is a measure that defines the level of inter-
depe...
Coupling
•Loose coupling makes it possible to:
•Understand one class without reading others
•Change one class without affe...
Coupling
 Coupling is characterized by passage of control
between modules
 “Control flag” (a variable that controls deci...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
public class Greeting {
public void WelcomeMessage(string t...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Coupling Cont.
public class Home
{
public Home()
{
Greeting...
Low Coupling
• Coupling in its very simple form
is the degree to which one class
is connected to another class. If
class i...
Low Coupling
•Low coupling can be achieved by having less classes
linking to one another. The best way to reduce
coupling ...
Low Coupling
•A class with high coupling is undesirable
because:
•Changes in related classes force local
changes.
•This cl...
The GRASP Pattern : Low Coupling
•Assign a responsibility so that the coupling remains
low
•Recall coupling is how strongl...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Payment POST Sale
Consider the following partial conceptual...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
makePayment()
1: create()
:POST
:Sale
p:Payment
2: addPayme...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
makePayment()
1: makePayment()
:POST :Sale
p:Payment
Anothe...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
•Which Design Supports Low Coupling?
DESIGN A
Register Paym...
Low Coupling
•Based only on the “coupling” factor, we prefer
Design B as it maintains overall lower coupling.
•We need to ...
Benefits of Low Coupling
•Low Coupling has the following benefits.
Lowly coupled classes are:
•less affected by changes in...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
29
High Coupling
Loose
Low
Content
Common
External
Control
...
Content Coupling
• Content coupling - When a module can directly access or
modify or refer to the content of another modul...
Example of Content Coupling
Part of program handles lookup for customer.
When customer not found, component adds
customer ...
Example of Content Coupling
Part of program handles lookup for customer.
When customer not found, component adds
customer ...
Example of Content Coupling
•Occurs when one component modifies an internal
data item in another component, or when one
co...
Common Coupling
• Common coupling- When multiple modules have read and
write access to some global data, it is called comm...
Example of Common Coupling
Process control component maintains current data
about state of operation. Gets data from multi...
Example of Common Coupling
Process control component maintains current data
about state of operation. Gets data from multi...
Example of Common Coupling
while (global variable == 0)
if (argument xyz > 25)
module 3 ();
else
module 4 ();
Notes By Adi...
Example of Common Coupling
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Control Coupling
• Control coupling- Two modules are called control-
coupled if one of them decides the function of the
ot...
Example of Control Coupling
•Occurs when one procedure calls another using a
‘flag’ or ‘command’ that explicitly controls ...
Example of Control Coupling
•Acceptable: Module p calls module q and q passes
back flag that says it cannot complete the t...
Stamp Coupling
•Stamp coupling- When multiple modules share common
data structure and work on different part of it, it is ...
Example of Stamp Coupling
Customer Billing System
The print routine of the customer billing accepts
customer data structur...
Example of Stamp Coupling
Customer Billing System
The print routine of the customer billing accepts
customer data structur...
Stamp Coupling
•Data structures are passed as parameters
•Two ways to reduce stamp coupling
• using an interface as the ar...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
public class Emailer
{
public void sendEmail(Employee e, St...
Data Coupling
•Data coupling- Data coupling is when two modules
interact with each other by means of passing data (as
para...
Example of Data Coupling
class Receiver
{
public void message( MyType X )
{
// code goes here
X.doSomethingForMe( Object d...
Cohesion
•Cohesion is a measure that defines the degree of
intra-dependability within elements of a module.
The greater th...
Cohesion
 Cohesion is a natural extension of the information
• hiding concept
 A cohesive module performs a single task ...
Cohesion
•High cohesion makes it easier to:
•Understand what a class or method does
•Use descriptive names
•Reuse classes ...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
public class Activities
{
public void PrintDocument(Documen...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Cohesion
class EmailMessage {
private
private
private
priva...
Types of Cohesion
1. Coincidental cohesion
2. Logical cohesion
3. Temporal cohesion
4. Procedural cohesion
5. Communicatio...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
High Cohesion
Low
Functional
Informational
Sequential
Commu...
Coincidental Cohesion
 A module has coincidental cohesion if it performs
• multiple, completely unrelated actions
 print...
Logical Cohesion
 A module has logical cohesion when it
performs a series of related actions, one of
which is selected by...
Example of Logical Cohesion
•A component reads inputs from tape, disk, and
network. All the code for these functions are i...
Temporal Cohesion
 A module has temporal cohesion when it
•performs a series of actions related in time
 Example
 open ...
Example of Temporal Cohesion
•A system initialization routine: this routine contains
all of the code for initializing all ...
Procedural Cohesion
 A module has procedural cohesion if it
performs a series of actions related by the
procedure to be f...
Example of Procedural Cohesion
...
Read part number from
data base
update repair record on
maintenance file.
...
Notes By ...
Communicational Cohesion
 A module has communicational cohesion if it
performs a series of actions related by the
procedu...
Example of Communicational Cohesion
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
•Update record in data
...
Functional Cohesion
 It is considered to be the highest degree of
cohesion, and it is highly expected. Elements of
module...
Informational Cohesion
 A module has informational cohesion if it performs
a number of actions, each with its own entry
p...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Examples of Cohesion-1
Function A
Function
B
Function
D
Fun...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Examples of Cohesion-2
Cohesion vs Coupling
Cohesion Coupling
Cohesion is the indication of
the relationship
within module.
Coupling is the indic...
Cohesion vs Coupling
Cohesion Coupling
While designing you should
strive for high cohesion i.e. a
cohesive component/ modu...
Notes By Adil Aslam
My Email Address : adilaslam5959@gmail.com
Determining Module Cohesion
Doing one
function
only?
Activi...
Design Verification
•The output of software design process is design
documentation, pseudo codes, detailed logic
diagrams,...
Design Verification
•It is then becomes necessary to verify the output
before proceeding to the next phase. The early any
...
Thank You 
Prochain SlideShare
Chargement dans…5
×

Coupling and Cohesion in Software Engineering

Software Design Basics
Modularization
Advantage of Modularization
Concurrency
Characteristics of Good Design
Coupling in Software Engineering
Example of Coupling in Software Engineering
Type of Coupling in Software Engineering
Low Coupling Example
Benefits of Low Coupling
Cohesion in Software Engineering
Examples of Cohesion in Software Engineering
Type of Cohesion in Software Engineering
Cohesion vs Coupling
Design Verification in Software Engineering

  • Soyez le premier à commenter

Coupling and Cohesion in Software Engineering

  1. 1. Coupling and Cohesion Lecture Slides By Adil Aslam My Email Address adilaslam5959@gmail.com
  2. 2. Software Design Basics • Software design is a process to transform user requirements into some suitable form, which helps the programmer in software coding and implementation. • For assessing user requirements, an SRS (Software Requirement Specification) document is created whereas for coding and implementation, there is a need of more specific and detailed requirements in software terms. The output of this process can directly be used into implementation in programming languages. • Software design is the first step in SDLC (Software Design Life Cycle), which moves the concentration from problem domain to solution domain. It tries to specify how to fulfill the requirements mentioned in SRS. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  3. 3. Software Design Levels •Software Design Levels •Software design yields three levels of results: •Architectural Design: The architectural design is the highest abstract version of the system. It identifies the software as a system with many components interacting with each other. At this level, the designers get the idea of proposed solution domain. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  4. 4. Example of Architectural Design Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  5. 5. Software Design Levels •Software Design Levels •Software design yields three levels of results: •High-level Design: The high-level design breaks the ‘single entity-multiple component’ concept of architectural design into less-abstracted view of sub-systems and modules and depicts their interaction with each other. High-level design focuses on how the system along with all of its components can be implemented in forms of modules. It recognizes modular structure of each sub-system and their relation and interaction among each other. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  6. 6. Software Design Levels •Software Design Levels •Software design yields three levels of results: •Detailed Design: Detailed design deals with the implementation part of what is seen as a system and its sub-systems in the previous two designs. It is more detailed towards modules and their implementations. It defines logical structure of each module and their interfaces to communicate with other modules. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  7. 7. Modularization •Modularization is a technique to divide a software system into multiple discrete and independent modules, which are expected to be capable of carrying out task(s) independently. These modules may work as basic constructs for the entire software. Designers tend to design modules such that they can be executed and/or compiled separately and independently. •Modular design unintentionally follows the rules of ‘divide and conquer’ problem-solving strategy this is because there are many other benefits attached with the modular design of a software. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  8. 8. Advantage of Modularization •Smaller components are easier to maintain •Program can be divided based on functional aspects •Desired level of abstraction can be brought in the program •Components with high cohesion can be re-used again •Concurrent execution can be made possible •Desired from security aspect Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  9. 9. Concurrency •Back in time, all software are meant to be executed sequentially. By sequential execution we mean that the coded instruction will be executed one after another implying only one portion of program being activated at any given time. Say, a software has multiple modules, then only one of all the modules can be found active at any time of execution. •In software design, concurrency is implemented by splitting the software into multiple independent units of execution, like modules and executing them in parallel. In other words, concurrency provides capability to the software to execute more than one part of code in parallel to each other. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  10. 10. Concurrency •It is necessary for the programmers and designers to recognize those modules, which can be made parallel execution. •Example •The spell check feature in word processor is a module of software, which runs along side the word processor itself. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  11. 11. Coupling and Cohesion •When a software program is modularized, its tasks are divided into several modules based on some characteristics. As we know, modules are set of instructions put together in order to achieve some tasks. They are though, considered as single entity but may refer to each other to work together. There are measures by which the quality of a design of modules and their interaction among them can be measured. These measures are called coupling and cohesion. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  12. 12. Characteristics of Good Design •Component independence •High cohesion •Low coupling •Exception identification and handling •Fault prevention and fault tolerance •Design for change Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  13. 13. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com 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. Coupling: Degree of Dependence Among Components
  14. 14. Coupling •“The degree of interdependence between two modules” •Coupling is a measure that defines the level of inter- dependability among modules of a program. It tells at what level the modules interfere and interact with each other. The lower the coupling, the better the program. •Coupling is the measure of the interdependence of one module to another. Modules should have low coupling. Low coupling minimizes the "ripple effect" where changes in one module cause errors in other modules. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  15. 15. Coupling •Loose coupling makes it possible to: •Understand one class without reading others •Change one class without affecting others •Thus: improves maintainability Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  16. 16. Coupling  Coupling is characterized by passage of control between modules  “Control flag” (a variable that controls decisions in a subordinate or superordinate module) is passed between modules  Relatively high levels of coupling occur when modules are communicate with external to software  External coupling is essential, but should be limited to a small number of modules with a structure Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  17. 17. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com public class Greeting { public void WelcomeMessage(string type) { switch (type) { case "GM": Morning"); Evening"); Night"); Console.WriteLine("Good break; case "GE": Console.WriteLine("Good break; case "GN": Console.WriteLine("Good break; } } } Coupling
  18. 18. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com Coupling Cont. public class Home { public Home() { Greeting example = new Greeting(); example.WelcomeMessage("GE"); } }
  19. 19. Low Coupling • Coupling in its very simple form is the degree to which one class is connected to another class. If class is highly coupled, then you are bound to use /edit rest of the connected classes where only editing one class could have served the purpose. • Example of High coupling is shown in the below image where one or more classes are dependent on quite a few other classes. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  20. 20. Low Coupling •Low coupling can be achieved by having less classes linking to one another. The best way to reduce coupling is by providing an API (interface). •OOP languages like C#, Java, C++ offer quite a few mechanisms to offer low coupling like public classes, public methods, interfaces and other interaction points between different classes and modules. •One last bit on low coupling / zero coupling is: Zero coupled classes are not very usable and very low coupled applications are very difficult to maintain. So striking a balance is definitely an art. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  21. 21. Low Coupling •A class with high coupling is undesirable because: •Changes in related classes force local changes. •This class is harder to understand in isolation. •This class is harder to reuse because its use requires the inclusion of all classes it is dependent upon. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  22. 22. The GRASP Pattern : Low Coupling •Assign a responsibility so that the coupling remains low •Recall coupling is how strongly one class has knowledge of another class •The best example of this is not to have POST talking to every class •POST should not have knowledge of any more classes then its has to Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  23. 23. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com Payment POST Sale Consider the following partial conceptual model: What class should be responsible for creating an instance of Payment ? Low Coupling: Example (1)
  24. 24. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com makePayment() 1: create() :POST :Sale p:Payment 2: addPayment(p) One solution is to let POST create an instance of Payment and pass a reference this Payment to Sale. This solution is suggested as POST records a Payment. Low Coupling: Example (1)
  25. 25. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com makePayment() 1: makePayment() :POST :Sale p:Payment Another solution is to let Sale create an instance of Payment. 1.1: create() Which of the two solutions is preferable from the point of view of coupling? Low Coupling: Example (1)
  26. 26. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com •Which Design Supports Low Coupling? DESIGN A Register Payment Sale DESIGN B Register Payment Sale
  27. 27. Low Coupling •Based only on the “coupling” factor, we prefer Design B as it maintains overall lower coupling. •We need to remember that when evaluating different designs, we should not just consider one factor (e.g. degree of coupling) in isolation. We need to consider other principles as well such as Information Expert and High Cohesion. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  28. 28. Benefits of Low Coupling •Low Coupling has the following benefits. Lowly coupled classes are: •less affected by changes in other components •simple to understand in isolation •convenient to reuse Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  29. 29. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com 29 High Coupling Loose Low Content Common External Control Stamp Data Uncoupled Avoid Try to achieve Type of Coupling
  30. 30. Content Coupling • Content coupling - When a module can directly access or modify or refer to the content of another module, it is called content level coupling. • One module directly references contents of the other •Example • module a modifies statements of module b • module a refers to local data of module b in terms of some numerical displacement within b • module a branches into local label of module b •Why is this bad? • almost any change to b requires changes to a Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  31. 31. Example of Content Coupling Part of program handles lookup for customer. When customer not found, component adds customer by directly modifying the contents of the data structure containing customer data. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  32. 32. Example of Content Coupling Part of program handles lookup for customer. When customer not found, component adds customer by directly modifying the contents of the data structure containing customer data. Improvement: When customer not found, component calls the AddCustomer() method that is responsible for maintaining customer data. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  33. 33. Example of Content Coupling •Occurs when one component modifies an internal data item in another component, or when one component branches into the middle of another component Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com Module B _________________ _________________ _________________ _________________ Generate D Call D _________________ _________________ _________________ _________________ _________________ _________________ _________________ _________________ Module D _________________ _________________ _________________ _________________ _________________ _________________ _________________ _________________ _________________ _________________
  34. 34. Common Coupling • Common coupling- When multiple modules have read and write access to some global data, it is called common or global coupling. • Example • two modules have access to same database, and can both read and write same record • Modules a and b can access and change the value of global variable • use of Java public statement • Why is this bad? • resulting code is unreadable • modules can have side-effects • must read entire module to understand • difficult to reuse • module exposed to more data than necessary Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  35. 35. Example of Common Coupling Process control component maintains current data about state of operation. 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. Improvement? Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  36. 36. Example of Common Coupling Process control component maintains current data about state of operation. 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. Improvement? Data manager component is responsible for data in data store. Processes send data to and request data from data manager. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  37. 37. Example of Common Coupling while (global variable == 0) if (argument xyz > 25) module 3 (); else module 4 (); Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com cca ccb global variable
  38. 38. Example of Common Coupling Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  39. 39. Control Coupling • Control coupling- Two modules are called control- coupled if one of them decides the function of the other module or changes its flow of execution. • one module passes an element of control to the other  Example -If module p calls module q and q passes back ‒ “I am unable to complete my task”, then q is passing data ‒ “I am unable to complete my task; write error message • ABC123”, then p and q are control coupled  Why is control coupling bad? ‒ Modules are not independent; module q (the called module) must know internal structure and logic of module p. This affects reusability ‒ Usually is associated with modules of logical cohesion Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  40. 40. Example of Control Coupling •Occurs when one procedure calls another using a ‘flag’ or ‘command’ that explicitly controls what the second procedure does Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com public routineX(String command) { if (command.equals("drawCircle") { drawCircle(); } else { drawRectangle(); } }
  41. 41. Example of Control Coupling •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, as a result, writes a specific message. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  42. 42. Stamp Coupling •Stamp coupling- When multiple modules share common data structure and work on different part of it, it is called stamp coupling. • data structure is passed as parameter, but called module operates on only some of individual components • Example calculate withholding (employee record) • Why is this bad? • affects understanding • not clear, without reading entire module, which fields of record are accessed or changed • unlikely to be reusable • other products have to use the same higher level data structures • passes more data than necessary • e.g., uncontrolled data access can lead to computer crime Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  43. 43. Example of Stamp Coupling Customer Billing System The print routine of the customer billing accepts customer data structure as an argument, parses it, and prints the name, address, and billing information. Improvement? Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  44. 44. Example of Stamp Coupling Customer Billing System The print routine of the customer billing accepts customer data structure as an argument, parses it, and prints the name, address, and billing information. Improvement? The print routine takes the customer name, address, and billing information as an argument. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  45. 45. Stamp Coupling •Data structures are passed as parameters •Two ways to reduce stamp coupling • using an interface as the argument type • passing simple variables Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  46. 46. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com public class Emailer { public void sendEmail(Employee e, String text) {...} ... } public class Emailer { public void sendEmail(String name, String email, String text) {...} ... } Using simple data types to avoid it: Example of Stamp Coupling
  47. 47. Data Coupling •Data coupling- Data coupling is when two modules interact with each other by means of passing data (as parameter). If a module passes data structure as parameter, then the receiving module should use all its components. • Every argument is either a simple argument or a data structure in which all elements are used by the called module • Example display time of arrival (flight number) get job with highest priority (job queue) • Slippery slope between stamp & data (see queue) • Why is this good? • maintenance is easier • good design has high cohesion & weak coupling Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  48. 48. Example of Data Coupling class Receiver { public void message( MyType X ) { // code goes here X.doSomethingForMe( Object data ); // more code } } Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  49. 49. Cohesion •Cohesion is a measure that defines the degree of intra-dependability within elements of a module. The greater the cohesion, the better is the program design. •“The measure of the strength of functional relatedness of elements within a module”. •Cohesion refers to the dependence within and among a module’s internal elements (e.g., data, functions, internal modules) Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  50. 50. Cohesion  Cohesion is a natural extension of the information • hiding concept  A cohesive module performs a single task within a software procedure, requiring little interaction with procedures being performed in other parts of a program  Simply state, a cohesive module should (ideally) do just one thing  We always strive for high cohesion, although the • mid-range of the spectrum is often acceptable Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  51. 51. Cohesion •High cohesion makes it easier to: •Understand what a class or method does •Use descriptive names •Reuse classes or methods Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  52. 52. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com public class Activities { public void PrintDocument(Document doc) { ….… } public void SendEmail(string rcpnt,string sbj,string txt){ ….… } public void CalculateDist(int x1, int y1, int x2, int y2){ ….… } } Cohesion
  53. 53. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com Cohesion class EmailMessage { private private private private string string string string sendTo; subject; message; username; public subj,string msg){EmailMessage(string to,string this.sendTo = to; this.subject = subject; this.message = message; } public void SendMessage() { // send message using sendTo, subject and message } public void Login(string username, string password) { // code to login } }
  54. 54. Types of Cohesion 1. Coincidental cohesion 2. Logical cohesion 3. Temporal cohesion 4. Procedural cohesion 5. Communicational cohesion 6. Functional cohesion 7. Informational cohesion Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  55. 55. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com High Cohesion Low Functional Informational Sequential Communicational Procedural Temporal Logical Coincidental Range of Cohesion
  56. 56. Coincidental Cohesion  A module has coincidental cohesion if it performs • multiple, completely unrelated actions  print next line, reverse string of characters comprising second parameter, add 7 to fifth parameter, convert fourth parameter to floating point  Arise from rules like “Every module will consist of • between 35 and 50 statements”  Why is coincidental cohesion so bad?  Degrades maintainability  Modules are not reusable  How to fix  Break into separate modules each performing one task Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  57. 57. Logical Cohesion  A module has logical cohesion when it performs a series of related actions, one of which is selected by the calling module  Example ‒ Module performing all input and output  Why is logical cohesion so bad? ‒ The interface is difficult to understand ‒ Code for more than one action may be intertwined ‒ Difficult to reuse Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  58. 58. Example of Logical Cohesion •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. Improvement •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. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  59. 59. Temporal Cohesion  A module has temporal cohesion when it •performs a series of actions related in time  Example  open old master file, new master file, transaction file, print file, initialize sales district table, read first transaction record, read first old master record  Why is temporal cohesion so bad?  Actions of this module are weakly related to one another, but strongly related to actions in other modules  Not reusable Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  60. 60. Example of Temporal Cohesion •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 in it time. Improvement •A system initialization routine sends an initialization message to each component. •Each component initializes itself at component instantiation time. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  61. 61. Procedural Cohesion  A module has procedural cohesion if it performs a series of actions related by the procedure to be followed by the product  Example ‒ read part number and update repair record on • master file  Why is procedural cohesion bad? ‒ Actions are still weakly connected ‒ Module is unlikely to be reusable Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  62. 62. Example of Procedural Cohesion ... Read part number from data base update repair record on maintenance file. ... Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com 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.
  63. 63. Communicational Cohesion  A module has communicational cohesion if it performs a series of actions related by the procedure to be followed by the product, but in addition all the actions operate on the same data  Examples ‒ update record in database and write it to audit trail ‒ calculate new coordinates and send them to terminal  Why is communicational cohesion bad? ‒ Still lack of reusability Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  64. 64. Example of Communicational Cohesion Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com •Update record in data base and send it to the printer. •database.Update (record). •record.Print().
  65. 65. Functional Cohesion  It is considered to be the highest degree of cohesion, and it is highly expected. Elements of module in functional cohesion are grouped because they all contribute to a single well-defined function. It can also be reused.  Module with functional cohesion performs exactly one action  Examples ‒ calculate sales commission  Why is functional cohesion good? ‒ More reusable ‒ Corrective maintenance easier ‒ Easier to extend product Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  66. 66. Informational Cohesion  A module has informational cohesion if it performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data structure  Major difference between logical and information cohesion ‒ Actions in the module with logical cohesion are intertwined ‒ Actions in the module with information cohesion are completely independent  Why is informational cohesion good? ‒ All the advantages of using abstract data type are gained Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  67. 67. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com 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 t0 Time t0 + X Time t0 + 2X Temporal Related by time Function A Function B Function C Procedural Related by order of functions
  68. 68. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com Examples of Cohesion-2
  69. 69. Cohesion vs Coupling Cohesion Coupling Cohesion is the indication of the relationship within module. Coupling is the indication of the relationships between modules. Cohesion shows the module’s relative functional strength. Coupling shows the relative independence among the modules. Cohesion is a degree (quality) to which a component / module focuses on the single thing Coupling is a degree to which a component / module is connected to the other modules. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  70. 70. Cohesion vs Coupling Cohesion Coupling While designing you should strive for high cohesion i.e. a cohesive component/ module focus on a single task (i.e., single-mindedness) with little interaction with other modules of the system. While designing you should strive for low coupling i.e. Dependency between modules should be less. Cohesion is Intra – Module Concept. Coupling is Inter - Module Concept. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  71. 71. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com Determining Module Cohesion Doing one function only? Activities same category? Sequence important? Module related by? Sequence important? Functional Sequential Communicational Procedural Temporal Logical Coincidental Yes No Data None Control Flow Yes Yes Yes No No No
  72. 72. Design Verification •The output of software design process is design documentation, pseudo codes, detailed logic diagrams, process diagrams, and detailed description of all functional or non-functional requirements. •The next phase, which is the implementation of software, depends on all outputs mentioned above. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  73. 73. Design Verification •It is then becomes necessary to verify the output before proceeding to the next phase. The early any mistake is detected, the better it is or it might not be detected until testing of the product. If the outputs of design phase are in formal notation form, then their associated tools for verification should be used otherwise a thorough design review can be used for verification and validation. •By structured verification approach, reviewers can detect defects that might be caused by overlooking some conditions. A good design review is important for good software design, accuracy and quality. Notes By Adil Aslam My Email Address : adilaslam5959@gmail.com
  74. 74. Thank You 

×