Coincidental Cohesion

by KodefuGuru 5. October 2009 18:19

In response to my post, Strive for Functional Cohesion, VisualKnight wrote the following:

We noramally [sic] most functional methods to the domain object class in this case your Address class. However, if the method is more general in nature I would add it to an Utility class so other team developers can find it easier. We have an Utility class that is available in both the web and app side.

Let me start off by saying that I am guilty of using the utility class at least as far back as when I programmed in Delphi. The reason I did so was quite simple: I came from a Turbo Pascal background, and many of my development practices had evolved from procedural programming. As I learned object oriented patterns and practices, the utility class was lost from my tool belt as a relic from ages past. However, I am glad VisualKnight brings it up, because the utility class is an excellent example of coincidental cohesion!

Coincidental cohesion is the lowest form of cohesion found in object oriented software development. Methods within a class that has coincidental cohesion will appear to have been placed there randomly or without consideration of the proper concern of the method. This type of class necessarily leads to tight coupling as its varied functionality is used in a myriad of places.

A utility class is a perfect example of coincidental cohesion. Do not confuse this class with the so-called “utility pattern” (which is just a static class in c#). It is true that static classes are often called helper classes, and those are also often called utility classes, but this is just a creational pattern. Many static classes have higher orders of cohesion than coincidental.

For a quick example of coincidental cohesion, let’s take a look at the C# example of the Utility pattern on Wikipedia.

// The utility class
public static class Utils
{
    /// <summary>
    /// A utility method that helps out with error logging.
    /// </summary>
    public static void LogError(String msg)
    {
        MyLogger logger = new MyLogger();  // Create instance of logger class
        logger.LogIt(msg);
    }

    /// <summary>
    /// A utility method that displays a message to the user.
    /// </summary>
    public static void ShowMsg(String msg)
    {
        MessageBox.Show(msg);
    }
}

This static class only contains two methods, but they have no relationship to each other. Now, any class that wants to log errors will need to use the Utils class, and any class that needs to show messages will need to use the Utils class. The Utils class does not have a clear concern defined within the system; it is a garbage heap on which to throw methods.

If you have a need to use static methods, you should at the very least group them logically. But I do stand by my principle of Strive for Function Cohesion. There should never be a Utility class which is used as a general bucket. Every method in your system means something, it belongs somewhere. If you’re having trouble deciding where it belongs, it may need to be broken down to operate on smaller components within the system. The one caveat to that is that orchestration may operate on multiple objects, but they generally belong in workflow anyway.

If you disagree and want to make a case for the Utility class, leave a comment. Be sure to back it up with a sample method and ask me how I would organize it.

Tags: , ,

Kodefu

Comments

10/5/2009 6:23:53 PM #

trackback

Trackback from DotNetKicks.com

Coincidental Cohesion

DotNetKicks.com

10/5/2009 8:34:41 PM #

trackback

Trackback from DotNetBurner - C#

Coincidental Cohesion

DotNetBurner - C#

10/5/2009 10:18:50 PM #

trackback

Trackback from Pinderkent: Pain and glory from the trenches of the IT world.

"Utility" or "helper" classes are a sign of a language defect.

Pinderkent: Pain and glory from the trenches of the IT world.

10/6/2009 12:59:19 AM #

Justin James

I've been struggling with this myself over the last few years. I have gotten much, much better about it. In Ruby (which I've never used, but I've studied it fairly in depth), this is elegant, just extend the base language to add the function you want to emulate that prodedural style. But in a language which is less friendly to the hybrid model, such as C#... well... it's tough. Here's a good example:

Let's say that there is a process which needs to be performed by many parts of the application, on the same data type, but the data type does not always represent the same thing. For the moment, let's say that we have the following function:

private string PrepareText(string TextToPrepare)
{
    string preparedText = TextToPrepare;
    const RegexOptions regexOptions = RegexOptions.Singleline | RegexOptions.Compiled;
    var punctuation = new Regex(@"[\p{P}]", regexOptions);
    preparedText = punctuation.Replace(preparedText, " ");
    var whitespaceRemover = new Regex(@"\s+", regexOptions);
    preparedText = whitespaceRemover.Replace(preparedText, " ");
    return preparedText;
}
}

Incidentally, that is from an actual application I'm working on, so it's a good example (although in this application, there is no need for it to be in a utility class). All the same, you can see how many aspects of an application might want to use this, without necessarily having to make a whole new class around a data type which only adds this functionality.

That being said, that essentially means that it is a perfect candidate to become an *extension method*. Which is exactly why I stopped using utility methods! I had certain utility classes which got dropped into a lot of projects; things like a shortcut to Regex replace. With extension methods, I no longer have a need to make a utility class with static utility methods, I now have a utility class with my standard extension methods. Smile

J.Ja

Justin James United States

10/6/2009 3:22:35 PM #

chris

Utility classes don't necessarily have coincidental cohesion, only a Utility class that acts as a miscellaneous bucket. I will describe more types of cohesion in upcoming articles, and even explain how you can achieve functional cohesion with utility classes using extension methods.

chris United States

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Whois KodefuGuru

Chris Eargle

Chris Eargle
.NET Community Champion

LinkedIn Twitter Technorati Facebook

MVP - Visual C#

 

INETA Community Champions
Friend of RedGate
Telerik .NET Ninja
Community blogs & blog posts

I am a #52er


World Map

RecentComments

Comment RSS

Tag cloud

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010