Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Cohesion in C#
Cohesion in C# refers to how closely related and focused the responsibilities within a class or module are. It measures the functional strength and unity of a module's elements. High cohesion means that a class has a single, well-defined purpose with all its methods working together toward that purpose.
The greater the cohesion, the better the program design becomes. High cohesion leads to more maintainable, reusable, and understandable code, while low cohesion results in classes that are difficult to maintain and test.
Types of Cohesion
Cohesion can be categorized from lowest to highest quality −
| Type | Description | Quality |
|---|---|---|
| Functional Cohesion | All elements work together for a single task | Highest (Best) |
| Sequential Cohesion | Output of one method becomes input to another | High |
| Communicational Cohesion | Methods work on the same data | Medium |
| Coincidental Cohesion | Methods grouped together without logical relationship | Lowest (Avoid) |
High Cohesion Example
The System.Math class is an excellent example of high cohesion. All its methods and constants are related to mathematical operations −
using System;
class MathOperations {
public static void Main() {
// All Math class members serve mathematical purposes
double number = -25.7;
double radius = 5.0;
Console.WriteLine("Absolute value: " + Math.Abs(number));
Console.WriteLine("PI constant: " + Math.PI);
Console.WriteLine("Power calculation: " + Math.Pow(2, 3));
Console.WriteLine("Circle area: " + (Math.PI * Math.Pow(radius, 2)));
}
}
The output of the above code is −
Absolute value: 25.7 PI constant: 3.14159265358979 Power calculation: 8 Circle area: 78.5398163397448
Low Cohesion Example (Poor Design)
Here's an example of a class with low cohesion that tries to do too many unrelated things −
using System;
// Bad example - Low cohesion
class UtilityClass {
public void SendEmail(string message) {
Console.WriteLine("Sending email: " + message);
}
public void PrintDocument(string content) {
Console.WriteLine("Printing: " + content);
}
public double CalculateInterest(double principal, double rate) {
return principal * rate / 100;
}
public void SaveToFile(string data) {
Console.WriteLine("Saving to file: " + data);
}
}
class Program {
public static void Main() {
UtilityClass util = new UtilityClass();
// This class does too many unrelated things
util.SendEmail("Hello World");
util.PrintDocument("Invoice");
Console.WriteLine("Interest: " + util.CalculateInterest(1000, 5));
util.SaveToFile("User data");
}
}
The output of the above code is −
Sending email: Hello World Printing: Invoice Interest: 50 Saving to file: User data
High Cohesion Refactored Example
The same functionality split into highly cohesive classes −
using System;
// High cohesion - Each class has a single responsibility
class EmailService {
public void SendEmail(string message) {
Console.WriteLine("Sending email: " + message);
}
}
class FinanceCalculator {
public double CalculateInterest(double principal, double rate) {
return principal * rate / 100;
}
}
class FileManager {
public void SaveToFile(string data) {
Console.WriteLine("Saving to file: " + data);
}
}
class Program {
public static void Main() {
EmailService email = new EmailService();
FinanceCalculator finance = new FinanceCalculator();
FileManager fileManager = new FileManager();
email.SendEmail("Hello World");
Console.WriteLine("Interest: " + finance.CalculateInterest(1000, 5));
fileManager.SaveToFile("User data");
}
}
The output of the above code is −
Sending email: Hello World Interest: 50 Saving to file: User data
Benefits of High Cohesion
-
Maintainability: Changes are localized to specific classes.
-
Reusability: Classes with single responsibilities can be reused in different contexts.
-
Testability: Focused classes are easier to unit test.
-
Readability: Code purpose is clear and easy to understand.
Conclusion
High cohesion in C# means creating classes where all methods and properties work together toward a single, well-defined purpose. This leads to more maintainable, reusable, and testable code. Always strive for functional cohesion where all elements of a class contribute to one specific task.
