Cohesion
Definition: Cohesion is the degree to which the elements of a module or class are closely related and focused on a single purpose. High cohesion is preferred because it makes code easier to understand, maintain, and reuse.
Example: A Payment class that contains only payment-related operations such as pay(), refund(), and calculateAmount() has high cohesion.
Coupling
Definition: Coupling is the degree of dependency between different modules or classes. Low coupling is preferred because changes in one module have less effect on other modules.
Example: If a Customer class can work independently without depending on the internal details of a Payment class, they have low coupling.


0 Comments