Editorials

Cohesion

Be a SSWUG.ORG Beta Tester!
By late Jan. 2012, SSWUG.ORG will be launching a new version of our site. We’ll have a fresh new look with some new cool features. However, we want to make sure it ready for the masses.

If you want to be one of the first to see the new SSWUG.ORG, and help determine it’s final look, you can participate in our CTP, and receive a free year of premium membership for your effort.

To be eligible to participate you need to complete this survey in totallity by Dec. 23.

Cohesion
I started exercising my New Year’s resolution early. I decided that after writing software for decades it was time to go back to the beginning and brush up on the foundations I tend to forget. I’ll tell someone that they should do something a certain way, but can’t remember the root problem I solved.

So, lately I have been reviewing Object Oriented Programming. The topic of cohesion is fresh in my mind. Today I get to share some of what I have re-learned.

Cohesion defines the efficiency of a method or function in your programs. The goal is to have methods that solve a single task, or at least solve related tasks. I learned cohesion by writing unit tests. Methods that do not follow the discipline of cohesion are difficult, if not impossible to test. Now I find that the term cohesion defines what I learned empirically through testing.

Here are some kinds of cohesion:

  • Functional – A method that does only one specific thing
  • Sequential – A method that does a number of things in the same order. The sequences share the same data.
  • Communicational – This is a weak form of cohesion where the methods are only related in that they operate on the same data.
  • Temporal – Functions that are executed based on time, often found as constructor in destructor methods for an object, or managers of state.
  • Procedural – This form of cohesion performs a number of things in the same order, while not sharing data like Sequential cohesion. In this case, the steps are better converted into a series o f Functional methods, each called by the original method operating as a sort of dispatcher.
  • Logical – this form of cohesion is found in methods with a number of branching statements each performing a similar task based on a parameter or state. Again this is better converted into functional methods called from a logical method.
  • Coincidental – This is the worst form of cohesion…or, perhaps should be called anti-cohesion. This is a big blob of code with all kinds of branching and tasks performing work that has nothing in common.

Perhaps you can see from these descriptions why I have become such a fan of unit testing code. By exercising the discipline of unit testing I have found myself writing code that follows good patterns of cohesion due to the pragmatic requirements of testing. It’s next to impossible to test a method with all kinds of branching or different behaviors.

I think the cool thing is that Cohesion is a technique you can apply to just about any language. Some languages lend themselves to the technique better than others. Even good old SQL can benefit from the use of Cohesion.

That is all for today as I leap back into my student chair. Did I get it right? Were there forms of Cohesion I missed? Do you have applications of Cohesion you’d like to share? Drop me a note to share by sending an Email to btaylor@sswug.org.

Cheers,

Ben

SSWUG-TV
Catch the Field Reporter Interviews from SQL Server MVP Kevin Kline
Watch the Show

$$SWYNK$$

Featured White Paper(s)
Structuring the Unstructured: How to Dimensionalize Semi-Structured Business Data
Written by Interactive Edge

The Business Intelligence industry … (read more)