Cohesion – the action or fact of forming a united whole.

Cohesion in the object-oriented programming tells you whether the members (states and behavior) of the class works towards a common goal or fulfills the central purpose of the class.

See the class shown below.


class Program
{
 
public string GetData(string fileName)
 {
    // Read file content
 }

 public void PersistDocument(string documentText, string targetFileName)
 {
    // persist document into given file
 }

 public Document ParseInput(string input)
 {

    // implementation goes here.

 }

 public void View(BankAccountStatement accountStatement)

 {

    // implementation goes here.

 }

 public void DownloadAsPdf(BankAccountStatement accountStatement)

 {

    // implementation goes here.

 }

 public void DownloadAsCSV(BankAccountStatement accountStatement)

 {

    // implementation goes here.

 }

Now, look at the methods in the Program class. They are clearly doing the unrelated job. I did not see any connection between the file read, file writes, parse Input and view account statement, download bank account statement as CSV or pdf. Well, I did not see the class has a central purpose.

The program class is violating the single responsibility principle. The program class looks like a utility class that contains miscellaneous (unrelated) methods. It’s like a silver bullet for every problem.

These methods should be reorganized into more-focused classes and should work towards the central purpose and should extract them to classes that should do

  • File read and write.
  • Parse document
  • View and download bank account statement.

Hence we need to create separate classes so that they work towards one and only one purpose. The program class can be divided as shown below.


class FileDocumentStorage
{
  public string GetData(string fileName)
  {
     // implementation goes here.
  }

  public void PersistDocument(string documentText, string targetFileName)
  {
     // implementation goes here.
  }
}

The FileDocumentStorage class reads data from given file and stores the document-text into the target file. It has only one purpose (single responsibility) that is file manipulation. The methods are more related to each other.


class InputParser
{
  public Document ParseInput(string input)
  {
    // implementation goes here.
  }
}

Similarly, class InputParser parses the input string and converts it into the document object.

class BankAccountStatementForm
{
 public void View(BankAccountStatement accountStatement)
 {
    // implementation goes here.
 }

 public void DownloadAsPdf(BankAccountStatement accountStatement)
 {
    // implementation goes here.
 }

 public void DownloadAsCSV(BankAccountStatement accountStatement)
 {
    // implementation goes here.
 }
}

Lastly, BankAccountStatementForm class views the bank account statement, downloads the bank account statement as PDF or CSV. It fulfills the central purpose of Viewing the Bank account statements.

Now the refactored classes` methods are working towards the central goal and look related.

You can see your brain can easily remember or understand everything the code does.

High/Strong cohesion is always preferred it reduces the complexity of the code by designing easy to understandable classes that obey the single responsibility principle.

Eventually,

Cohesion means that the state and behavior of the class should support a central purpose.

Dattatraya Kale

Aspiring agile software craftsman, clean code, polyglot, in love with different programming paradigm. I am on a never-ending journey towards mastery of software.

2 thoughts to “Cohesion – the action or fact of forming a united whole.”

Leave a Reply to Nikeetaa ChaudriCancel reply