Cohesion
Leandro Siqueira
AI Evaluation Specialist | Multimodal Data Annotator | Software Development Expert | Empowering Professionals in Technical & Soft Skills
When diving into object-oriented programming (OOP), you’ve likely encountered the term "cohesion" at some point. And for good reason — it's all about how well the parts of a class or module relate to each other and collaborate to fulfill a single, clear purpose.
In today’s world of modern software development, cohesion is a big deal. It ensures your code isn’t a chaotic tangle of unrelated tasks and keeps your projects sane (and your teammates happy).
This article aims to underscore just how vital it is to write cohesive code, while nudging you, dear reader, to explore this topic further. Trust me, mastering cohesion might just delay the arrival of those premature gray hairs! ??
?? What is Cohesion?
In summary, cohesion measures how focused a class or module is on performing a specific task.
? When a class has a single, well-defined function and includes only the methods and fields necessary to accomplish that function, we call it highly cohesive.
? On the other hand, if a class handles multiple, unrelated responsibilities, it is said to have low cohesion.
Can you readily identify the purpose of the following class?
type
TDataProcessor = class sealed
public
function ReadFile(const AFileName: string): Boolean;
function AddValues(const AValue1, AValue2: Double): Double;
function GenerateReport(const AFileName: string): Boolean;
function ConvertToString(const AValue: Double): string;
procedure SaveDataToDatabase(const AData: string);
end;
And this one?
type
TCalculator = class sealed
public
function Add(const AValue1, AValue2 : Double) : Double;
function Subtract(const AValue1, AValue2 : Double) : Double;
function Multiply(const AValue1, AValue2 : Double) : Double;
function Divide(const AValue1, AValue2 : Double) : Double;
end;
Which of these do you think is highly cohesive? If you chose the second one, you're correct! ??
??The TCalculator class is highly cohesive because all its methods are focused on a single responsibility: performing mathematical calculations.
The TDataProcessor, however, has low cohesion because it handles multiple, unrelated tasks such as reading files, adding values, generating reports, saving data to a database, and converting values to strings.
Note: Cohesion closely aligns with the Single Responsibility Principle (SRP), the "S" in the famous SOLID principles. By adhering to SRP, you can design robust classes with a singular reason to change. Want to dig deeper? Check this out for more details!
So, what do our systems gain from this? Let’s take a look at some benefits.
领英推荐
??Readability
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
This quote by Martin Fowler emphasizes a key principle of software development: writing code that not only works for the machine but is also easy for humans to understand.
Well-organized, well-written, and cohesive code significantly improves its readability, making it easier for the programmer who wrote it and for anyone who needs to work with it in the future.
??Easier to Maintain
Cohesive code, with a clear and focused purpose, simplifies long-term maintenance. When code is neatly organized into well-defined classes and functions, it becomes easier to fix bugs, add new features, or refactor without unintentionally introducing errors.
A highly cohesive class or method will have fewer interdependencies, making modifications straightforward and less risky.
????Improved Collaboration
Clear and well-structured code fosters better collaboration among team members. Developers can work together seamlessly, knowing that their changes won’t inadvertently affect other parts of the system.
When code is cohesive, it ensures that each module or class serves a specific, well-defined function, avoiding unnecessary complexity and reducing the risk of conflicting changes. This trust in the system's structure leads to more efficient teamwork.
Gold Tips
Note: Use techniques like Class-Responsibility-Collaboration (CRC) cards to brainstorm the role of each class.
Conclusion
Cohesion is a cornerstone of good OOP. By ensuring that each class has a clear, focused responsibility, developers can create code that is easier to read, maintain, and extend.
By focusing on this critical aspect of design, you can build scalable and sustainable software systems.
#Delphi #DelphiDevelopers #DelphiTips #DelphiCode #OOP #Cohesion #SRP #CleanCode #Programming