CodePark/ questions / DRY, KISS, YAGNI & coupling
START PRACTICINGPRACTICE
Backend question · Software design & patterns › DRY, KISS, YAGNI & coupling

Explain coupling and cohesion. What combination are you aiming for, and how do you spot the opposite?

Coupling is how much one module depends on another. Tight coupling means a change in one module forces changes in the other. For example, a service reads another module's database tables or its private fields.

Cohesion is how closely the contents of one module belong together. A billing module where everything is about invoices and payments has high cohesion. A utils file with date helpers, SQL builders and email templates has low cohesion.

Aim for low coupling and high cohesion. Each module does one clear job. Modules talk to each other through small, stable interfaces.

Warning signs of the opposite:

  • One feature needs edits in many modules.
  • Modules import each other in a circle (circular imports).
  • Modules reach into each other's internals.
  • Catch-all modules have names like common, helpers or manager.
  • Coupling is how much one module depends on other modules.
  • Cohesion is how closely the jobs inside one module are related.
  • The goal is low coupling and high cohesion.
  • Warning signs are edits spread over many files, circular imports, shared internals and catch-all utils modules.