DEV Community

Daniel Guglielmi
Daniel Guglielmi

Posted on • Updated on

High Cohesion, Low Coupling

Pieces
Photo by Jackson Simmer on Unsplash


High cohesion and low coupling are two important design principles in software engineering.

High cohesion refers to the degree to which the elements within a module or component of software are related to each other and work together to achieve a common purpose. In other words, it measures the degree to which the parts of a module or component are closely related and work towards a single goal. A module or component with high cohesion is easier to understand, maintain, and modify because the code is more self-contained and has fewer dependencies on other parts of the system.

Low coupling, on the other hand, refers to the degree to which modules or components are independent of each other and can function separately without relying on other parts of the system. It measures the degree to which a module or component depends on other modules or components. A system with low coupling is more modular, flexible, and easier to change or update, as changes made to one module or component do not affect the others.

In summary, high cohesion and low coupling are both design principles that aim to create code that is more maintainable, understandable, and flexible. High cohesion ensures that the parts of a module or component work together towards a common goal, while low coupling ensures that modules or components are independent and can be changed or updated without affecting other parts of the system.


High Cohesion and Low Coupling for React JS Developers

Here are some examples of how high cohesion and low coupling can be achieved in React.js:
High Cohesion

  • Keeping related states and behavior together in a component. For example, a component that displays a list of items should also contain the logic for rendering each item.
  • Breaking up large components into smaller ones that are focused on a specific task. For example, a complex form can be broken down into smaller components that handle individual fields or sections of the form.
  • Using a consistent naming convention for files and components that reflect their functionality. For example, a component that displays a header can be named Header.js, making it easy to locate and understand its purpose.

Low Coupling

  • Using props to pass data and behavior between components instead of relying on shared state. This allows components to be more independent and easier to test.
  • Using a state management library such as Redux to manage shared states between components. This reduces the dependencies between components and makes it easier to reason about the state of the application.
  • Using event-driven programming to handle communication between components. For example, a component can emit an event when a certain action is performed, and other components can listen to that event and update accordingly.

By following these practices, developers can create React applications that are more modular, maintainable, and scalable. This makes it easier to make changes to the codebase over time and reduces the risk of introducing bugs or unexpected behavior.


High Cohesion and Low coupling for Node JS Backend Developers

Here are some examples of how high cohesion and low coupling can be achieved in Node.js:
High Cohesion

  • Grouping related functionality into separate modules or files. For example, a module that handles database access should contain only functions related to database operations.
  • Using consistent naming conventions for modules and functions that reflect their purpose. For example, a function that generates a random string can be named generateRandomString.
  • Separating business logic from implementation details. For example, a function that processes payment information should not be tightly coupled to a specific payment processing library.

Low Coupling

  • Using dependency injection to provide modules with the resources they need to function, rather than requiring them to access those resources directly. This reduces the dependencies between modules and makes it easier to test them in isolation.
  • Using interfaces to define the contract between modules. This allows modules to be replaced or modified without affecting the rest of the system, as long as they adhere to the same interface.
  • Using event-driven programming to handle communication between modules. For example, a module can emit an event when a certain action is performed, and other modules can listen to that event and update accordingly.

By following these practices, developers can create Node.js applications that are modular, maintainable, and scalable. This makes it easier to make changes to the codebase over time and reduces the risk of introducing bugs or unexpected behavior.

Top comments (0)