The Ying And Yang Of Software Development
Published in
5 min readDec 20, 2022
Let’s discuss coupling and cohesion, and how to use the Single Responsibility Principle to get the right balance.
Coupling
Coupling represents the degree of interdependence between software components. In simpler terms, if a change in a component directly affects another component, they are coupled.
For instance, in the code snippet below, the EmalService class directly depends on the CustomerService:
@RequiredArgsConstructor
public class EmailService {
private static final String orderPlacedTemplate = """
Dear {},
Your order was successfully placed.
It will be delivered your address ({}), in 3 working days.
Thank you,
Team Dhl.
""";
private final CustomerService customerService;
public void sendOrderPlacedEmail(String username) {
Customer customer = customerService.findByUsername(username);
String emailBody = MessageFormat.format(orderPlacedTemplate,
customer.getFullname(),
customer.getAddress().getAddressLineOne());
sendEmail(customer.getEmail(), emailBody);
}
private void sendEmail(Email email, String body) {
// ....
}
}
Consequently, if the CustomerService changes, the EmailService might be…