CSC/ECE 517 Fall 2007/wiki2 7 as: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
mNo edit summary
(3)
Line 19: Line 19:
*The higher the cohesion the “better”.
*The higher the cohesion the “better”.


;Note: Low coupling is usually results in high degree of cohesion.


;Some Small Definitions
;Some Small Definitions
Line 48: Line 49:


To get high cohesion, the following are some simple thumb rules
To get high cohesion, the following are some simple thumb rules
*The method must do only a single conceptual task. Es: Instance methods, Constructors, wrappers, getter/setter methods.
*The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.
*A method must not assume the type/values of its parameters and arguments.
*A method must not assume the type/values of its parameters and arguments.
== Types of coupling ==
=== Content Coupling ===
Two modules are content coupled if one directly references contents of the other.  This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods.  (like using public variables in a class/global variables without appropriate precautions).  This should be avoided since any modification of data should be easy to find and easy to understand.
=== DATA COUPLING ===
==== Definition of Data Coupling ====
Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used.
* Use data coupling when the argument list is small.  As a rule of thumb, limit the argument list to three items.
* Strengths of Data Coupling
**A module sees only the data elements it requires.
**It is the best form of coupling.
*Weakness of Data Coupling
**A module can be difficult to maintain if many data elements are passed.  Too many parameters can also indicate that a module has been poorly partitioned.

Revision as of 02:41, 24 October 2007

Topic

Cohesion and coupling Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.

Definition

Coupling

Coupling is simply defined as the degree of interdependence between modules or its like a measure of the strength of interconnection of modules or objects.

  • The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module.
  • The lower the coupling the “better”.

Cohesion

Cohesion can be described as the extent to which its individual components are needed to perform the same task i.e. how tightly bound or related [a module’s] internal elements are to one another.

  • The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand.
  • The higher the cohesion the “better”.
Note
Low coupling is usually results in high degree of cohesion.
Some Small Definitions

Module a lexically contiguous sequence of program statements bounded by boundary elements, having an aggregate identifier.

Relationship A relationship exists between one module and another if that module cannot function correctly without the presence of the other.

Low Coupling and High Cohesion with different methods

Assuming the state of an object is the snapshot of its attributes in the time, all methods can belong to one of these three categories:

  • Utility: doesn't use or modify object state for ex: Integer.toString(int), Math.cos(double)
  • State View: use object state for ex: TextField.getText(), Object.toString()
  • State Change: modify object state for ex: Vector.addElement(..), Hashtable.clear()


An utility method can reach the lowest possible coupling:

  • takes only input parameters, and only if it needs them to make the output
  • returns a value (primitive or handle), or alters objects passed a parameters, and it produces all its output data (it doesn't call other methods)

A State/View method with lowest possible coupling does:

  • takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)
  • returns a value, alters parameters or throws an exception

A State/Change method with lowest possible coupling does:

  • takes input parameters and uses attributes or methods (class or instance ones) belonging to its class (i.e. in every way but not from class members of others classes, constants excluded)
  • returns a value, alters parameters or throws an exception , or through attributes and methods (class or instance ones) belonging to its class (i.e. in every way but not with class members of others classes)


To get high cohesion, the following are some simple thumb rules

  • The method must do only a single conceptual task. Ex: Instance methods, Constructors, wrappers, getter/setter methods.
  • A method must not assume the type/values of its parameters and arguments.

Types of coupling

Content Coupling

Two modules are content coupled if one directly references contents of the other. This can occur when you directly modify an instance variable of a instantiated class without using getter-setter methods. (like using public variables in a class/global variables without appropriate precautions). This should be avoided since any modification of data should be easy to find and easy to understand.


DATA COUPLING

Definition of Data Coupling

Data coupling occurs between two modules when data are passed by parameters using a simple argument list and every item in the list is used.

  • Use data coupling when the argument list is small. As a rule of thumb, limit the argument list to three items.
  • Strengths of Data Coupling
    • A module sees only the data elements it requires.
    • It is the best form of coupling.
  • Weakness of Data Coupling
    • A module can be difficult to maintain if many data elements are passed. Too many parameters can also indicate that a module has been poorly partitioned.