Coesione, accoppiamento e separazione delle responsabilità
Padroneggi tre proprietà fondamentali della progettazione software che determinano la manutenibilità del codice: alta coesione, basso accoppiamento e chiara separazione delle responsabilità.
Coesione, accoppiamento e separazione delle responsabilità è una lezione Clean Architecture & Design Patterns in Practice gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Clean Architecture & Design Patterns in Practice, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Clean Architecture & Design Patterns in Practice include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why These Properties Matter
Under clean code and SOLID lie three deeper design properties: cohesion, coupling, and separation of concerns. They explain why the rules work.
What is Cohesion
Cohesion measures how well a module’s responsibilities belong together. High cohesion = one well-defined job; low cohesion = a grab-bag of unrelated methods.
Low vs High Cohesion
Compare the two: the Util class mixes unrelated concerns, while UserRepository is tightly focused. That focus is high cohesion.
// Low cohesion
class Util {
saveUser() {}
sendEmail() {}
formatDate() {}
}
// High cohesion
class UserRepository {
save() {}
findById() {}
}What is Coupling
Coupling measures how much one module depends on another’s internals. Tight coupling spreads changes everywhere; loose coupling keeps interactions minimal.
Tight Coupling Example
Here OrderService creates its own SmtpMailer, locking it to that exact class. Swapping the mailer means editing the service — tight coupling.
class OrderService {
constructor() {
this.mailer = new SmtpMailer();
}
}Loosening the Coupling
Inject the mailer instead. Now OrderService depends on an abstraction and accepts any implementation. That’s loose coupling.
class OrderService {
constructor(mailer) {
this.mailer = mailer;
}
}The Sweet Spot
The sweet spot is high cohesion and low coupling together: focused modules that stay independent, so systems are easy to change, test, and reason about.
Separation of Concerns
Separation of concerns means splitting a program so each part owns one distinct concern — keep UI, business logic, and data access in separate layers.
Mixed Concerns
This function tangles data access, logic, and presentation together. Touch any one concern and you risk breaking the others — mixed concerns.
function showTotal(id) {
const rows = db.query('SELECT * FROM orders WHERE id=' + id);
const total = rows.reduce((s, r) => s + r.price, 0);
document.body.innerHTML = 'Total: ' + total;
}Separated Concerns
Split it into a repository, a calculator, and a view. Each concern now changes and tests independently. Much cleaner.
const order = repository.findById(id);
const total = calculator.total(order);
view.render(total);How They Connect to SOLID
These properties underpin SOLID: single responsibility drives cohesion, dependency inversion drives low coupling, and layering expresses separation of concerns.
Quick Check
High cohesion, low coupling — can you tell them apart?
Recap
You’ve got the foundations: cohesion keeps related work together, low coupling minimizes dependencies, and separation of concerns divides by purpose.
Domande Frequenti
La lezione «Coesione, accoppiamento e separazione delle responsabilità» è gratuita?
Sì — il testo completo di «Coesione, accoppiamento e separazione delle responsabilità» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Clean Architecture & Design Patterns in Practice, passa a CoddyKit PRO. Il corso Clean Architecture & Design Patterns in Practice include 4 lezioni in totale.
Cosa imparerò in «Coesione, accoppiamento e separazione delle responsabilità»?
Padroneggi tre proprietà fondamentali della progettazione software che determinano la manutenibilità del codice: alta coesione, basso accoppiamento e chiara separazione delle responsabilità. Eserciti Clean Architecture & Design Patterns in Practice con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Clean Architecture & Design Patterns in Practice?
Non è richiesta alcuna esperienza precedente. Clean Architecture & Design Patterns in Practice su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Coesione, accoppiamento e separazione delle responsabilità»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Clean Architecture & Design Patterns in Practice?
Sì. Ogni lezione Clean Architecture & Design Patterns in Practice include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Introduzione al Clean Code
- Panoramica dei principi SOLID
- Il valore di una buona progettazione
- Coesione, accoppiamento e separazione delle responsabilità