<?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=Smrober4</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=Smrober4"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Smrober4"/>
	<updated>2026-05-23T08:17:00Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16230</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16230"/>
		<updated>2008-07-31T01:15:23Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Single-choice */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Meyer's Principles == &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small [http://en.wikipedia.org/wiki/Interface_%28computer_science%29 interfaces] states that if two [http://en.wikipedia.org/wiki/Module_%28programming%29 modules] must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer responsible for implementing the interface.  By designing and implementing a small interface, the programmer also minimizes [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 coupling] between modules.&lt;br /&gt;
&lt;br /&gt;
Below is an example of a small interface written in [http://java.sun.com/javase/6/docs/technotes/guides/language/index.html Java].&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduces coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing too much information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface, which in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the [http://docs.eiffel.com/eiffelstudio/docs_no_content.html Eiffel] language does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented] languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, programmers must choose to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another through their individual interaction with a common [http://en.wikipedia.org/wiki/Global_variable global variable].  This is an example of [http://www.site.uottawa.ca:4321/oose/commoncoupling.html common coupling] and violates the principle of explicit interfaces.&lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but programmers must be sure to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-lang.org/en/ Ruby] supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
[http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx C#] is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, [http://en.wikipedia.org/wiki/C_(programming_language) C], and [http://en.wikipedia.org/wiki/C%2B%2B C++] are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the [http://www.microsoft.com/NET/ .NET] language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The single-choice principle limits the number of modules that must maintain the list of alternatives to one, thereby simplifying maintenance.  The single-choice principle has to do with separation of responsibility and encapsulation.  Like the small interfaces and explicit interfaces principles, following the single-choice principle is a design decision made by the programmer.  All object-oriented languages support the ability to abide by this principle, as long as the programmer follows good principles of object-oriented design.&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;br /&gt;
&lt;br /&gt;
== References == &lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 48]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 50]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 57]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 54]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 63]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16220</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16220"/>
		<updated>2008-07-31T00:48:44Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Explicit Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Meyer's Principles == &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small [http://en.wikipedia.org/wiki/Interface_%28computer_science%29 interfaces] states that if two [http://en.wikipedia.org/wiki/Module_%28programming%29 modules] must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer responsible for implementing the interface.  By designing and implementing a small interface, the programmer also minimizes [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 coupling] between modules.&lt;br /&gt;
&lt;br /&gt;
Below is an example of a small interface written in [http://java.sun.com/javase/6/docs/technotes/guides/language/index.html Java].&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduces coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing too much information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface, which in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the [http://docs.eiffel.com/eiffelstudio/docs_no_content.html Eiffel] language does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented] languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, programmers must choose to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another through their individual interaction with a common [http://en.wikipedia.org/wiki/Global_variable global variable].  This is an example of [http://www.site.uottawa.ca:4321/oose/commoncoupling.html common coupling] and violates the principle of explicit interfaces.&lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but programmers must be sure to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-lang.org/en/ Ruby] supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
[http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx C#] is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, [http://en.wikipedia.org/wiki/C_(programming_language) C], and [http://en.wikipedia.org/wiki/C%2B%2B C++] are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the [http://www.microsoft.com/NET/ .NET] language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The single-choice principle limits the number of modules that must maintain the list of alternatives to one, thereby simplifying maintenance.&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;br /&gt;
&lt;br /&gt;
== References == &lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 48]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 50]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 57]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 54]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 63]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16215</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16215"/>
		<updated>2008-07-31T00:41:27Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Small Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Meyer's Principles == &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small [http://en.wikipedia.org/wiki/Interface_%28computer_science%29 interfaces] states that if two [http://en.wikipedia.org/wiki/Module_%28programming%29 modules] must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer responsible for implementing the interface.  By designing and implementing a small interface, the programmer also minimizes [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 coupling] between modules.&lt;br /&gt;
&lt;br /&gt;
Below is an example of a small interface written in [http://java.sun.com/javase/6/docs/technotes/guides/language/index.html Java].&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduces coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing too much information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface, which in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the [http://docs.eiffel.com/eiffelstudio/docs_no_content.html Eiffel] language does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented] languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, programmers must choose to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common [http://en.wikipedia.org/wiki/Global_variable global variable].  This is an example of [http://www.site.uottawa.ca:4321/oose/commoncoupling.html common coupling] and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-lang.org/en/ Ruby] supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
[http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx C#] is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, [http://en.wikipedia.org/wiki/C_(programming_language) C], and [http://en.wikipedia.org/wiki/C%2B%2B C++] are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the [http://www.microsoft.com/NET/ .NET] language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The single-choice principle limits the number of modules that must maintain the list of alternatives to one, thereby simplifying maintenance.&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;br /&gt;
&lt;br /&gt;
== References == &lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 48]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 50]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 57]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 54]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 63]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16052</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16052"/>
		<updated>2008-07-27T16:36:21Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Small Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Meyer's Principles == &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small [http://en.wikipedia.org/wiki/Interface_%28computer_science%29 interfaces] states that if two [http://en.wikipedia.org/wiki/Module_%28programming%29 modules] must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 coupling] between modules.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in [http://java.sun.com/javase/6/docs/technotes/guides/language/index.html Java].&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing too much information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the [http://docs.eiffel.com/eiffelstudio/docs_no_content.html Eiffel] language does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented] languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common [http://en.wikipedia.org/wiki/Global_variable global variable].  This is an example of [http://www.site.uottawa.ca:4321/oose/commoncoupling.html common coupling] and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-lang.org/en/ Ruby] supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
[http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx C#] is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, [http://en.wikipedia.org/wiki/C_(programming_language) C], and [http://en.wikipedia.org/wiki/C%2B%2B C++] are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the [http://www.microsoft.com/NET/ .NET] language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The single-choice principle limits the number of modules that must maintain the list of alternatives to one, thereby simplifying maintenance.&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;br /&gt;
&lt;br /&gt;
== References == &lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 48]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 50]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 57]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 54]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 63]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16046</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16046"/>
		<updated>2008-07-27T03:23:50Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Meyer's Principles == &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small [http://en.wikipedia.org/wiki/Interface_%28computer_science%29 interfaces] states that if two [http://en.wikipedia.org/wiki/Module_%28programming%29 modules] must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 coupling] between modules.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in [http://java.sun.com/javase/6/docs/technotes/guides/language/index.html Java].&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the [http://docs.eiffel.com/eiffelstudio/docs_no_content.html Eiffel] language does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented] languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common [http://en.wikipedia.org/wiki/Global_variable global variable].  This is an example of [http://www.site.uottawa.ca:4321/oose/commoncoupling.html common coupling] and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-lang.org/en/ Ruby] supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
[http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx C#] is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, [http://en.wikipedia.org/wiki/C_(programming_language) C], and [http://en.wikipedia.org/wiki/C%2B%2B C++] are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the [http://www.microsoft.com/NET/ .NET] language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The single-choice principle limits the number of modules that must maintain the list of alternatives to one, thereby simplifying maintenance.&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;br /&gt;
&lt;br /&gt;
== References == &lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 48]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 50]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 57]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 54]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 63]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16045</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16045"/>
		<updated>2008-07-27T03:22:59Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small [http://en.wikipedia.org/wiki/Interface_%28computer_science%29 interfaces] states that if two [http://en.wikipedia.org/wiki/Module_%28programming%29 modules] must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 coupling] between modules.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in [http://java.sun.com/javase/6/docs/technotes/guides/language/index.html Java].&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the [http://docs.eiffel.com/eiffelstudio/docs_no_content.html Eiffel] language does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented] languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common [http://en.wikipedia.org/wiki/Global_variable global variable].  This is an example of [http://www.site.uottawa.ca:4321/oose/commoncoupling.html common coupling] and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
[http://www.ruby-lang.org/en/ Ruby] supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
[http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx C#] is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, [http://en.wikipedia.org/wiki/C_(programming_language) C], and [http://en.wikipedia.org/wiki/C%2B%2B C++] are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the [http://www.microsoft.com/NET/ .NET] language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The single-choice principle limits the number of modules that must maintain the list of alternatives to one, thereby simplifying maintenance.&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;br /&gt;
&lt;br /&gt;
== References == &lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 48]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 50]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 57]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 54]&lt;br /&gt;
# [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1 Object Oriented Software Construction, 2nd edition, page 63]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16044</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=16044"/>
		<updated>2008-07-27T03:06:17Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Single-choice */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small interfaces states that if two modules must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing coupling between modules.  The principle of small interfaces can be somewhat subjective because there is not a widely accepted test to determine whether the principle has been violated.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in Java.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the Eiffel languages does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All object-oriented languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [*].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common global variable.  This is an example of common coupling and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://en.wikipedia.org/wiki/Uniform_access_principle].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
Ruby supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
C# is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, C, and C++ are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [*].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the .NET language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [*].  The single-choice principle limits the number of modules that must maintain the list of alternatives to one, thereby simplifying maintenance.&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15977</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15977"/>
		<updated>2008-07-27T01:47:25Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small interfaces states that if two modules must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing coupling between modules.  The principle of small interfaces can be somewhat subjective because there is not a widely accepted test to determine whether the principle has been violated.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in Java.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the Eiffel languages does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All object-oriented languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [*].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common global variable.  This is an example of common coupling and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://en.wikipedia.org/wiki/Uniform_access_principle].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
Ruby supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
C# is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, C, and C++ are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [*].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the .NET language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of single-choice states that if a system provides a set of alternatives, then the complete list of alternatives are known by only one module [http://en.wikipedia.org/wiki/Single_choice_principle].  &lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15975</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15975"/>
		<updated>2008-07-27T01:45:58Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Self-documentation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small interfaces states that if two modules must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing coupling between modules.  The principle of small interfaces can be somewhat subjective because there is not a widely accepted test to determine whether the principle has been violated.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in Java.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the Eiffel languages does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All object-oriented languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [*].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common global variable.  This is an example of common coupling and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://en.wikipedia.org/wiki/Uniform_access_principle].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
Ruby supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
C# is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, C, and C++ are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of self-documentation states that all information about a particular module should be included as part of that module [*].  The self-documentation principle encourages keeping documentation related to a module embedded directly within the module, thereby encouraging the documentation of module to be synchronized with its functionality.  The Eiffel language does this very elegantly as shown in this [https://svn.origo.ethz.ch/eiffelstudio/branches/eth/origo_integration/Src/Eiffel/switch/communication/status/call_stack/eiffel_call_stack_classic.e example].&lt;br /&gt;
    &lt;br /&gt;
Java and many of the .NET language such as C#, provide special language features to promote the self-documentation principle.  In Java, programmers can use the special [http://en.wikipedia.org/wiki/Javadoc Javadoc] notation to describe classes and methods.  Here is an example of that notation for describing a [http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html Stack].&lt;br /&gt;
  /** &lt;br /&gt;
   * The Stack class represents a last-in-first-out (LIFO) stack of objects.&lt;br /&gt;
   */&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Creates an empty Stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     * Tests if this stack is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public boolean empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /**&lt;br /&gt;
     *  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
     */    &lt;br /&gt;
    public Object peek() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Removes the object at the top of this stack and returns that &lt;br /&gt;
     * object as the value of this function.&lt;br /&gt;
     */&lt;br /&gt;
    public Object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Pushes an item onto the top of this stack.&lt;br /&gt;
     */&lt;br /&gt;
    public Object push(Object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
C# also has a special syntax for embedding [http://msdn.microsoft.com/en-us/library/b2s063f7(VS.80).aspx XML Documentation] within its modules, which is highlighted by the following example.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  /// &amp;lt;summary&amp;gt; &lt;br /&gt;
  /// The Stack class represents a last-in-first-out (LIFO) stack of objects.  &lt;br /&gt;
  /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
  public class Stack {&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Creates an empty Stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public Stack() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Tests if this stack is empty.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public bool empty() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    ///  Looks at the object at the top of this stack without removing it from the stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;    &lt;br /&gt;
    public object peek() { // implementation... }&lt;br /&gt;
  &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Removes the object at the top of this stack and returns &lt;br /&gt;
    /// that object as the value of this function.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object pop() { // implementation... }&lt;br /&gt;
    &lt;br /&gt;
    /// &amp;lt;summary&amp;gt;&lt;br /&gt;
    /// Pushes an item onto the top of this stack.&lt;br /&gt;
    /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
    public object push(object item) { // implementation... }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This principle is a dificult to follow in C++, where syntax exists for creating comments, but there are not universally accepted conventions or tools for extracting documentation from the code.&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15884</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15884"/>
		<updated>2008-07-26T23:14:30Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Explicit Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small interfaces states that if two modules must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing coupling between modules.  The principle of small interfaces can be somewhat subjective because there is not a widely accepted test to determine whether the principle has been violated.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in Java.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the Eiffel languages does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All object-oriented languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of explicit interfaces states that if two modules must interact, then the interaction should be plainly visible [*].  Similar to the principle of small interfaces, this principle also speaks to the degree of coupling within a system.  For instance, two modules may inadvertently interact with one another via their individual interaction with a common global variable.  This is an example of common coupling and violates the principle of explicit interfaces.  &lt;br /&gt;
&lt;br /&gt;
Most object-oriented languages will support the principle of explicit interfaces, but it is up to the programmer to keep this principle in check.&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://en.wikipedia.org/wiki/Uniform_access_principle].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
Ruby supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
C# is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, C, and C++ are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The designer of a module should strive to make all information about the module part of the module itself&amp;quot; [ftp://ftp.idc.ac.il/pub/courses/cs/oosc/ch1-3.ppt#288,34,Self-Documentation%20Principle].&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15881</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15881"/>
		<updated>2008-07-26T23:13:12Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Uniform-access */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small interfaces states that if two modules must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing coupling between modules.  The principle of small interfaces can be somewhat subjective because there is not a widely accepted test to determine whether the principle has been violated.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in Java.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the Eiffel languages does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All object-oriented languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The principle of explicit interfaces maintains that all communications between modules must be visible. This means that as well as minimizing the number of interfaces and the amount of communication, all communications must also be clearly represented in the system: &lt;br /&gt;
In the following diagram we can see that a tightly coupled relationship exits between module A and module B, as they are both reliant on variable x. In order to adhere to the criteria of decomposability, composibilty, continuity and understandability the data relationship between A and B must be clearly marked:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
It is not disirable that A can change the value of x without this being visible in its code. Similarly, B’s interests in x must also be clearly visible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of uniform-access states that operations of a module must be accessible using a uniform notation.  In addition, this notation should not reveal to callers whether the evaluated operation was performed by accessing storage or by a computation [http://en.wikipedia.org/wiki/Uniform_access_principle].  Languages that support the uniform-access principle allow the implementation of a storage based property to change to a computed property without requiring changes to clients.&lt;br /&gt;
&lt;br /&gt;
Ruby supports the uniform-access principle as illustrated by this example.&lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    attr_accessor :circumference, :area&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @circumference = PI*radius*2&lt;br /&gt;
      @area = PI*radius*radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
In the above example, the &amp;lt;code&amp;gt;circumference&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;area&amp;lt;/code&amp;gt; properties of &amp;lt;code&amp;gt;Circle&amp;lt;/code&amp;gt; were referenced from storage.  We can easily change those properties to be computed when accessed without any changes to the client.  &lt;br /&gt;
&lt;br /&gt;
  class Circle&lt;br /&gt;
    # circumference and area are calculated and stored&lt;br /&gt;
    PI = 3.142&lt;br /&gt;
    &lt;br /&gt;
    def circumference()&lt;br /&gt;
      PI*@radius*2&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
    def area()&lt;br /&gt;
      PI*@radius*radius&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    def initialize(radius)&lt;br /&gt;
      @radius = radius&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # create a new circle&lt;br /&gt;
  c = Circle.new(4)&lt;br /&gt;
  # access properties of circle&lt;br /&gt;
  puts c.circumference&lt;br /&gt;
  puts c.circumference()&lt;br /&gt;
  puts c.area&lt;br /&gt;
  puts c.area ( )&lt;br /&gt;
&lt;br /&gt;
C# is another language that supports the uniform-access through the use of its [http://en.wikipedia.org/wiki/Property_%28programming%29 properties] language feature.  Java, C, and C++ are languages that do not support the uniform-access principle because accessing a field requires a different notation than accessing functions.&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The designer of a module should strive to make all information about the module part of the module itself&amp;quot; [ftp://ftp.idc.ac.il/pub/courses/cs/oosc/ch1-3.ppt#288,34,Self-Documentation%20Principle].&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15858</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15858"/>
		<updated>2008-07-26T22:13:15Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Small Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
Meyer’s principle of small interfaces states that if two modules must interact, then the least amount of information should be shared between them [http://www.amazon.com/Object-Oriented-Software-Construction-Prentice-Hall-International/dp/0136291554/ref=pd_bbs_sr_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1217110101&amp;amp;sr=8-1].  The correct implementation of this principle often depends on the programmer who is responsible for keeping their interfaces small, thereby decreasing coupling between modules.  The principle of small interfaces can be somewhat subjective because there is not a widely accepted test to determine whether the principle has been violated.&lt;br /&gt;
&lt;br /&gt;
Here is an example of a small interface in Java.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the EmployeeDb interface was kept relatively small, which in turn will reduce coupling with clients.  Using this same example, we can violate the principle of small interfaces by exposing more information to clients.&lt;br /&gt;
&lt;br /&gt;
  public class EmployeeDb {&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(string name, Date birthDate) { // implementation … }&lt;br /&gt;
    public List&amp;lt;Employee&amp;gt; findEmployees(FindCriteria criteria) { // implementation … }&lt;br /&gt;
    public void setDb(DbConnection db) { // implementation… }&lt;br /&gt;
    public DbConnection getDb() { // implementation… }&lt;br /&gt;
    public void setTransaction(Transaction trx) { // implementation… }&lt;br /&gt;
    public Transaction getTransaction() {  // implementation… }&lt;br /&gt;
    public Employee getEmployee(int employeeId) { // implementation… }&lt;br /&gt;
    public void saveEmployee(Employee emp) { // implementation… }&lt;br /&gt;
    public class FindCriteria {&lt;br /&gt;
      public FindCriteria(int employeeId, String name, Date birthDate, String phone) { // implementation… }&lt;br /&gt;
      public getEmployeeId() { // implementation… }&lt;br /&gt;
      public getName() { // implementation… }&lt;br /&gt;
      public getBirthDate() { // implementation… }&lt;br /&gt;
      public getPhone() { // implementation… }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The above example adds five additional operations to our interface that in turn increases the coupling of the EmployeeDb class to its clients.  This coupling hinders the ability to change the EmployeeDb class to use a different data source without affecting its clients.  In addition to exposing extra operations, this version of the EmployeeDb class also exposes a nested class to its clients.  This again is additional information being shared that potentially counters the principle of small interfaces.  It is worth noting that the Eiffel languages does not support nested classes.&lt;br /&gt;
&lt;br /&gt;
All object-oriented languages support the ability to abide by this principle, but they also support the ability to violate this principle.  Ultimately, it is up to programmers to follow the principle of small interfaces, thereby reducing coupling within their applications.&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The principle of explicit interfaces maintains that all communications between modules must be visible. This means that as well as minimizing the number of interfaces and the amount of communication, all communications must also be clearly represented in the system: &lt;br /&gt;
In the following diagram we can see that a tightly coupled relationship exits between module A and module B, as they are both reliant on variable x. In order to adhere to the criteria of decomposability, composibilty, continuity and understandability the data relationship between A and B must be clearly marked:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
It is not disirable that A can change the value of x without this being visible in its code. Similarly, B’s interests in x must also be clearly visible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The designer of a module should strive to make all information about the module part of the module itself&amp;quot; [ftp://ftp.idc.ac.il/pub/courses/cs/oosc/ch1-3.ppt#288,34,Self-Documentation%20Principle].&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15653</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15653"/>
		<updated>2008-07-25T21:19:30Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Self-documentation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The small interface or weak coupling principle states that if any two modules or objects communicate they should exchange as little information as possible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The principle of explicit interfaces maintains that all communications between modules must be visible. This means that as well as minimizing the number of interfaces and the amount of communication, all communications must also be clearly represented in the system: &lt;br /&gt;
In the following diagram we can see that a tightly coupled relationship exits between module A and module B, as they are both reliant on variable x. In order to adhere to the criteria of decomposability, composibilty, continuity and understandability the data relationship between A and B must be clearly marked:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
It is not disirable that A can change the value of x without this being visible in its code. Similarly, B’s interests in x must also be clearly visible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The designer of a module should strive to make all information about the module part of the module itself&amp;quot; [ftp://ftp.idc.ac.il/pub/courses/cs/oosc/ch1-3.ppt#288,34,Self-Documentation%20Principle].&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15235</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15235"/>
		<updated>2008-07-24T03:28:27Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Self-documentation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The small interface or weak coupling principle states that if any two modules or objects communicate they should exchange as little information as possible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The principle of explicit interfaces maintains that all communications between modules must be visible. This means that as well as minimizing the number of interfaces and the amount of communication, all communications must also be clearly represented in the system: &lt;br /&gt;
In the following diagram we can see that a tightly coupled relationship exits between module A and module B, as they are both reliant on variable x. In order to adhere to the criteria of decomposability, composibilty, continuity and understandability the data relationship between A and B must be clearly marked:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
It is not disirable that A can change the value of x without this being visible in its code. Similarly, B’s interests in x must also be clearly visible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The designer of a module should strive to make all information about the module part of the module itself&amp;quot; [ftp://ftp.idc.ac.il/pub/courses/cs/oosc/ch1-3.ppt#288,34,Self-Documentation Principle].&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15224</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15224"/>
		<updated>2008-07-24T02:51:27Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Explicit Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The small interface or weak coupling principle states that if any two modules or objects communicate they should exchange as little information as possible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The principle of explicit interfaces maintains that all communications between modules must be visible. This means that as well as minimizing the number of interfaces and the amount of communication, all communications must also be clearly represented in the system: &lt;br /&gt;
In the following diagram we can see that a tightly coupled relationship exits between module A and module B, as they are both reliant on variable x. In order to adhere to the criteria of decomposability, composibilty, continuity and understandability the data relationship between A and B must be clearly marked:&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
It is not disirable that A can change the value of x without this being visible in its code. Similarly, B’s interests in x must also be clearly visible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15223</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15223"/>
		<updated>2008-07-24T02:50:51Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Small Interfaces */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
&amp;quot;The small interface or weak coupling principle states that if any two modules or objects communicate they should exchange as little information as possible&amp;quot; [http://www.cit.gu.edu.au/~francis/sa2000/unit2.htm].&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15222</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15222"/>
		<updated>2008-07-24T02:45:08Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Also See */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eiffel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;br /&gt;
* [http://www.cetus-links.org/oo_eiffel.html Object Oriented Language: Eiffel]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15220</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15220"/>
		<updated>2008-07-24T02:37:49Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Also See */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Bertrand_Meyer Wikipedia: Bertrand Meyer]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eifel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15219</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15219"/>
		<updated>2008-07-24T02:35:42Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Uniform-access */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Uniform_access_principle]&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eifel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15218</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15218"/>
		<updated>2008-07-24T02:35:10Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Single-choice */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Single_choice_principle]&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eifel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15216</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15216"/>
		<updated>2008-07-24T02:34:15Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Also See */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 Wikipedia: Eifel Programming Language]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15213</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15213"/>
		<updated>2008-07-24T02:29:04Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Also See */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://c2.com/cgi/wiki?OoDesignPrinciples OO Design Principles]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://c2.com/cgi/wiki?BertrandMeyerAndHisOpinions Bertrand Meyer And His Opinions]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15212</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=15212"/>
		<updated>2008-07-24T02:26:50Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages? &lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://c2.com/cgi/wiki?OoDesignPrinciples OO Design Principles]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=14973</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=14973"/>
		<updated>2008-07-23T03:29:51Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
=== Small Interfaces ===&lt;br /&gt;
&lt;br /&gt;
=== Explicit Interfaces ===&lt;br /&gt;
&lt;br /&gt;
=== Uniform-access ===&lt;br /&gt;
&lt;br /&gt;
=== Self-documentation ===&lt;br /&gt;
&lt;br /&gt;
=== Single-choice ===&lt;br /&gt;
&lt;br /&gt;
== Also See == &lt;br /&gt;
&lt;br /&gt;
* [http://c2.com/cgi/wiki?OoDesignPrinciples OO Design Principles]&lt;br /&gt;
* [http://www.tucs.fi/summerschool2001/Meyer/meyer-architecture.pdf Slides: Principles of Object-Oriented Software Architecture]&lt;br /&gt;
* [http://www.geocities.com/tablizer/meyer1.htm Critique of Bertrand Meyer's Object Oriented Software Construction]&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=14968</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 8 smr</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_8_smr&amp;diff=14968"/>
		<updated>2008-07-23T03:22:10Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Meyer's principles.''' Bertrand Meyer is a prominent author in o-o design. He has developed a set of principles, many of which are embodied in the Eiffel language. Consider the principles of small interfaces, explicit interfaces, the uniform-access principle, the self-documentation principle, and the single-choice principle. What are good examples of each? Do other languages besides Eiffel support them? Is it difficult to follow these principles in certain o-o languages?&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14408</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14408"/>
		<updated>2008-07-08T03:25:09Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by the fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below), the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of their software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying loose coupling will often promote high cohesion and creating highly cohesive modules will often result in loose coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14407</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14407"/>
		<updated>2008-07-08T03:24:28Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by the fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below), the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of their software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14403</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14403"/>
		<updated>2008-07-08T03:19:16Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Procedural Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by the fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below), the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14400</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14400"/>
		<updated>2008-07-08T03:14:48Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Common coupling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by the fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14399</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14399"/>
		<updated>2008-07-08T03:14:03Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Procedural Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by the fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14398</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14398"/>
		<updated>2008-07-08T03:13:42Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Logical Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by the fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14395</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14395"/>
		<updated>2008-07-08T03:11:36Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14394</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14394"/>
		<updated>2008-07-08T03:11:23Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 [3]].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14393</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14393"/>
		<updated>2008-07-08T03:11:01Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 3].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14392</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14392"/>
		<updated>2008-07-08T03:10:39Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/&lt;br /&gt;
#http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/coupling.html&lt;br /&gt;
#http://en.wikipedia.org/wiki/Coupling_%28computer_science%29&lt;br /&gt;
#http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14382</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14382"/>
		<updated>2008-07-08T02:53:41Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated each of these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  Experience tells us, however, that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals within object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14381</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14381"/>
		<updated>2008-07-08T02:50:35Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Measuring Coupling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, &lt;br /&gt;
  primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, &lt;br /&gt;
  or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  However, experience tells us that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals in object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14377</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14377"/>
		<updated>2008-07-08T02:48:33Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Within this article, we have discussed the basic premise of cohesion and provided examples that illustrate the varying degrees of cohesion.  By creating modules that are highly cohesive, programmers can prevent costly maintenance, while increasing the readability, reliability, and reusability of their code.  Often related to the concept of cohesion is coupling, which we also discussed.  Coupling also has varying degrees and we illustrated these using several examples.  By reducing coupling between modules, programmers can encourage code reuse as well as simplify maintenance and testing of the software.  It is important to consider the relationship between these two similar concepts.  As mentioned in the introduction, the goal of object-oriented programming is to have highly cohesive, loosely coupled classes.  Applying the low coupling will often promote high cohesion and creating highly cohesive modules will often result in low coupling [http://en.wikipedia.org/wiki/Coupling_(computer_science)].  However, experience tells us that it is not always possible to achieve the highest degree of cohesion and the lowest degree of coupling.  Consequently, good engineers should use metrics to measure the degree of cohesion and coupling in their programs and be familiar with the different types of cohesion and coupling so that they may be successful in achieving one of the primary goals in object-oriented programming.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14373</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14373"/>
		<updated>2008-07-08T02:22:00Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Stamp coupling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14372</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14372"/>
		<updated>2008-07-08T02:21:36Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desirable form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14371</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14371"/>
		<updated>2008-07-08T02:20:57Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Data coupling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desirable form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; interact using only primitive types of data.  This is a desired form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14370</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14370"/>
		<updated>2008-07-08T02:19:37Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Stamp coupling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; classes share the common &amp;lt;code&amp;gt;Customer&amp;lt;/code&amp;gt; class.  This is a desirable form of coupling.&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14369</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14369"/>
		<updated>2008-07-08T02:17:02Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Control coupling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example [http://it.toolbox.com/blogs/enterprise-solutions/design-principles-coupling-data-and-otherwise-16061] illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;Client&amp;lt;/code&amp;gt; class controls the flow of execution within the &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; module.  This form of coupling requires modules calling &amp;lt;code&amp;gt;CustomerInfo&amp;lt;/code&amp;gt; to know about the flow of execution within its class.&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14368</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14368"/>
		<updated>2008-07-08T02:12:58Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Information cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14367</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14367"/>
		<updated>2008-07-08T02:12:32Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Sequential Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14363</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14363"/>
		<updated>2008-07-08T02:07:03Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Coupling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Sequential Cohesion=====&lt;br /&gt;
Sequential cohesion describes modules whose operations are intended to be executed in sequence with the output of each operation providing input to the subsequently executed operation.  The following example illustrates sequential cohesion.&lt;br /&gt;
&lt;br /&gt;
   class Cube {&lt;br /&gt;
       public ArrayList getInfo(){ &lt;br /&gt;
              ArrayList tempList = new ArrayList();&lt;br /&gt;
              tempList.add(getArea());&lt;br /&gt;
              tempList.add(getVolume());&lt;br /&gt;
              return tempList;&lt;br /&gt;
       }&lt;br /&gt;
       public double getArea(){return 6*side*side;} &lt;br /&gt;
       public double getVolume(){return side*side*side;}&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between them are said to be highly or tightly coupled.  Alternatively, modules that have very little connection between them are said to be lowly or loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and comprehension of the relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14362</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14362"/>
		<updated>2008-07-08T02:02:59Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Functional Cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Sequential Cohesion=====&lt;br /&gt;
Sequential cohesion describes modules whose operations are intended to be executed in sequence with the output of each operation providing input to the subsequently executed operation.  The following example illustrates sequential cohesion.&lt;br /&gt;
&lt;br /&gt;
   class Cube {&lt;br /&gt;
       public ArrayList getInfo(){ &lt;br /&gt;
              ArrayList tempList = new ArrayList();&lt;br /&gt;
              tempList.add(getArea());&lt;br /&gt;
              tempList.add(getVolume());&lt;br /&gt;
              return tempList;&lt;br /&gt;
       }&lt;br /&gt;
       public double getArea(){return 6*side*side;} &lt;br /&gt;
       public double getVolume(){return side*side*side;}&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the operations in the &amp;lt;code&amp;gt;Stack&amp;lt;/code&amp;gt; class facilitate the task of supporting a Stack data structure.&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between one another are said to be have high coupled or be tightly coupled.  Alternatively, modules that have very little connection between one another are said to have low coupling or be loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and general understanding of relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14361</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14361"/>
		<updated>2008-07-08T01:59:34Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Information cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
  &lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
    &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Sequential Cohesion=====&lt;br /&gt;
Sequential cohesion describes modules whose operations are intended to be executed in sequence with the output of each operation providing input to the subsequently executed operation.  The following example illustrates sequential cohesion.&lt;br /&gt;
&lt;br /&gt;
   class Cube {&lt;br /&gt;
       public ArrayList getInfo(){ &lt;br /&gt;
              ArrayList tempList = new ArrayList();&lt;br /&gt;
              tempList.add(getArea());&lt;br /&gt;
              tempList.add(getVolume());&lt;br /&gt;
              return tempList;&lt;br /&gt;
       }&lt;br /&gt;
       public double getArea(){return 6*side*side;} &lt;br /&gt;
       public double getVolume(){return side*side*side;}&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between one another are said to be have high coupled or be tightly coupled.  Alternatively, modules that have very little connection between one another are said to have low coupling or be loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and general understanding of relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14359</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14359"/>
		<updated>2008-07-08T01:58:55Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Information cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Communicational cohesion describes modules that perform several tasks against a shared data structure.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
&lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
  &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, all of the &amp;lt;code&amp;gt;RectangleTransformer&amp;lt;/code&amp;gt; operations are performing actions against the shared &amp;lt;code&amp;gt;rectangle&amp;lt;/code&amp;gt; member variable.&lt;br /&gt;
&lt;br /&gt;
=====Sequential Cohesion=====&lt;br /&gt;
Sequential cohesion describes modules whose operations are intended to be executed in sequence with the output of each operation providing input to the subsequently executed operation.  The following example illustrates sequential cohesion.&lt;br /&gt;
&lt;br /&gt;
   class Cube {&lt;br /&gt;
       public ArrayList getInfo(){ &lt;br /&gt;
              ArrayList tempList = new ArrayList();&lt;br /&gt;
              tempList.add(getArea());&lt;br /&gt;
              tempList.add(getVolume());&lt;br /&gt;
              return tempList;&lt;br /&gt;
       }&lt;br /&gt;
       public double getArea(){return 6*side*side;} &lt;br /&gt;
       public double getVolume(){return side*side*side;}&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between one another are said to be have high coupled or be tightly coupled.  Alternatively, modules that have very little connection between one another are said to have low coupling or be loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and general understanding of relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14358</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 6 cc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_6_cc&amp;diff=14358"/>
		<updated>2008-07-08T01:57:43Z</updated>

		<summary type="html">&lt;p&gt;Smrober4: /* Information cohesion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Cohesion and coupling. Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is worthwhile to gather readable illustrations of where they apply. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Categorize these examples, so that the reader will see the big picture, rather than just a set of redundant illustrations. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling.''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] and [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Coupling] are concepts often discussed when referring to [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming].  In fact, one of the primary goals of object-oriented programming is “to build highly cohesive classes and to maintain loose coupling between those classes” [http://javaboutique.internet.com/tutorials/coupcoh/].  This begs the question as to what is meant by cohesive classes and how do we know when classes are highly cohesive?   Further, what does loose coupling mean and how do we know it has been maintained?  This article combines information and examples gathered from the Web in an effort to shed some light on these very relevant questions.  &lt;br /&gt;
&lt;br /&gt;
In the first section of this article, we provide the basic definition of cohesion along with examples that describe the different types of cohesion.  Next, we explain the concept of coupling and use more examples to illustrate the different types of coupling.  In the third section of this article, we highlight some of the concepts related to cohesion and coupling, including ways to measure both and an illustration of [http://en.wikipedia.org/wiki/Law_of_Demeter Demeter’s Law].  Lastly, we provide a conclusion to the topics discussed herein.&lt;br /&gt;
&lt;br /&gt;
== Cohesion ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Cohesion] refers to the degree that a [http://en.wikipedia.org/wiki/Module_%28programming%29 module] performs one and only one function.  In this context, a module refers to any logical collection of “program entities” [http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf].  This logical collection of program entities could be a [http://en.wikipedia.org/wiki/Function_%28computer_science%29 function], a [http://en.wikipedia.org/wiki/Class_%28computer_science%29 class], or even a [http://en.wikipedia.org/wiki/Software_package_%28programming%29  package].  As stated earlier, the goal of object-oriented programming is to achieve highly cohesive classes.  High cohesion is desired because it increases the likelihood that a module will be comprehendible, reliable, robust, and reusable [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  Alternatively, a module that exemplifies low cohesion is difficult to comprehend, costly to maintain, and less likely to be reused [http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29].  The following examples illustrate the different types of cohesion and are arranged from lowest (least desirable) to highest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Coincidental Cohesion=====&lt;br /&gt;
Coincidental cohesion describes a module whose operations are unrelated to one another and the module itself can be used to achieve several different types of tasks.  The following example illustrates coincidental cohesion [http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069].&lt;br /&gt;
&lt;br /&gt;
  public static class BackendService {&lt;br /&gt;
    public static float computeNetPay(int orderId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public static float calculateInventoryReorderAmount(int inventoryId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public static boolean generateInvoice(int invoiceId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;BackendService&amp;lt;/code&amp;gt; provides a one stop shop for many different types of tasks.  This type of cohesion is the least desirable and should be avoided.&lt;br /&gt;
&lt;br /&gt;
=====Logical Cohesion=====&lt;br /&gt;
Logical cohesion describes a module that groups operations because categorically they are related but the operations themselves are quite different.  Typically, these modules accept a control flag which indicates which operation to execute.  The following example illustrates logical cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class DataStore {&lt;br /&gt;
    public void SaveData(int destination, byte[] data) {&lt;br /&gt;
      switch (destination) {&lt;br /&gt;
        default:&lt;br /&gt;
        case 0:&lt;br /&gt;
          SaveToDb(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 1:&lt;br /&gt;
          SaveToFile(data)&lt;br /&gt;
          break;&lt;br /&gt;
        &lt;br /&gt;
        case 2:&lt;br /&gt;
          SaveToWebService(data)&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToDb(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToFile(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void SaveToWebService(byte[] data) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, although all of the operations are logically related by teh fact that they all save data, the truth is they are in fact quite different.  Saving to a database likely requires a completely different implementation than saving to a file or web service.&lt;br /&gt;
&lt;br /&gt;
=====Temporal Cohesion=====&lt;br /&gt;
Temporal cohesion describes a module that has several operations grouped by the fact that the operations are executed within temporal proximity.  The following example illustrates temporal cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Startup {&lt;br /&gt;
    public void Initialize() {&lt;br /&gt;
       InitializeLogging();&lt;br /&gt;
       &lt;br /&gt;
       InitializeUI();&lt;br /&gt;
       &lt;br /&gt;
       InitializeDb();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeLogging() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeUI() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void InitializeDb() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
This example highlights a common implementation pattern where tasks are grouped simply by the fact that they need to be executed at around the same time.  Aside from needing to be executed at the same time, these operations implement tasks that are indeed quite different.&lt;br /&gt;
&lt;br /&gt;
=====Procedural Cohesion=====&lt;br /&gt;
Procedural cohesion describes a module whose operations are typically grouped because they are executed within a sequence.  Unlike sequential cohesion (discussed below) however, the operations within a procedurally cohesive module can be somewhat unrelated and output from one operation is not necessarily used as input to a subsequently executed operation.  The following example illustrates procedural cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class GradeBookSystem {&lt;br /&gt;
    public void Login(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public ArrayList GetStudents(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateGrades(ArrayList grades) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void UpdateAttendance(ArrayList dates) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void Logout(int teacherId) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;GradeBookSystem&amp;lt;/code&amp;gt; exposes different tasks that allow the teacher to login, track student grades/attendance and logout.  These operations are grouped in a procedurally cohesive manner to facilitate the higher level level task of upgrading the teacher's grade book.&lt;br /&gt;
&lt;br /&gt;
=====Communicational Cohesion=====&lt;br /&gt;
Communicational cohesion describes modules that perform multiple operations on the same input or output data.  The following example illustrates communication cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInformation {&lt;br /&gt;
    public CustomerInformation(int accountNum) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public String getName() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public float getBalance() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, the &amp;lt;code&amp;gt;getName&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getBalance&amp;lt;/code&amp;gt; operations facilitate tasks against the common input &amp;lt;code&amp;gt;accountNum&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===== Information cohesion =====&lt;br /&gt;
Information cohesion can do several things with the same data - a class with various methods using same data.  The following example illustrates information cohesion.&lt;br /&gt;
&lt;br /&gt;
  class RectangleTransformer {&lt;br /&gt;
&lt;br /&gt;
    Rectangle rectangle;&lt;br /&gt;
  &lt;br /&gt;
    public RectangleTransformer(Rectangle rectangle) { &lt;br /&gt;
      this.recentangle = rectangle; &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public double getArea() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public double getPermiter() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void flip() {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void stretch(double width, double height) {&lt;br /&gt;
      // implementation &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Sequential Cohesion=====&lt;br /&gt;
Sequential cohesion describes modules whose operations are intended to be executed in sequence with the output of each operation providing input to the subsequently executed operation.  The following example illustrates sequential cohesion.&lt;br /&gt;
&lt;br /&gt;
   class Cube {&lt;br /&gt;
       public ArrayList getInfo(){ &lt;br /&gt;
              ArrayList tempList = new ArrayList();&lt;br /&gt;
              tempList.add(getArea());&lt;br /&gt;
              tempList.add(getVolume());&lt;br /&gt;
              return tempList;&lt;br /&gt;
       }&lt;br /&gt;
       public double getArea(){return 6*side*side;} &lt;br /&gt;
       public double getVolume(){return side*side*side;}&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=====Functional Cohesion=====&lt;br /&gt;
Functional cohesion describes a module that is designed to perform one and only one task.  A functionally cohesive module may contain multiple methods, but all of these methods are designed to help the user achieve a single task.  The following example illustrates functional cohesion.&lt;br /&gt;
&lt;br /&gt;
  public class Stack {&lt;br /&gt;
    public Stack() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void push(Object obj) {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public Object pop() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public Object peek() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int isEmpty() {&lt;br /&gt;
      // implementation&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages and Disadvantages===&lt;br /&gt;
Cohesion is the idea that the module does a single task - be it calculating data, checking file etc. The &amp;quot;single task mindedness&amp;quot; drastically reduces code breaking when other modules are changed. If the module uses data from multiple other modules - if even one module changes or breaks, this module might need to be changed thus more time wasted. With single task modules, individual modules can be changed with very little problem.&lt;br /&gt;
&lt;br /&gt;
==Coupling==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Coupling] refers to the degree of connectedness between two modules.  Modules that have many connections between one another are said to be have high coupled or be tightly coupled.  Alternatively, modules that have very little connection between one another are said to have low coupling or be loosely coupled.  Introducing more coupling between modules, increases the interdependencies between modules.  As a result, attempting to reuse one module requires the “import” of all of its associated or coupled modules [http://www.site.uottawa.ca:4321/oose/coupling.html].  In addition, high coupling may introduce issues with code reuse, maintenance, testing, and general understanding of relationships between modules [http://en.wikipedia.org/wiki/Coupling_%28computer_science%29].  For these reasons, it is important to maintain loose coupling between classes.  The following examples illustrate the different types of coupling and are arranged from tightest (least desirable) to loosest (most desirable).&lt;br /&gt;
&lt;br /&gt;
=====Content coupling===== &lt;br /&gt;
Content coupling occurs when one or more modules access the internals of another module.  The following example illustrates content coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Rectangle {&lt;br /&gt;
  &lt;br /&gt;
    public int Top = 0;&lt;br /&gt;
    public int Left = 0;&lt;br /&gt;
    public int Width = 0;&lt;br /&gt;
    public int Height = 0;&lt;br /&gt;
    &lt;br /&gt;
    public Rectangle(int top, int left, int width, int height) {&lt;br /&gt;
      this.Top = top;&lt;br /&gt;
      this.Left = left;&lt;br /&gt;
      this.Width = width;&lt;br /&gt;
      this.Height = Height;&lt;br /&gt;
    }&lt;br /&gt;
     &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return this.Width * this.Height;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class FloorPlan {&lt;br /&gt;
    Rectangle rectangle = null;&lt;br /&gt;
  &lt;br /&gt;
    public FloorPlan(int width, int height) {&lt;br /&gt;
      rectangle = new Rectangle(0, 0, 50, 100);&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    public void modifyDimensions(int width, int height) {&lt;br /&gt;
      rectangle.Width = width;&lt;br /&gt;
      rectangle.Height = height;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public int getArea() {&lt;br /&gt;
      return rectangle.getArea();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In this example, &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; is able to directly modify the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object.  This coupling creates a dependency from &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; on the internals of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; object that inhibits maintenance of the &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class.  If someone wanted to go back and change the &amp;lt;code&amp;gt;Width&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;Height&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;Rectangle&amp;lt;/code&amp;gt; class to use a different data type they would also have to update the &amp;lt;code&amp;gt;FloorPlan&amp;lt;/code&amp;gt; class.   &lt;br /&gt;
&lt;br /&gt;
=====Common coupling===== &lt;br /&gt;
Common coupling occurs when two or more modules modify the same same global variable.  The following example illustrates common coupling.&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;string.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #define NUM_FIELDS 3&lt;br /&gt;
  &lt;br /&gt;
  class EmployeeRecordParser {&lt;br /&gt;
    public:&lt;br /&gt;
      EmployeeRecordParser(char* strRow, int nFields) : m_nCount(nFields), m_aryFields(0) {&lt;br /&gt;
  &lt;br /&gt;
        m_aryFields = new char*[m_nCount];&lt;br /&gt;
  &lt;br /&gt;
        char* strField = strtok(strRow, &amp;quot;,&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        for (int ct = 0; ct &amp;lt; m_nCount &amp;amp;&amp;amp; strField; ++ct) {&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct] = new char[strlen(strField) + 1];&lt;br /&gt;
 &lt;br /&gt;
           memcpy(m_aryFields[ct], strField, strlen(strField));&lt;br /&gt;
  &lt;br /&gt;
           m_aryFields[ct][strlen(strField)] = 0;&lt;br /&gt;
  &lt;br /&gt;
           strField = strtok(NULL, &amp;quot;,&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
       }&lt;br /&gt;
   	&lt;br /&gt;
      ~EmployeeRecordParser() {&lt;br /&gt;
         if (m_aryFields)			&lt;br /&gt;
           delete [] m_aryFields;&lt;br /&gt;
       }&lt;br /&gt;
  &lt;br /&gt;
       int GetCount() { return m_nCount; }&lt;br /&gt;
       char* operator[](int nIndex) { return GetField(nIndex); }&lt;br /&gt;
       char* GetField(int nIndex) { return nIndex &amp;lt; m_nCount ? m_aryFields[nIndex] : &amp;quot;&amp;quot;; }&lt;br /&gt;
   &lt;br /&gt;
     private:&lt;br /&gt;
       char**	m_aryFields;&lt;br /&gt;
       int	m_nCount;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  void ParseRecords(char* strFile) {&lt;br /&gt;
    int nRecords = 0;&lt;br /&gt;
    char* strRow = strtok(strFile, &amp;quot;\n&amp;quot;);&lt;br /&gt;
  &lt;br /&gt;
    while (strRow) {		&lt;br /&gt;
  &lt;br /&gt;
      EmployeeRecordParser record(strRow, NUM_FIELDS);&lt;br /&gt;
  &lt;br /&gt;
      printf(&amp;quot;\nEmployee Record %d\n------------------------\n&amp;quot;, ++nRecords);&lt;br /&gt;
  &lt;br /&gt;
      for (int i = 0; i &amp;lt; record.GetCount(); ++i)  {&lt;br /&gt;
        printf(&amp;quot;Field %d: %s\n&amp;quot;, i, record[i]);&lt;br /&gt;
      }&lt;br /&gt;
  &lt;br /&gt;
      strRow = strtok(NULL, &amp;quot;\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  int main() {&lt;br /&gt;
    char str[] = &amp;quot;Tom,Frank,919-777-2333\nMikel,Dundlin,919-234-5512\nRobert,Skoglund,919-232-2904&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
    ParseRecords(str);&lt;br /&gt;
   &lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the C++ example above, both the &amp;lt;code&amp;gt;ParseRecords&amp;lt;/code&amp;gt; method and the &amp;lt;code&amp;gt;EmployeeRecordParser&amp;lt;/code&amp;gt; class make use of the globally accessible [http://www.cplusplus.com/reference/clibrary/cstring/strtok.html strtok] function.  Internally, &amp;lt;code&amp;gt;strtok&amp;lt;/code&amp;gt; uses a static variable to track the position of the current string being tokenized, which is also used to determine when the whole string has been parsed.  In this particular example, the coupling on this common function has a side-effect that causes a bug that prevents all the records from being correctly parsed.&lt;br /&gt;
&lt;br /&gt;
=====Control coupling===== &lt;br /&gt;
Control coupling occurs when one module controls the execution flow of another module.  The following example illustrates control coupling.&lt;br /&gt;
&lt;br /&gt;
  enum InfoType { id, name, balance }&lt;br /&gt;
  &lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public Object getCustomerInfo(InfoType type) {&lt;br /&gt;
      Object returnVal = null;&lt;br /&gt;
      switch (infoType) {&lt;br /&gt;
        case InfoType.id:                    &lt;br /&gt;
          returnVal = getCustomerId();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.name:&lt;br /&gt;
          returnVal = getCustomerName();&lt;br /&gt;
          break;&lt;br /&gt;
  &lt;br /&gt;
        case InfoType.balance:&lt;br /&gt;
          returnVal = getCustomerBalance();&lt;br /&gt;
          break;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;br /&gt;
      return returnVal;      &lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      int id = (int)customerInfo.getCustomerInfo(InfoType.id);&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Stamp coupling=====&lt;br /&gt;
Stamp coupling occurs when two or more modules access or modify the same data of a shared object.  The following example illustrates stamp coupling.&lt;br /&gt;
&lt;br /&gt;
  public class Customer {&lt;br /&gt;
    private int id = 0;&lt;br /&gt;
    private String name = &amp;quot;&amp;quot;;&lt;br /&gt;
    private float balance = 0.0f;&lt;br /&gt;
  &lt;br /&gt;
    public int getId() { return id; }&lt;br /&gt;
  &lt;br /&gt;
    public void setId(int _id) { id = _id; }&lt;br /&gt;
  &lt;br /&gt;
    public String getName() { return name; }&lt;br /&gt;
  &lt;br /&gt;
    public void setName(String _name) { name = _name; }&lt;br /&gt;
  &lt;br /&gt;
    public float getBalance() { return balance; }&lt;br /&gt;
  &lt;br /&gt;
    public void setBalance(float _balance) { balance = _balance; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo() {&lt;br /&gt;
    public void save(Customer customer) {&lt;br /&gt;
      int id = customer.getId();&lt;br /&gt;
      String name = gustomer.getName();&lt;br /&gt;
      // ...&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public class Client {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
  &lt;br /&gt;
    public void execute() {&lt;br /&gt;
      Customer customer = new Customer();&lt;br /&gt;
  &lt;br /&gt;
      customer.setId(5);&lt;br /&gt;
      customer.setName(&amp;quot;Example&amp;quot;);&lt;br /&gt;
      customer.setBalance(100f);&lt;br /&gt;
      &lt;br /&gt;
      customerInfo.save(customer);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
=====Data coupling===== &lt;br /&gt;
Data coupling occurs when one module passes primitive type or simple data structure to another module as an argument.  The following example illustrates data coupling.&lt;br /&gt;
&lt;br /&gt;
  public class CustomerInfo&lt;br /&gt;
  {&lt;br /&gt;
    public float getCustomerBalance(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
      // implementation details&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
    &lt;br /&gt;
  public class Client&lt;br /&gt;
  {&lt;br /&gt;
    private customerInfo = new CustomerInfo();&lt;br /&gt;
    &lt;br /&gt;
    public void execute(int customerId)&lt;br /&gt;
    {&lt;br /&gt;
        float balance = customerInfo.getCustomerBalance(customerId);&lt;br /&gt;
  &lt;br /&gt;
        // ...    &lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
 &lt;br /&gt;
=====Message coupling=====&lt;br /&gt;
This is the loosest type of coupling.  Modules are not dependent on each other, instead they use a public interface to exchange parameter-less messages.  The following example illustrates messag coupling.&lt;br /&gt;
&lt;br /&gt;
       class superclass {&lt;br /&gt;
           public void processData(){ module1.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
       public Module2 {&lt;br /&gt;
           public void doSomething() { superclass.processData(); }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
=====No coupling===== &lt;br /&gt;
Modules do not communicate at all with one another.&lt;br /&gt;
&lt;br /&gt;
===Advantages and disadvantages===&lt;br /&gt;
Coupling allows interaction between different modules so more complicated tasks can be done. However, a strong coupling will decrease the flexibility of the modules and it will be harder to main and understand. If coupling is too tight, changing one module might have a &amp;quot;snowball effect&amp;quot; and will require changes of other modules that are dependent on it. Coupling must be used with caution and modules must use exactly what it needs and nothing more.&lt;br /&gt;
&lt;br /&gt;
==Related Concepts==&lt;br /&gt;
When researching cohesion and coupling there are some related concepts that repeatedly appear.  Below we discuss some related concepts to cohesion and coupling. &lt;br /&gt;
&lt;br /&gt;
====Measuring Cohesion====&lt;br /&gt;
The goal of well-designed systems is to have highly cohesive modules.  Below are three metrics that can be used to determine the level of cohesion within a system.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 1 (LCOM1)'''&lt;br /&gt;
  LCOM1 = (P &amp;gt; Q) ? (P – Q) : 0&lt;br /&gt;
  &lt;br /&gt;
  P = Total number of method pairs that do not use a common field of the class.&lt;br /&gt;
  Q = Total number of method pairs that access at least one common field of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM1 values indicate higher cohesion and better overall design.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 2 (LCOM2)'''&lt;br /&gt;
  LCOM2 = 1 – sum(mA)/(m*a)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
Lower LCOM2 values indicate higher cohesion and better overall design.  If the total number of methods or attributes is zero than the value of LCOM2 is undefined.&lt;br /&gt;
&lt;br /&gt;
'''Lack of Cohesion 3 (LCOM3)'''&lt;br /&gt;
&lt;br /&gt;
  LCOM3 = (m – sum(mA)/a) / (m – 1)&lt;br /&gt;
  &lt;br /&gt;
  m = Total number of methods in the class.&lt;br /&gt;
  a = Total number of attributes in the class.&lt;br /&gt;
  mA = Total number of methods that access attribute a.&lt;br /&gt;
  sum(mA) = Sum of all mA for all attributes of the class.&lt;br /&gt;
&lt;br /&gt;
LCOM3 values greater than one indicates low cohesion and should be addressed.  If the total number of methods is less than two or the number of attributes is zero than the value of LCOM3 is undefined.&lt;br /&gt;
&lt;br /&gt;
====Measuring Coupling====&lt;br /&gt;
While it is impossible to avoid some level of coupling within systems, the goal is to reduce coupling as much as possible.  Below are three metrics that can be used to determine the level of coupling within a system.&lt;br /&gt;
&lt;br /&gt;
'''Coupling Between Objects (CBO)'''&lt;br /&gt;
&lt;br /&gt;
  CBO = sum(t)&lt;br /&gt;
  &lt;br /&gt;
  t = Total number of types that are referenced by a particular class, not including any possible super-classes, primitive types or common framework classes.&lt;br /&gt;
&lt;br /&gt;
Lower CBO values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Data Abstraction Coupling (DAC)'''&lt;br /&gt;
&lt;br /&gt;
  DAC = sum(a)&lt;br /&gt;
  &lt;br /&gt;
  a = Total number of types that are used for attribute declarations, not including primitive types, common framework classes, or types that are inherited from any possible super-classes.&lt;br /&gt;
&lt;br /&gt;
Lower DC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
'''Method Invocation Coupling (MIC)'''&lt;br /&gt;
&lt;br /&gt;
  MIC = nMIC / (N – 1)&lt;br /&gt;
  &lt;br /&gt;
  N = Total number of classes defined within the project.&lt;br /&gt;
  nMIC = Total number of classes that receive a message from the target class.&lt;br /&gt;
&lt;br /&gt;
Lower MIC values indicate lower coupling.&lt;br /&gt;
&lt;br /&gt;
====Demeter's Law====&lt;br /&gt;
[http://en.wikipedia.org/wiki/Law_of_Demeter Demeter's Law] is a design principle that when applied to object-oriented programming means that object A can reference object B but object A cannot use object B to reference object C.  Complying with this principle prevents object A from knowing that object B uses object C thereby reducing coupling.  If object A needs to access a function of object C then it is up to object B to expose an operation encapsulating the reference to object C.  The following example [http://javaboutique.internet.com/tutorials/coupcoh/index-2.html] illustrates how this could be done.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
     return order.getProducts().getTotalCost();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In the example object the object which implements &amp;lt;code&amp;gt;calculateTotal()&amp;lt;/code&amp;gt; is calling &amp;lt;code&amp;gt;getTotalCost&amp;lt;/code&amp;gt; on a &amp;lt;code&amp;gt;Products&amp;lt;/code&amp;gt; object which is exposed through &amp;lt;code&amp;gt;order&amp;lt;/code&amp;gt;.  An alternative to this approach would be for the order object to expose this functionality as suggested by the following example.&lt;br /&gt;
&lt;br /&gt;
  public float calculateTotal(Order order) {&lt;br /&gt;
    return order.getTotalCost()&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public class Order {&lt;br /&gt;
    // ...&lt;br /&gt;
  &lt;br /&gt;
    public float getTotalCost() {&lt;br /&gt;
      return products.getTotalCost();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    // ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Coupling and Cohesion go hand in hand. On one hand cohesion wants modules to do exactly a single task thus reduces problems and making a module handle itself while on the other hand coupling is introduced by how dependent the module is on other modules. Good programming desires high cohesion and low coupling - modules that does one task without affecting other modules while at the same time use as less data/objects from other modules as possible to have low coupling. Although low coupling is desirable, that doesn't mean high coupling doesn't have its uses. In some modules the programmer might want it to be tightly coupled for performance reasons - perhaps a series of modules dependent on each other to bring out the maximum output. A non-IT way of seeing this might be our human body - it is tightly coupled and parts/modules are all dependent on one and another - &amp;quot;removing or fixing&amp;quot; one will involve many other; Even though high coupling is bad but the human body functions much better tightly coupled than loosely coupled. So basically even though high cohesion and lose coupling is very desirable, the programmer has to view the design of the system to figure out what the best approach is.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29 Wikipedia: Cohesion]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Coupling_%28computer_science%29 Wikipedia: Coupling]&lt;br /&gt;
*[http://www.cs.kent.edu/~jmaletic/cs63901/downloads/CouplingCohesion.pdf Software Engineeriing Presentation: Coupling and Cohesion]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node23.html More Cohesion Metrics]&lt;br /&gt;
*[http://www.frontendart.com/monitor/help/node22.html More Coupling Metrics]&lt;br /&gt;
*[http://javaboutique.internet.com/tutorials/coupcoh/index-2.html Example: Measuring Coupling and Cohesion]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-cohesion-16069&lt;br /&gt;
#http://www.waysys.com/ws_content_bl_pgssd_ch06.html&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#cohesion&lt;br /&gt;
#http://javaboutique.internet.com/tutorials/coupcoh/index-2.html&lt;br /&gt;
#http://bmrc.berkeley.edu/courseware/cs169/spring01/lectures/objects/sld001.htm&lt;br /&gt;
#http://blogs.ittoolbox.com/eai/implementation/archives/design-principles-coupling-data-and-otherwise-16061&lt;br /&gt;
#http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29&lt;br /&gt;
#http://www.cs.sjsu.edu/faculty/pearce/modules/lectures/ood/metrics/Cohesion.htm&lt;br /&gt;
#http://class.ee.iastate.edu/berleant/home/Courses/SoftwareEngineering/CprE486fall2004/designModularity.htm&lt;br /&gt;
#http://www.eli.sdsu.edu/courses/spring99/cs535/notes/cohesion/cohesion.html#Heading8&lt;br /&gt;
#http://www.site.uottawa.ca:4321/oose/index.html#sequentialcohesion&lt;/div&gt;</summary>
		<author><name>Smrober4</name></author>
	</entry>
</feed>