<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Snviswan2</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Snviswan2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Snviswan2"/>
	<updated>2026-05-14T05:00:34Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9607</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9607"/>
		<updated>2007-11-20T00:59:53Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;br /&gt;
&lt;br /&gt;
== What Is ==&lt;br /&gt;
&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
This is usually also called as Model-View-Controller Pattern.  The essence behind this is that when the business logic or data access components are large, a Controller class is introduced between user interface (''View'') and the core logic (''Model''). [[http://cristobal.baray.com/indiana/projects/mvc.html]]&lt;br /&gt;
&lt;br /&gt;
A very good example for this is the Java Swing Architecture.  In Swing, all GUI is defined via objects and classes derived from the JComponent object.  The business logic is defined in ActionListeners, Event Descriptors.&lt;br /&gt;
&lt;br /&gt;
To put things simply, &lt;br /&gt;
&lt;br /&gt;
An easy way to understand MVC: the model is the data, the view is the window on the screen, and the controller is the glue between the two. -- ''ConnellyBarnes''&lt;br /&gt;
&lt;br /&gt;
== SmallTalk ==&lt;br /&gt;
&lt;br /&gt;
However intuitive and simple idea it is, it has an origin in this work and its inventors (TrygveReenskaug and others).  Lets discuss how it actually works.  This is not a chat application as is usually the perception, but is actually a language that is inspired by the desire to make human computer interaction simpler and smoother.  The language has the concepts of object oriented Programming and each object has only following properties,&lt;br /&gt;
&lt;br /&gt;
   1. Hold state (references to other objects).&lt;br /&gt;
   2. Receive a message from itself or another object.&lt;br /&gt;
   3. In the course of processing a message, send messages to itself or another object.&lt;br /&gt;
&lt;br /&gt;
The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate.&lt;br /&gt;
&lt;br /&gt;
The working clearly indicates the semblances of the MVC pattern.&lt;br /&gt;
[[http://en.wikipedia.org/wiki/Smalltalk]]&lt;br /&gt;
&lt;br /&gt;
== An Example ==&lt;br /&gt;
&lt;br /&gt;
=== Http Request === &lt;br /&gt;
&lt;br /&gt;
  Browser           View              Controller      Model&lt;br /&gt;
    .                 .                 .               .&lt;br /&gt;
    . HTTP Request    .                 .               .&lt;br /&gt;
    +----------------------------------&amp;gt;+               .&lt;br /&gt;
    |                 .                 | update model  .&lt;br /&gt;
    |                 .                 +--------------&amp;gt;+&lt;br /&gt;
    |                 .                 |               |&lt;br /&gt;
    |                 .                 | return status |&lt;br /&gt;
    |                 .                 +&amp;lt;--------------+&lt;br /&gt;
    |                 .     select view |               .&lt;br /&gt;
    |                 +&amp;lt;----------------+               .&lt;br /&gt;
    |                 |                 .               .&lt;br /&gt;
    |                 | query state     .               .&lt;br /&gt;
    |                 +--------------------------------&amp;gt;+&lt;br /&gt;
    |                 |                 .               |&lt;br /&gt;
    |                 |                 .  return state |&lt;br /&gt;
    |                 +&amp;lt;--------------------------------+&lt;br /&gt;
    |   HTTP Response |                 .               .&lt;br /&gt;
    +&amp;lt;----------------+                 .               .&lt;br /&gt;
    .                 .                 .               .&lt;br /&gt;
&lt;br /&gt;
[[http://www.perlmonks.org/?node_id=402070]]&lt;br /&gt;
&lt;br /&gt;
=== Working ===&lt;br /&gt;
&lt;br /&gt;
The working of a MVC model is simple, and is usually done via multiple steps.  Its an extremely verbose model in the sense that there are loot more classes than need be, for a simple application.&lt;br /&gt;
&lt;br /&gt;
The entry point of a request is Controller, and based on that it updates model, and then selects view.  The logic for both are given in the controller.  Once the view is selected, the view directly queries the model for necessary data, the response for the request is given to the user.&lt;br /&gt;
&lt;br /&gt;
There are a few points here that must be understood. &lt;br /&gt;
&lt;br /&gt;
* The request arrives at controller, and does not go through the view.&lt;br /&gt;
* Controller(s) merely select the view.&lt;br /&gt;
* Controller only updates model.&lt;br /&gt;
* Interactions between the View and model are not monitored by the controller.&lt;br /&gt;
* View only queries the model.&lt;br /&gt;
* Response is displayed or returned to the user view the view.&lt;br /&gt;
&lt;br /&gt;
=== This example ===&lt;br /&gt;
&lt;br /&gt;
It has a few important advantages&lt;br /&gt;
&lt;br /&gt;
* Views can be changed without touching the actual model implementation.&lt;br /&gt;
* Requests are seperated from the model implementation.  Controller acts as the shield between data and the logic.&lt;br /&gt;
* Lastly, Requests are independent of the views. and response views can be changed as per logic.&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
The MVC model, has a lot of advantages.  All of them are due to the simplicity of design.&lt;br /&gt;
&lt;br /&gt;
* '''The greatest advantage''' is the clarity of design. When designing the application, this trait makes the entire program easier to implement and maintain.&lt;br /&gt;
* '''Efficient modularity''' of the design allows any of the components to be swapped in and out as the user or programmer desires - even the model! &lt;br /&gt;
* '''Multiple views''' Consider a PC game.  It would be extremely easy to allow the user to customize the in-game player.  Game environment, music, everything can be changed accordingly without touching the complicated game logic.&lt;br /&gt;
* '''Scalability''' Due to its simplicity and clarity of design, its very scalable and can be extended with multiple functionalities in the end.&lt;br /&gt;
* '''User Interface''' The user interface is also extendable, an example for that is macros and custom keys.  they can be implemented as a series of standard commands issued to controller.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
Its disadvantage arises only from the fact that its extremely verbose.&lt;br /&gt;
&lt;br /&gt;
* '''More Classes''' Can lead to lots more classes than expected.  Careful design and understanding is necessary to avoid both more classes as well as have lesser number of classes&lt;br /&gt;
* '''Design Trace''' It becomes hard to determine which functionality should be implemented where.  The base difficulty of coding is simple, but maintaining the design is sometimes confusing and tasking.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* http://java.sun.com/blueprints/patterns/MVC-detailed.html&lt;br /&gt;
* A better explanation: http://ootips.org/mvc-pattern.html&lt;br /&gt;
* http://c2.com/cgi/wiki?ModelViewController&lt;br /&gt;
* A Simple article: http://www.informit.com/guides/content.aspx?g=java&amp;amp;seqNum=279&lt;br /&gt;
* http://cs.anu.edu.au/~Alistair.Rendell/Teaching/mdtutorial/node12.html&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
* SmallTalk: http://www.smalltalk.org/main/&lt;br /&gt;
* Front Controller Pattern: http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html&lt;br /&gt;
* Again Java: http://java.sun.com/blueprints/patterns/FrontController.html&lt;br /&gt;
* Observer Pattern: http://en.wikipedia.org/wiki/Observer_pattern&lt;br /&gt;
* Controller pattern implementation in ASPX .NET: http://msdn2.microsoft.com/en-us/library/ms998532.aspx&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9605</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9605"/>
		<updated>2007-11-20T00:54:24Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;br /&gt;
&lt;br /&gt;
== What Is ==&lt;br /&gt;
&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
This is usually also called as Model-View-Controller Pattern.  The essence behind this is that when the business logic or data access components are large, a Controller class is introduced between user interface (''View'') and the core logic (''Model''). &lt;br /&gt;
&lt;br /&gt;
A very good example for this is the Java Swing Architecture.  In Swing, all GUI is defined via objects and classes derived from the JComponent object.  The business logic is defined in ActionListeners, Event Descriptors.&lt;br /&gt;
&lt;br /&gt;
To put things simply, &lt;br /&gt;
&lt;br /&gt;
An easy way to understand MVC: the model is the data, the view is the window on the screen, and the controller is the glue between the two. -- ''ConnellyBarnes''&lt;br /&gt;
&lt;br /&gt;
== SmallTalk ==&lt;br /&gt;
&lt;br /&gt;
However intuitive and simple idea it is, it has an origin in this work and its inventors (TrygveReenskaug and others).  Lets discuss how it actually works.  This is not a chat application as is usually the perception, but is actually a language that is inspired by the desire to make human computer interaction simpler and smoother.  The language has the concepts of object oriented Programming and each object has only following properties,&lt;br /&gt;
&lt;br /&gt;
   1. Hold state (references to other objects).&lt;br /&gt;
   2. Receive a message from itself or another object.&lt;br /&gt;
   3. In the course of processing a message, send messages to itself or another object.&lt;br /&gt;
&lt;br /&gt;
The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate.&lt;br /&gt;
&lt;br /&gt;
The working clearly indicates the semblances of the MVC pattern.&lt;br /&gt;
[[http://en.wikipedia.org/wiki/Smalltalk]]&lt;br /&gt;
&lt;br /&gt;
== An Example ==&lt;br /&gt;
&lt;br /&gt;
=== Http Request === &lt;br /&gt;
&lt;br /&gt;
  Browser           View              Controller      Model&lt;br /&gt;
    .                 .                 .               .&lt;br /&gt;
    . HTTP Request    .                 .               .&lt;br /&gt;
    +----------------------------------&amp;gt;+               .&lt;br /&gt;
    |                 .                 | update model  .&lt;br /&gt;
    |                 .                 +--------------&amp;gt;+&lt;br /&gt;
    |                 .                 |               |&lt;br /&gt;
    |                 .                 | return status |&lt;br /&gt;
    |                 .                 +&amp;lt;--------------+&lt;br /&gt;
    |                 .     select view |               .&lt;br /&gt;
    |                 +&amp;lt;----------------+               .&lt;br /&gt;
    |                 |                 .               .&lt;br /&gt;
    |                 | query state     .               .&lt;br /&gt;
    |                 +--------------------------------&amp;gt;+&lt;br /&gt;
    |                 |                 .               |&lt;br /&gt;
    |                 |                 .  return state |&lt;br /&gt;
    |                 +&amp;lt;--------------------------------+&lt;br /&gt;
    |   HTTP Response |                 .               .&lt;br /&gt;
    +&amp;lt;----------------+                 .               .&lt;br /&gt;
    .                 .                 .               .&lt;br /&gt;
&lt;br /&gt;
[[http://www.perlmonks.org/?node_id=402070]]&lt;br /&gt;
&lt;br /&gt;
=== Working ===&lt;br /&gt;
&lt;br /&gt;
The working of a MVC model is simple, and is usually done via multiple steps.  Its an extremely verbose model in the sense that there are loot more classes than need be, for a simple application.&lt;br /&gt;
&lt;br /&gt;
The entry point of a request is Controller, and based on that it updates model, and then selects view.  The logic for both are given in the controller.  Once the view is selected, the view directly queries the model for necessary data, the response for the request is given to the user.&lt;br /&gt;
&lt;br /&gt;
There are a few points here that must be understood. &lt;br /&gt;
&lt;br /&gt;
* The request arrives at controller, and does not go through the view.&lt;br /&gt;
* Controller(s) merely select the view.&lt;br /&gt;
* Controller only updates model.&lt;br /&gt;
* Interactions between the View and model are not monitored by the controller.&lt;br /&gt;
* View only queries the model.&lt;br /&gt;
* Response is displayed or returned to the user view the view.&lt;br /&gt;
&lt;br /&gt;
=== This example ===&lt;br /&gt;
&lt;br /&gt;
It has a few important advantages&lt;br /&gt;
&lt;br /&gt;
* Views can be changed without touching the actual model implementation.&lt;br /&gt;
* Requests are seperated from the model implementation.  Controller acts as the shield between data and the logic.&lt;br /&gt;
* Lastly, Requests are independent of the views. and response views can be changed as per logic.&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
The MVC model, has a lot of advantages.  All of them are due to the simplicity of design.&lt;br /&gt;
&lt;br /&gt;
* '''The greatest advantage''' is the clarity of design. When designing the application, this trait makes the entire program easier to implement and maintain.&lt;br /&gt;
* '''Efficient modularity''' of the design allows any of the components to be swapped in and out as the user or programmer desires - even the model! &lt;br /&gt;
* '''Multiple views''' Consider a PC game.  It would be extremely easy to allow the user to customize the in-game player.  Game environment, music, everything can be changed accordingly without touching the complicated game logic.&lt;br /&gt;
* '''Scalability''' Due to its simplicity and clarity of design, its very scalable and can be extended with multiple functionalities in the end.&lt;br /&gt;
* '''User Interface''' The user interface is also extendable, an example for that is macros and custom keys.  they can be implemented as a series of standard commands issued to controller.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
Its disadvantage arises only from the fact that its extremely verbose.&lt;br /&gt;
&lt;br /&gt;
* '''More Classes''' Can lead to lots more classes than expected.  Careful design and understanding is necessary to avoid both more classes as well as have lesser number of classes&lt;br /&gt;
* '''Design Trace''' It becomes hard to determine which functionality should be implemented where.  The base difficulty of coding is simple, but maintaining the design is sometimes confusing and tasking.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* http://java.sun.com/blueprints/patterns/MVC-detailed.html&lt;br /&gt;
* A better explanation: http://ootips.org/mvc-pattern.html&lt;br /&gt;
* &lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9604</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9604"/>
		<updated>2007-11-20T00:52:20Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;br /&gt;
&lt;br /&gt;
== What Is ==&lt;br /&gt;
&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
This is usually also called as Model-View-Controller Pattern.  The essence behind this is that when the business logic or data access components are large, a Controller class is introduced between user interface (''View'') and the core logic (''Model''). &lt;br /&gt;
&lt;br /&gt;
A very good example for this is the Java Swing Architecture.  In Swing, all GUI is defined via objects and classes derived from the JComponent object.  The business logic is defined in ActionListeners, Event Descriptors.&lt;br /&gt;
&lt;br /&gt;
To put things simply, &lt;br /&gt;
&lt;br /&gt;
An easy way to understand MVC: the model is the data, the view is the window on the screen, and the controller is the glue between the two. -- ''ConnellyBarnes''&lt;br /&gt;
&lt;br /&gt;
== SmallTalk ==&lt;br /&gt;
&lt;br /&gt;
However intuitive and simple idea it is, it has an origin in this work and its inventors (TrygveReenskaug and others).  Lets discuss how it actually works.  This is not a chat application as is usually the perception, but is actually a language that is inspired by the desire to make human computer interaction simpler and smoother.  The language has the concepts of object oriented Programming and each object has only following properties,&lt;br /&gt;
&lt;br /&gt;
   1. Hold state (references to other objects).&lt;br /&gt;
   2. Receive a message from itself or another object.&lt;br /&gt;
   3. In the course of processing a message, send messages to itself or another object.&lt;br /&gt;
&lt;br /&gt;
The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate.&lt;br /&gt;
&lt;br /&gt;
The working clearly indicates the semblances of the MVC pattern.&lt;br /&gt;
[[http://en.wikipedia.org/wiki/Smalltalk]]&lt;br /&gt;
&lt;br /&gt;
== An Example ==&lt;br /&gt;
&lt;br /&gt;
=== Http Request === &lt;br /&gt;
&lt;br /&gt;
  Browser           View              Controller      Model&lt;br /&gt;
    .                 .                 .               .&lt;br /&gt;
    . HTTP Request    .                 .               .&lt;br /&gt;
    +----------------------------------&amp;gt;+               .&lt;br /&gt;
    |                 .                 | update model  .&lt;br /&gt;
    |                 .                 +--------------&amp;gt;+&lt;br /&gt;
    |                 .                 |               |&lt;br /&gt;
    |                 .                 | return status |&lt;br /&gt;
    |                 .                 +&amp;lt;--------------+&lt;br /&gt;
    |                 .     select view |               .&lt;br /&gt;
    |                 +&amp;lt;----------------+               .&lt;br /&gt;
    |                 |                 .               .&lt;br /&gt;
    |                 | query state     .               .&lt;br /&gt;
    |                 +--------------------------------&amp;gt;+&lt;br /&gt;
    |                 |                 .               |&lt;br /&gt;
    |                 |                 .  return state |&lt;br /&gt;
    |                 +&amp;lt;--------------------------------+&lt;br /&gt;
    |   HTTP Response |                 .               .&lt;br /&gt;
    +&amp;lt;----------------+                 .               .&lt;br /&gt;
    .                 .                 .               .&lt;br /&gt;
&lt;br /&gt;
[[http://www.perlmonks.org/?node_id=402070]]&lt;br /&gt;
&lt;br /&gt;
=== Working ===&lt;br /&gt;
&lt;br /&gt;
The working of a MVC model is simple, and is usually done via multiple steps.  Its an extremely verbose model in the sense that there are loot more classes than need be, for a simple application.&lt;br /&gt;
&lt;br /&gt;
The entry point of a request is Controller, and based on that it updates model, and then selects view.  The logic for both are given in the controller.  Once the view is selected, the view directly queries the model for necessary data, the response for the request is given to the user.&lt;br /&gt;
&lt;br /&gt;
There are a few points here that must be understood. &lt;br /&gt;
&lt;br /&gt;
* The request arrives at controller, and does not go through the view.&lt;br /&gt;
* Controller(s) merely select the view.&lt;br /&gt;
* Controller only updates model.&lt;br /&gt;
* Interactions between the View and model are not monitored by the controller.&lt;br /&gt;
* View only queries the model.&lt;br /&gt;
* Response is displayed or returned to the user view the view.&lt;br /&gt;
&lt;br /&gt;
=== This example ===&lt;br /&gt;
&lt;br /&gt;
It has a few important advantages&lt;br /&gt;
&lt;br /&gt;
* Views can be changed without touching the actual model implementation.&lt;br /&gt;
* Requests are seperated from the model implementation.  Controller acts as the shield between data and the logic.&lt;br /&gt;
* Lastly, Requests are independent of the views. and response views can be changed as per logic.&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
The MVC model, has a lot of advantages.  All of them are due to the simplicity of design.&lt;br /&gt;
&lt;br /&gt;
* '''The greatest advantage''' is the clarity of design. When designing the application, this trait makes the entire program easier to implement and maintain.&lt;br /&gt;
* '''Efficient modularity''' of the design allows any of the components to be swapped in and out as the user or programmer desires - even the model! &lt;br /&gt;
* '''Multiple views''' Consider a PC game.  It would be extremely easy to allow the user to customize the in-game player.  Game environment, music, everything can be changed accordingly without touching the complicated game logic.&lt;br /&gt;
* '''Scalability''' Due to its simplicity and clarity of design, its very scalable and can be extended with multiple functionalities in the end.&lt;br /&gt;
* '''User Interface''' The user interface is also extendable, an example for that is macros and custom keys.  they can be implemented as a series of standard commands issued to controller.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
Its disadvantage arises only from the fact that its extremely verbose.&lt;br /&gt;
&lt;br /&gt;
* '''More Classes''' Can lead to lots more classes than expected.  Careful design and understanding is necessary to avoid both more classes as well as have lesser number of classes&lt;br /&gt;
* '''Design Trace''' It becomes hard to determine which functionality should be implemented where.  The base difficulty of coding is simple, but maintaining the design is sometimes confusing and tasking.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9595</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9595"/>
		<updated>2007-11-20T00:31:16Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;br /&gt;
&lt;br /&gt;
== What Is ==&lt;br /&gt;
&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
This is usually also called as Model-View-Controller Pattern.  The essence behind this is that when the business logic or data access components are large, a Controller class is introduced between user interface (''View'') and the core logic (''Model''). &lt;br /&gt;
&lt;br /&gt;
A very good example for this is the Java Swing Architecture.  In Swing, all GUI is defined via objects and classes derived from the JComponent object.  The business logic is defined in ActionListeners, Event Descriptors.&lt;br /&gt;
&lt;br /&gt;
To put things simply, &lt;br /&gt;
&lt;br /&gt;
An easy way to understand MVC: the model is the data, the view is the window on the screen, and the controller is the glue between the two. -- ''ConnellyBarnes''&lt;br /&gt;
&lt;br /&gt;
== SmallTalk ==&lt;br /&gt;
&lt;br /&gt;
However intuitive and simple idea it is, it has an origin in this work and its inventors (TrygveReenskaug and others).  Lets discuss how it actually works.  This is not a chat application as is usually the perception, but is actually a language that is inspired by the desire to make human computer interaction simpler and smoother.  The language has the concepts of object oriented Programming and each object has only following properties,&lt;br /&gt;
&lt;br /&gt;
   1. Hold state (references to other objects).&lt;br /&gt;
   2. Receive a message from itself or another object.&lt;br /&gt;
   3. In the course of processing a message, send messages to itself or another object.&lt;br /&gt;
&lt;br /&gt;
The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate.&lt;br /&gt;
&lt;br /&gt;
The working clearly indicates the semblances of the MVC pattern.&lt;br /&gt;
[[http://en.wikipedia.org/wiki/Smalltalk]]&lt;br /&gt;
&lt;br /&gt;
== An Example ==&lt;br /&gt;
&lt;br /&gt;
=== Http Request === &lt;br /&gt;
&lt;br /&gt;
  Browser           View              Controller      Model&lt;br /&gt;
    .                 .                 .               .&lt;br /&gt;
    . HTTP Request    .                 .               .&lt;br /&gt;
    +----------------------------------&amp;gt;+               .&lt;br /&gt;
    |                 .                 | update model  .&lt;br /&gt;
    |                 .                 +--------------&amp;gt;+&lt;br /&gt;
    |                 .                 |               |&lt;br /&gt;
    |                 .                 | return status |&lt;br /&gt;
    |                 .                 +&amp;lt;--------------+&lt;br /&gt;
    |                 .     select view |               .&lt;br /&gt;
    |                 +&amp;lt;----------------+               .&lt;br /&gt;
    |                 |                 .               .&lt;br /&gt;
    |                 | query state     .               .&lt;br /&gt;
    |                 +--------------------------------&amp;gt;+&lt;br /&gt;
    |                 |                 .               |&lt;br /&gt;
    |                 |                 .  return state |&lt;br /&gt;
    |                 +&amp;lt;--------------------------------+&lt;br /&gt;
    |   HTTP Response |                 .               .&lt;br /&gt;
    +&amp;lt;----------------+                 .               .&lt;br /&gt;
    .                 .                 .               .&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9593</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9593"/>
		<updated>2007-11-20T00:26:48Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;br /&gt;
&lt;br /&gt;
== What Is ==&lt;br /&gt;
&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
This is usually also called as Model-View-Controller Pattern.  The essence behind this is that when the business logic or data access components are large, a Controller class is introduced between user interface (''View'') and the core logic (''Model''). &lt;br /&gt;
&lt;br /&gt;
A very good example for this is the Java Swing Architecture.  In Swing, all GUI is defined via objects and classes derived from the JComponent object.  The business logic is defined in ActionListeners, Event Descriptors.&lt;br /&gt;
&lt;br /&gt;
To put things simply, &lt;br /&gt;
&lt;br /&gt;
An easy way to understand MVC: the model is the data, the view is the window on the screen, and the controller is the glue between the two. -- ''ConnellyBarnes''&lt;br /&gt;
&lt;br /&gt;
== SmallTalk ==&lt;br /&gt;
&lt;br /&gt;
However intuitive and simple idea it is, it has an origin in this work and its inventors (TrygveReenskaug and others).  Lets discuss how it actually works.  This is not a chat application as is usually the perception, but is actually a language that is inspired by the desire to make human computer interaction simpler and smoother.  The language has the concepts of object oriented Programming and each object has only following properties,&lt;br /&gt;
&lt;br /&gt;
   1. Hold state (references to other objects).&lt;br /&gt;
   2. Receive a message from itself or another object.&lt;br /&gt;
   3. In the course of processing a message, send messages to itself or another object.&lt;br /&gt;
&lt;br /&gt;
The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate.&lt;br /&gt;
&lt;br /&gt;
The working clearly indicates the semblances of the MVC pattern.&lt;br /&gt;
[[http://en.wikipedia.org/wiki/Smalltalk]]&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9589</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9589"/>
		<updated>2007-11-20T00:11:15Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;br /&gt;
&lt;br /&gt;
== What Is ==&lt;br /&gt;
&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
This is usually also called as Model-View-Controller Pattern.  The essence behind this is that when the business logic or data access components are large, a Controller class is introduced between user interface (''View'') and the core logic (''Model''). [[Image:upload.wikimedia.org/wikipedia/commons/2/2e/ModelViewControllerDiagram.svg]]&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9588</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9588"/>
		<updated>2007-11-20T00:11:02Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;br /&gt;
&lt;br /&gt;
== What Is ==&lt;br /&gt;
&lt;br /&gt;
'''Controller Pattern'''&lt;br /&gt;
&lt;br /&gt;
This is usually also called as Model-View-Controller Pattern.  The essence behind this is that when the business logic or data access components are large, a Controller class is introduced between user interface (''View'') and the core logic (''Model''). [[Image:http://upload.wikimedia.org/wikipedia/commons/2/2e/ModelViewControllerDiagram.svg]]&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9580</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9580"/>
		<updated>2007-11-20T00:03:01Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Question Number 6.'''&lt;br /&gt;
&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9578</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9578"/>
		<updated>2007-11-20T00:02:40Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
'''Question Number 6.'''&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9577</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 6 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_6_aa&amp;diff=9577"/>
		<updated>2007-11-20T00:02:23Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic ==&lt;br /&gt;
''Question Number 6.''&lt;br /&gt;
Take the Controller pattern (which we did not cover in class) and catalog the information on it available on the Web. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7712</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7712"/>
		<updated>2007-10-25T02:28:44Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
In simple layman words,  Coupling is a relationship between conceptswhile Cohesion, on the other hand is a metric reserved for a single concept (or a group of related concepts treated as a whole.) Cohesion is not really about relatedness. Cohesion is also not about completeness. &amp;lt;br&amp;gt;&lt;br /&gt;
Cohesion is more about integrity. i.e. Evaluating cohesion is about evaluating the 'Dogness' of the Dog model. or well something like that.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.ugolandini.net/AccoppiamentoCoesioneE.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.cs.unc.edu/~stotts/COMP145/modules.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://javaboutique.internet.com/tutorials/coupcoh/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.eli.sdsu.edu/courses/spring01/cs635/notes/module/module.html &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt; &lt;br /&gt;
[2] http://www.kellen.net/Coupling%20and%20Cohesion.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://forums.objectsbydesign.com/showthread.php?threadid=264 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://forum.java.sun.com/thread.jspa?threadID=5225243&amp;amp;messageID=9916000 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://corfield.org/blog/index.cfm/do/blog.entry/entry/Coupling_amp_Cohesion__Self_amp_Other &amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://www.compoundtheory.com/?action=displayPost&amp;amp;ID=63 &amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://www-static.cc.gatech.edu/classes/cs2390_97_summer/lectures/rdd/slide7.html &amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://codebetter.com/blogs/jeremy.miller/pages/129542.aspx &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://ieeexplore.ieee.org/Xplore/login.jsp?url=/iel5/9462/30025/01374314.pdf?arnumber=1374314 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7710</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7710"/>
		<updated>2007-10-25T02:27:37Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
In simple layman words,  Coupling is a relationship between conceptswhile Cohesion, on the other hand is a metric reserved for a single concept (or a group of related concepts treated as a whole.) Cohesion is not really about relatedness. Cohesion is also not about completeness. &amp;lt;br&amp;gt;&lt;br /&gt;
Cohesion is more about integrity. i.e. Dogs bark and Dogs wag their tails. Neither is necessarily related or similar, but if they were to be a part of a model of Dog, that model would be cohesive. Evaluating cohesion is about evaluating the 'Dogness' of the Dog model.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.ugolandini.net/AccoppiamentoCoesioneE.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.cs.unc.edu/~stotts/COMP145/modules.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://javaboutique.internet.com/tutorials/coupcoh/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.eli.sdsu.edu/courses/spring01/cs635/notes/module/module.html &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt; &lt;br /&gt;
[2] http://www.kellen.net/Coupling%20and%20Cohesion.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://forums.objectsbydesign.com/showthread.php?threadid=264 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://forum.java.sun.com/thread.jspa?threadID=5225243&amp;amp;messageID=9916000 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://corfield.org/blog/index.cfm/do/blog.entry/entry/Coupling_amp_Cohesion__Self_amp_Other &amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://www.compoundtheory.com/?action=displayPost&amp;amp;ID=63 &amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://www-static.cc.gatech.edu/classes/cs2390_97_summer/lectures/rdd/slide7.html &amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://codebetter.com/blogs/jeremy.miller/pages/129542.aspx &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://ieeexplore.ieee.org/Xplore/login.jsp?url=/iel5/9462/30025/01374314.pdf?arnumber=1374314 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7709</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7709"/>
		<updated>2007-10-25T02:27:19Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
In simple layman words,  Coupling is a relationship between conceptswhile Cohesion, on the other hand is a metric reserved for a single concept (or a group of related concepts treated as a whole.) Cohesion is not really about relatedness. Cohesion is also not about completeness.&lt;br /&gt;
Cohesion is more about integrity. i.e. Dogs bark and Dogs wag their tails. Neither is necessarily related or similar, but if they were to be a part of a model of Dog, that model would be cohesive. Evaluating cohesion is about evaluating the 'Dogness' of the Dog model.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.ugolandini.net/AccoppiamentoCoesioneE.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.cs.unc.edu/~stotts/COMP145/modules.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://javaboutique.internet.com/tutorials/coupcoh/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.eli.sdsu.edu/courses/spring01/cs635/notes/module/module.html &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt; &lt;br /&gt;
[2] http://www.kellen.net/Coupling%20and%20Cohesion.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://forums.objectsbydesign.com/showthread.php?threadid=264 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://forum.java.sun.com/thread.jspa?threadID=5225243&amp;amp;messageID=9916000 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://corfield.org/blog/index.cfm/do/blog.entry/entry/Coupling_amp_Cohesion__Self_amp_Other &amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://www.compoundtheory.com/?action=displayPost&amp;amp;ID=63 &amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://www-static.cc.gatech.edu/classes/cs2390_97_summer/lectures/rdd/slide7.html &amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://codebetter.com/blogs/jeremy.miller/pages/129542.aspx &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://ieeexplore.ieee.org/Xplore/login.jsp?url=/iel5/9462/30025/01374314.pdf?arnumber=1374314 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7706</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7706"/>
		<updated>2007-10-25T02:21:52Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.ugolandini.net/AccoppiamentoCoesioneE.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.cs.unc.edu/~stotts/COMP145/modules.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://javaboutique.internet.com/tutorials/coupcoh/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.eli.sdsu.edu/courses/spring01/cs635/notes/module/module.html &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt; &lt;br /&gt;
[2] http://www.kellen.net/Coupling%20and%20Cohesion.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://forums.objectsbydesign.com/showthread.php?threadid=264 &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://forum.java.sun.com/thread.jspa?threadID=5225243&amp;amp;messageID=9916000 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://corfield.org/blog/index.cfm/do/blog.entry/entry/Coupling_amp_Cohesion__Self_amp_Other &amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://www.compoundtheory.com/?action=displayPost&amp;amp;ID=63 &amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://www-static.cc.gatech.edu/classes/cs2390_97_summer/lectures/rdd/slide7.html &amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://codebetter.com/blogs/jeremy.miller/pages/129542.aspx &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://ieeexplore.ieee.org/Xplore/login.jsp?url=/iel5/9462/30025/01374314.pdf?arnumber=1374314 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7705</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7705"/>
		<updated>2007-10-25T02:20:59Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: /* Further Reading */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.ugolandini.net/AccoppiamentoCoesioneE.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.cs.unc.edu/~stotts/COMP145/modules.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://javaboutique.internet.com/tutorials/coupcoh/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.eli.sdsu.edu/courses/spring01/cs635/notes/module/module.html &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt; &lt;br /&gt;
[2] http://www.kellen.net/Coupling%20and%20Cohesion.htm ,br&amp;gt;&lt;br /&gt;
[3] http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://forums.objectsbydesign.com/showthread.php?threadid=264&lt;br /&gt;
[5] http://forum.java.sun.com/thread.jspa?threadID=5225243&amp;amp;messageID=9916000 &amp;lt;br&amp;gt;&lt;br /&gt;
[6] http://corfield.org/blog/index.cfm/do/blog.entry/entry/Coupling_amp_Cohesion__Self_amp_Other &amp;lt;br&amp;gt;&lt;br /&gt;
[7] http://www.compoundtheory.com/?action=displayPost&amp;amp;ID=63 &amp;lt;br&amp;gt;&lt;br /&gt;
[8] http://www-static.cc.gatech.edu/classes/cs2390_97_summer/lectures/rdd/slide7.html &amp;lt;br&amp;gt;&lt;br /&gt;
[9] http://codebetter.com/blogs/jeremy.miller/pages/129542.aspx &amp;lt;br&amp;gt;&lt;br /&gt;
[10] http://ieeexplore.ieee.org/Xplore/login.jsp?url=/iel5/9462/30025/01374314.pdf?arnumber=1374314 &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7702</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7702"/>
		<updated>2007-10-25T02:17:55Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.ugolandini.net/AccoppiamentoCoesioneE.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://www.cs.unc.edu/~stotts/COMP145/modules.html. &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://javaboutique.internet.com/tutorials/coupcoh/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.eli.sdsu.edu/courses/spring01/cs635/notes/module/module.html &amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1 &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7699</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7699"/>
		<updated>2007-10-25T02:16:56Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.ugolandini.net/AccoppiamentoCoesioneE.html.&lt;br /&gt;
[2] http://www.cs.unc.edu/~stotts/COMP145/modules.html.&lt;br /&gt;
[3] http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
[4] http://www.eli.sdsu.edu/courses/spring01/cs635/notes/module/module.html&lt;br /&gt;
[5] http://www.informit.com/articles/article.aspx?p=30496&amp;amp;rl=1&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7275</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7275"/>
		<updated>2007-10-24T16:50:18Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: /* Metrics */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For Cohesion:&lt;br /&gt;
&lt;br /&gt;
Data Complexity&lt;br /&gt;
D(i) = V(i) / f(i) + 1&lt;br /&gt;
&lt;br /&gt;
where&lt;br /&gt;
*D(i) = data complexity of module i&lt;br /&gt;
*V(i) = I/O variables in module i&lt;br /&gt;
f(i) = fanout of module i&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7268</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7268"/>
		<updated>2007-10-24T16:41:56Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7267</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7267"/>
		<updated>2007-10-24T16:41:12Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Coupling (C) = 1 - 1/(d_i + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7266</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7266"/>
		<updated>2007-10-24T16:40:22Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7265</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7265"/>
		<updated>2007-10-24T16:39:59Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled).&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7264</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7264"/>
		<updated>2007-10-24T16:39:22Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number &lt;br /&gt;
  ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled)&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7263</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7263"/>
		<updated>2007-10-24T16:38:43Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 11&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Metrics ==&lt;br /&gt;
&lt;br /&gt;
Coupling in Software Engineering &amp;lt;ref name=Pressman&amp;gt;Pressman, Roger S. Ph.D (1982).  Software Engineering - A Practitioner's Approach - Fourth Edition.  ISBN 0-07-052182-4&amp;lt;/ref&amp;gt; describes a version of metrics associated with this concept.&lt;br /&gt;
&lt;br /&gt;
For data and control flow coupling:&lt;br /&gt;
&lt;br /&gt;
  d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; = number of input control parameters&lt;br /&gt;
  d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output data parameters&lt;br /&gt;
  c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; = number of output control parameters&lt;br /&gt;
&lt;br /&gt;
For global coupling:&lt;br /&gt;
&lt;br /&gt;
  g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; = number of global variables used as data&lt;br /&gt;
  g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; = number of global variables used as control&lt;br /&gt;
&lt;br /&gt;
For environmental coupling:&lt;br /&gt;
&lt;br /&gt;
  w = number of modules called (fan-out)&lt;br /&gt;
  r = number of modules calling the module under consideration (fan-in)&lt;br /&gt;
&lt;br /&gt;
Coupling (C) = 1 - 1/(d&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt; + d&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + 2·c&amp;lt;sub&amp;gt;o&amp;lt;/sub&amp;gt; + g&amp;lt;sub&amp;gt;d&amp;lt;/sub&amp;gt; + 2·g&amp;lt;sub&amp;gt;c&amp;lt;/sub&amp;gt; + w + r)&lt;br /&gt;
&lt;br /&gt;
  &amp;quot;1 - ...&amp;quot; makes the value larger the more coupled the module is.  This number &lt;br /&gt;
  ranges from approximately 0.66 (low coupling) to 1.0 (highly coupled)&lt;br /&gt;
&lt;br /&gt;
For example, if a module has only a single input and output data parameter &lt;br /&gt;
&lt;br /&gt;
C = 1 - 1/(1+0+1+0+0+0+1+0) = 1 - 1/3 = 0.67&lt;br /&gt;
&lt;br /&gt;
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,&lt;br /&gt;
&lt;br /&gt;
C = 1 - 1/(5 + 2·5 + 5 + 2·5 + 10 + 0 + 3 + 4) = 0.98&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
== Further Reading ==&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7262</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7262"/>
		<updated>2007-10-24T16:35:45Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 10&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples include functions like validation procedures, fetching data from database, displaying information etc..&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
==== Examples ====&lt;br /&gt;
&lt;br /&gt;
Examples of activities in a sequentially cohesive module are:&lt;br /&gt;
*retrieve customer, retrieve customer order, and generate invoice&lt;br /&gt;
*get and edit input data&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7255</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7255"/>
		<updated>2007-10-24T16:31:24Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
*Drag Drop – an event triggered when a dragged object is dropped on a window&lt;br /&gt;
*Sum Elements in Array&lt;br /&gt;
*Convert Kilometers to Miles&lt;br /&gt;
*Read Customer Record&lt;br /&gt;
*Calculate Net Pay&lt;br /&gt;
*Assign Account Number&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7254</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7254"/>
		<updated>2007-10-24T16:30:44Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*This can lead to code duplication.&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following two examples have procedural cohesion, but has code duplication.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void printdata () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to print data&lt;br /&gt;
  ....&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function appropriately returns data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
retType returnData () {&lt;br /&gt;
  //code to fetch data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //code to modify data&lt;br /&gt;
  ....&lt;br /&gt;
&lt;br /&gt;
  //return data&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
&lt;br /&gt;
Drag Drop – an event triggered when a dragged object is dropped on a window,&lt;br /&gt;
Sum Elements in Array,&lt;br /&gt;
Convert Kilometers to Miles,&lt;br /&gt;
Read Customer Record,&lt;br /&gt;
Calculate Net Pay,&lt;br /&gt;
Assign Account Number.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7239</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7239"/>
		<updated>2007-10-24T16:23:50Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
The following are some examples.&lt;br /&gt;
&lt;br /&gt;
Drag Drop – an event triggered when a dragged object is dropped on a window,&lt;br /&gt;
Sum Elements in Array,&lt;br /&gt;
Convert Kilometers to Miles,&lt;br /&gt;
Read Customer Record,&lt;br /&gt;
Calculate Net Pay,&lt;br /&gt;
Assign Account Number.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7230</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7230"/>
		<updated>2007-10-24T16:18:20Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 9&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
*Sequential association the type in which the output data from one processing element serve as input data for the next processing element&lt;br /&gt;
*A module that performs multiple sequential functions where the sequential relationship among all of the functions is implied by the problems or application statement and where there is a data relationship among all of the functions&lt;br /&gt;
*Cure:&lt;br /&gt;
**Decompose into smaller modules &lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is the best form of cohesion.  Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
*If the operations of a module can be collectively described as a single specific function in a coherent way, the module has functional cohesion&lt;br /&gt;
*In an object-oriented system:&lt;br /&gt;
**Each operation in public interface of an object should be functional cohesive &lt;br /&gt;
**Each object should represent a single cohesive concept&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7225</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7225"/>
		<updated>2007-10-24T16:13:27Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 8&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
Common Occurrence:&lt;br /&gt;
*Object does not represent any single object-oriented concept&lt;br /&gt;
*Collection of commonly used source code as a class inherited via multiple inheritance&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Something&lt;br /&gt;
   {&lt;br /&gt;
   public static int findPattern( String text, String pattern)&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static int average( Vector numbers )&lt;br /&gt;
      { // blah}&lt;br /&gt;
   public static OutputStream openFile( String fileName )&lt;br /&gt;
      { // blah}&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
i.e. Module performs a set of related functions, one of which is selected via function parameter when calling the module&lt;br /&gt;
&lt;br /&gt;
Similar to control coupling&lt;br /&gt;
&lt;br /&gt;
Cure:&lt;br /&gt;
Isolate each function into separate operations &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public void sample( int flag )&lt;br /&gt;
   {&lt;br /&gt;
   switch ( flag )&lt;br /&gt;
      {&lt;br /&gt;
      case ON:&lt;br /&gt;
         // bunch of on stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case OFF:&lt;br /&gt;
         // bunch of off stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case CLOSE:&lt;br /&gt;
         // bunch of close stuff&lt;br /&gt;
         break;&lt;br /&gt;
      case COLOR:&lt;br /&gt;
         // bunch of color stuff&lt;br /&gt;
         break;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
Elements are grouped into a module because they are all processed within the same limited time period&lt;br /&gt;
&lt;br /&gt;
Common example:&lt;br /&gt;
*Constructors/Initialization modules that provide default values for objects&lt;br /&gt;
*Destructor/&amp;quot;End of Job&amp;quot; modules that clean up &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
procedure initializeData()&lt;br /&gt;
   {&lt;br /&gt;
   font = &amp;quot;times&amp;quot;;&lt;br /&gt;
   windowSize = &amp;quot;200,400&amp;quot;;&lt;br /&gt;
   foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
   foo.size = 12;&lt;br /&gt;
   foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * A way to do this properly in OOPS is as follows&lt;br /&gt;
 * Make sure that each object has a constructor and destructor&lt;br /&gt;
 * &lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
class foo&lt;br /&gt;
   {&lt;br /&gt;
   public foo()&lt;br /&gt;
      {&lt;br /&gt;
      foo.name = &amp;quot;Not Set&amp;quot;;&lt;br /&gt;
      foo.size = 12;&lt;br /&gt;
      foo.location = &amp;quot;/usr/local/lib/java&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
Associates processing elements on the basis of their procedural or algorithmic relationships&lt;br /&gt;
&lt;br /&gt;
*Procedural modules are application specific&lt;br /&gt;
*In context the module seems reasonable, however removed from the context these modules seem strange and very hard to understand&lt;br /&gt;
*Can not understand module without understanding the program and the conditions existing when module is called&lt;br /&gt;
*This kind of cohesion makes module hard to modify, understand&lt;br /&gt;
*Cure:&lt;br /&gt;
**Redesign the system, if a module is unnecessary, remove it from objects&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
==== Definition ====&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
*Operations of a module all operate upon the same input data set and/or produce the same output data&lt;br /&gt;
*Cure:&lt;br /&gt;
**Isolate each element into separate modules&lt;br /&gt;
*However good news is that, it rarely occurs in object-oriented systems due to polymorphism &lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion (best) ===&lt;br /&gt;
&lt;br /&gt;
Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
Since cohesion is a ranking type of scale, the ranks do not indicate a steady progression of improved cohesion.  Studies by various people including [[Larry Constantine]] and [[Edward Yourdon]] as well as others indicate that the first two types of cohesion are much inferior to the others and that modules with communicational cohesion or better tend to be much superior than lower types of cohesion.  The seventh type, functional cohesion, is considered the best type.&lt;br /&gt;
&lt;br /&gt;
However, while functional cohesion is considered the most desirable type of cohesion for a software module, it may not actually be achievable.  There are many cases where communicational cohesion is about the best that can be attained in the circumstances.  However the emphasis of a software design should be to maintain module cohesion of communicational or better since these types of cohesion are associated with modules of lower lines of code per module with the source code focused on a particular functional objective with less extranous or unnecessary functionality, and tend to be reusable under a greater variety of conditions.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7221</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7221"/>
		<updated>2007-10-24T15:55:51Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Stamp Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Data Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Control Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Common Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Content Coupling ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion (best) ===&lt;br /&gt;
&lt;br /&gt;
Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
Since cohesion is a ranking type of scale, the ranks do not indicate a steady progression of improved cohesion.  Studies by various people including [[Larry Constantine]] and [[Edward Yourdon]] as well as others indicate that the first two types of cohesion are much inferior to the others and that modules with communicational cohesion or better tend to be much superior than lower types of cohesion.  The seventh type, functional cohesion, is considered the best type.&lt;br /&gt;
&lt;br /&gt;
However, while functional cohesion is considered the most desirable type of cohesion for a software module, it may not actually be achievable.  There are many cases where communicational cohesion is about the best that can be attained in the circumstances.  However the emphasis of a software design should be to maintain module cohesion of communicational or better since these types of cohesion are associated with modules of lower lines of code per module with the source code focused on a particular functional objective with less extranous or unnecessary functionality, and tend to be reusable under a greater variety of conditions.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7220</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7220"/>
		<updated>2007-10-24T15:53:37Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 7&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;br /&gt;
&lt;br /&gt;
== Types of cohesion ==&lt;br /&gt;
Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a [[rubric (academic)|rubric]] to determine a cohesion classification.  The types of cohesion, in order of the worst to the best type, are as follows:&lt;br /&gt;
&lt;br /&gt;
=== Coincidental cohesion === &lt;br /&gt;
&lt;br /&gt;
This is worst form of cohesion.  Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).&lt;br /&gt;
&lt;br /&gt;
=== Logical cohesion === &lt;br /&gt;
&lt;br /&gt;
Logical cohesion is when parts of a module are grouped because they logically are categorised to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).&lt;br /&gt;
&lt;br /&gt;
=== Temporal cohesion ===&lt;br /&gt;
&lt;br /&gt;
Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).&lt;br /&gt;
&lt;br /&gt;
=== Procedural cohesion === &lt;br /&gt;
&lt;br /&gt;
Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).&lt;br /&gt;
&lt;br /&gt;
=== Communicational cohesion ===&lt;br /&gt;
&lt;br /&gt;
Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).&lt;br /&gt;
&lt;br /&gt;
=== Sequential cohesion ===&lt;br /&gt;
&lt;br /&gt;
Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).&lt;br /&gt;
&lt;br /&gt;
=== Functional cohesion (best) ===&lt;br /&gt;
&lt;br /&gt;
Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module (e.g. calculating the sine of an angle).&lt;br /&gt;
&lt;br /&gt;
=== Notes ===&lt;br /&gt;
&lt;br /&gt;
Since cohesion is a ranking type of scale, the ranks do not indicate a steady progression of improved cohesion.  Studies by various people including [[Larry Constantine]] and [[Edward Yourdon]] as well as others indicate that the first two types of cohesion are much inferior to the others and that modules with communicational cohesion or better tend to be much superior than lower types of cohesion.  The seventh type, functional cohesion, is considered the best type.&lt;br /&gt;
&lt;br /&gt;
However, while functional cohesion is considered the most desirable type of cohesion for a software module, it may not actually be achievable.  There are many cases where communicational cohesion is about the best that can be attained in the circumstances.  However the emphasis of a software design should be to maintain module cohesion of communicational or better since these types of cohesion are associated with modules of lower lines of code per module with the source code focused on a particular functional objective with less extranous or unnecessary functionality, and tend to be reusable under a greater variety of conditions.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7158</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7158"/>
		<updated>2007-10-24T03:19:14Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 6&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
Use of a flag to determine what the function will do.&lt;br /&gt;
&lt;br /&gt;
A flag called &amp;lt;I&amp;gt;done&amp;lt;/I&amp;gt; is passed into the function.  If the game is over, the flag done will be TRUE and the board will be printed differently than if the game is not done. A single function shouldn't have a dual purpose.  We should have written two different functions, one to print the board during the game and a different one to show the player where the ships were if he lost.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example illustrates the modification of a global variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
This example is simple and self explanatory, one function moves the control to another function using a label. (very very bad programming)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7155</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7155"/>
		<updated>2007-10-24T03:16:11Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 5&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
In this example, we are passing an entire RECTANGLE to each of the functions, even though the function really doesn't need to see or modify all of the members.  Written this way, the function must know the names of the members and is also allowed to change any of the members.  Each of these functions really should have been written to take primitive types, not the entire RECTANGLE.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
This example shows a better way to write the previous program.  Here we will be passing and returning only primitive data types.  They are all that is really needed by the functions and now the functions are more general, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7154</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7154"/>
		<updated>2007-10-24T03:14:00Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 5&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
==== Example ==== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
int    CalcArea      (int width, int length);&lt;br /&gt;
int    CalcPerimeter (int width, int length);&lt;br /&gt;
double CalcDiagonal  (int width, int length);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
&lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color  = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1.area = CalcArea(rect1.width, rect1.length);&lt;br /&gt;
   rect1.perimeter = CalcPerimeter(rect1.width, rect1.length);&lt;br /&gt;
   rect1.diagonal = CalcDiagonal(rect1.width, rect1.length);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int CalcArea (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int area;&lt;br /&gt;
&lt;br /&gt;
   area = width * length;&lt;br /&gt;
&lt;br /&gt;
   return area;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int CalcPerimeter (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   int perimeter;&lt;br /&gt;
&lt;br /&gt;
   perimeter = 2 * width + 2 * length;&lt;br /&gt;
&lt;br /&gt;
   return perimeter;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
double CalcDiagonal (int width, int length)&lt;br /&gt;
{&lt;br /&gt;
   double diagonal;&lt;br /&gt;
&lt;br /&gt;
   diagonal = sqrt (width * width&lt;br /&gt;
                    + length * length);&lt;br /&gt;
&lt;br /&gt;
   return diagonal;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
typedef struct rectangle&lt;br /&gt;
{	&lt;br /&gt;
   int length;&lt;br /&gt;
   int width; &lt;br /&gt;
   int area;&lt;br /&gt;
   int perimeter;&lt;br /&gt;
   int color;&lt;br /&gt;
   double diagonal;&lt;br /&gt;
   char symbol;&lt;br /&gt;
} RECTANGLE;&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea      (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r);&lt;br /&gt;
RECTANGLE CalcDiagonal  (RECTANGLE r);&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   RECTANGLE rect1;&lt;br /&gt;
    &lt;br /&gt;
   rect1.length = 7;&lt;br /&gt;
   rect1.width  = 6;&lt;br /&gt;
   rect1.color   = RED;&lt;br /&gt;
   rect1.symbol = '#';&lt;br /&gt;
&lt;br /&gt;
   rect1 = CalcArea (rect1);&lt;br /&gt;
   rect1 = CalcPerimeter (rect1);&lt;br /&gt;
   rect1 = CalcDiagonal (rect1);&lt;br /&gt;
&lt;br /&gt;
   printf(&amp;quot;For width: %d and length %d\n&amp;quot;,&lt;br /&gt;
          rect1.width, rect1.length);&lt;br /&gt;
   printf(&amp;quot;The area, perimeter and diagonal are\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;%d %d %f\n&amp;quot;, rect1.area, rect1.perimeter,&lt;br /&gt;
          rect1.diagonal);&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcArea (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.area = r.width * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcPerimeter (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.perimeter = 2 * r.width + 2 * r.length;&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
RECTANGLE CalcDiagonal (RECTANGLE r)&lt;br /&gt;
{&lt;br /&gt;
   r.diagonal = sqrt (r.width * r.width&lt;br /&gt;
                      + r.length * r.length);&lt;br /&gt;
&lt;br /&gt;
   return r;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7153</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7153"/>
		<updated>2007-10-24T03:11:48Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
    printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
    printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7152</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7152"/>
		<updated>2007-10-24T03:11:24Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
void PrintBoard (char board[ROWS][COLS], int rows, int cols, int done) { &lt;br /&gt;
  int i, j; &lt;br /&gt;
  printf(&amp;quot; 0 1 2 3 4 5 6 7 8 9\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); &lt;br /&gt;
  for(i = 0; i &amp;lt; rows; i++) { &lt;br /&gt;
    printf(&amp;quot;%d &amp;quot;, i); &lt;br /&gt;
    for(j = 0; j &amp;lt; cols; j++) { &lt;br /&gt;
      if(done) { &lt;br /&gt;
        printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
      } else { &lt;br /&gt;
        if(board[i][j] == ' ' || board[i][j] == 'O' || board[i][j] == 'X') { &lt;br /&gt;
           printf(&amp;quot;| %c &amp;quot;, board[i][j]); &lt;br /&gt;
           } else { &lt;br /&gt;
             printf(&amp;quot;| &amp;quot;); &lt;br /&gt;
           } &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
  printf(&amp;quot;|\n&amp;quot;); &lt;br /&gt;
  printf(&amp;quot; -----------------------------------------\n&amp;quot;); } } &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Function1 (int a) { &lt;br /&gt;
  if (a &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar++; &lt;br /&gt;
    a = 0; &lt;br /&gt;
  } &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Function2 (void) { &lt;br /&gt;
  if(myGlobalVar &amp;gt; 0) { &lt;br /&gt;
    myGlobalVar = 42; &lt;br /&gt;
  } else { &lt;br /&gt;
    myGlobalVar = -1; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
==== Example ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; &lt;br /&gt;
  goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7148</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7148"/>
		<updated>2007-10-24T03:07:04Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7146</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7146"/>
		<updated>2007-10-24T03:05:40Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7145</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7145"/>
		<updated>2007-10-24T03:05:23Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lanp=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7144</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7144"/>
		<updated>2007-10-24T03:03:45Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt; {&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
} &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7143</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7143"/>
		<updated>2007-10-24T03:03:22Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7142</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7142"/>
		<updated>2007-10-24T03:03:10Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
int Func1 (int a) { &lt;br /&gt;
  printf (&amp;quot;In Func1\n&amp;quot;); &lt;br /&gt;
  a += 2; goto F2A; &lt;br /&gt;
  return a; &lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
void Func2 (void) { &lt;br /&gt;
  printf(&amp;quot;In Func2\n&amp;quot;); &lt;br /&gt;
  F2A: &lt;br /&gt;
  printf(&amp;quot;At Func2A\n&amp;quot;); &lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7141</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7141"/>
		<updated>2007-10-24T02:55:38Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
*Generally, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7140</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7140"/>
		<updated>2007-10-24T02:53:10Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 4&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Content Coupling === &lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
*Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
*;Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*;Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;br /&gt;
&lt;br /&gt;
=== STAMP COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Stamp Coupling ====&lt;br /&gt;
&lt;br /&gt;
Stamp coupling occurs between modules when data are passed by parameters using a data structure containing fields which may or may not be used. &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, never pass a data structure containing many fields to a module that only needs a few.&lt;br /&gt;
*Normally we dont make a distinction between the passing of parameters in an unstructured format (as described under data coupling) and the passing of parameters in a data structure (stamp coupling).  The distinction between data and stamp coupling is not relevant in object-oriented systems.&lt;br /&gt;
*;Strengths of Stamp Coupling&lt;br /&gt;
**It is a loose form of coupling.&lt;br /&gt;
**It simplifies interfaces between modules.&lt;br /&gt;
*;Weaknesses of Stamp Coupling&lt;br /&gt;
**It forces the creation of artificial data structures (i.e., bundling of unrelated data elements together in a structure). Data structures are appropriate as long as the data bundled together is meaningful and related.&lt;br /&gt;
&lt;br /&gt;
=== CONTROL COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Control Coupling ====&lt;br /&gt;
&lt;br /&gt;
Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches).  &lt;br /&gt;
&lt;br /&gt;
*As a rule of thumb, use descriptive flags (i.e., a flag that describes a situation or condition, such as end-of-file).  Do not use control flags.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, control coupling occurs when an object receives a message from another object and the receiving object responds to the message differently based on control information contained in the message.  Another type of control coupling is evident when a method returns control information to an object in responding to a message.&lt;br /&gt;
**An example of object-oriented control coupling is a method that returns an error code with a value of zero when no errors are encountered handling a message.&lt;br /&gt;
*It only a moderate form of coupling and doesnt affect the quality of the program that much.&lt;br /&gt;
*Weaknesses of Control Coupling&lt;br /&gt;
**Control flags passed down the hierarchy require the calling program to know about the internals of the called program (i.e., the called program is not a black box).&lt;br /&gt;
**Control flags passed up the hierarchy cause a subordinate module to influence the flow of control in the parent module (i.e., the subordinate module has an affect on a module it does not control).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== COMMON COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Common Coupling ==== &lt;br /&gt;
&lt;br /&gt;
Common coupling occurs when modules communicate using global data areas (i.e., universal common data areas).  For example, C allows the developer to declare a data element as external, enabling it to be accessed by all modules.&lt;br /&gt;
&lt;br /&gt;
*Common coupling is also known as global coupling.&lt;br /&gt;
*;Object-Oriented Considerations&lt;br /&gt;
**In object-oriented systems, common coupling occurs when an object references a specific external object and has knowledge of the structure and implementation details of the external object.  Generally one should avoid the use of data in the public interface of an object.  However, it is sometimes necessary for objects to include items other than methods in their public interfaces.  In these instances, the general guideline is to use constants rather than variables.&lt;br /&gt;
*Weaknesses of Common Coupling&lt;br /&gt;
**Modules are very tightly coupled.  A fault in one module using global data may show up in another module because global data may be updated by any module at any time.&lt;br /&gt;
**Modules referencing global data are tied to specific data names, unlike modules that reference data through parameters.  This greatly limits the module's re-usability.&lt;br /&gt;
**It may take considerable time for a maintenance programmer to determine what module updated the global data.  The more global data used, the more difficult the job becomes to determine how modules obtain their data.&lt;br /&gt;
**It is more difficult to track the modules that access global data when a piece of global data is changed (e.g., a field size is changed).&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== CONTENT COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Content Coupling ====&lt;br /&gt;
&lt;br /&gt;
Content coupling occurs between two modules if one refers to the internals of the other module.  For example, if one module branches into another module, if one module refers to or changes data in another module, or if one module alters a statement in another module.  In practice, only assembler language allows content coupling.  Most procedural and object-oriented programming languages make it difficult to implement content coupling.&lt;br /&gt;
&lt;br /&gt;
*Content coupling is also known as pathological coupling.&lt;br /&gt;
*Avoid Content Coupling&lt;br /&gt;
*Content coupling is the worst level of coupling.  Content coupling should never be used when designing modules.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Determining Coupling Type ==&lt;br /&gt;
&lt;br /&gt;
When two modules are coupled in more than one way, the coupling type is determined by the worst coupling type.  For example, if two modules are data and control coupled, they are described as being control coupled.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Object Coupling ==&lt;br /&gt;
&lt;br /&gt;
Object-oriented applications are comprised of systems of interacting objects.  Objects must be coupled for these interactions to occur.  In object-oriented applications, coupling can take place between the object and all objects external to it or coupling can occur among the items (i.e., methods, state, component objects) that comprise an individual object. &lt;br /&gt;
&lt;br /&gt;
Object coupling is categorized into interface coupling and internal coupling.  Interface coupling describes the coupling between an object and objects external to it through a public interface.  Internal coupling describes how behaviors within an object interface with each other.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7138</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7138"/>
		<updated>2007-10-24T02:41:56Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: 3&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
;Note: Low coupling is usually results in high degree of cohesion.&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;br /&gt;
&lt;br /&gt;
== Types of coupling ==&lt;br /&gt;
&lt;br /&gt;
=== Content Coupling === &lt;br /&gt;
&lt;br /&gt;
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).   This should be avoided since any modification of data should be easy to find and easy to understand.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== DATA COUPLING ===&lt;br /&gt;
&lt;br /&gt;
==== Definition of Data Coupling ====&lt;br /&gt;
&lt;br /&gt;
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used. &lt;br /&gt;
&lt;br /&gt;
* Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.&lt;br /&gt;
* Strengths of Data Coupling&lt;br /&gt;
**A module sees only the data elements it requires.&lt;br /&gt;
**It is the best form of coupling.&lt;br /&gt;
*Weakness of Data Coupling&lt;br /&gt;
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7131</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7131"/>
		<updated>2007-10-24T02:24:12Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To get high cohesion, the following are some simple thumb rules&lt;br /&gt;
*The method must do only a single conceptual task. Es: Instance methods, Constructors, wrappers, getter/setter methods.&lt;br /&gt;
*A method must not assume the type/values of its parameters and arguments.&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7129</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7129"/>
		<updated>2007-10-24T02:20:43Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Low Coupling and High Cohesion with different methods ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* takes only input parameters, and only if it needs them to make the output&lt;br /&gt;
* returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes)&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7125</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7125"/>
		<updated>2007-10-24T02:16:38Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Methods and coupling ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* take only input parameters, and only if it needs them to make the output&lt;br /&gt;
* return a value (primitive or handle), or alter objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* return a value, alter parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes)&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7124</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 7 as</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_7_as&amp;diff=7124"/>
		<updated>2007-10-24T02:15:57Z</updated>

		<summary type="html">&lt;p&gt;Snviswan2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Topic == &lt;br /&gt;
&lt;br /&gt;
'''Cohesion and coupling''' Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.&lt;br /&gt;
&lt;br /&gt;
== Definition ==&lt;br /&gt;
&lt;br /&gt;
=== Coupling ===&lt;br /&gt;
&lt;br /&gt;
Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.&lt;br /&gt;
&lt;br /&gt;
*The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.&lt;br /&gt;
*The lower the coupling the “better”.&lt;br /&gt;
&lt;br /&gt;
=== Cohesion ===&lt;br /&gt;
&lt;br /&gt;
Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.&lt;br /&gt;
&lt;br /&gt;
*The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.&lt;br /&gt;
*The higher the cohesion the “better”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
;Some Small Definitions&lt;br /&gt;
&lt;br /&gt;
'''Module''' a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.&lt;br /&gt;
&lt;br /&gt;
'''Relationship''' A relationship exists between one module and another if that module cannot function correctly without the presence of the other.&lt;br /&gt;
&lt;br /&gt;
== Methods and coupling ==&lt;br /&gt;
&lt;br /&gt;
Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:&lt;br /&gt;
* '''Utility''': doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)&lt;br /&gt;
* '''State View''': use object state for ex: TextField.getText(), Object.toString()&lt;br /&gt;
* '''State Change''': modify object state for ex: Vector.addElement(..), Hashtable.clear() &lt;br /&gt;
&lt;br /&gt;
An utility method can reach the lowest possible coupling:&lt;br /&gt;
* INPUT:  take only input parameters, and only if it needs them to make the output&lt;br /&gt;
* OUTPUT: return a value (primitive or handle), or alter objects passed a parameters, and it produces all its output data (it doesn't call other methods) &lt;br /&gt;
&lt;br /&gt;
A State/View method with lowest possible coupling does:&lt;br /&gt;
* INPUT: takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* OUTPUT: return a value, alter parameters or throws an exception &lt;br /&gt;
&lt;br /&gt;
A State/Change method with lowest possible coupling does:&lt;br /&gt;
* INPUT: takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)&lt;br /&gt;
* OUTPUT: return a value, alter parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes)&lt;/div&gt;</summary>
		<author><name>Snviswan2</name></author>
	</entry>
</feed>