The Ying And Yang Of Software Development

Emanuel Trandafir
CodeX
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.

Photo by Alex Padurariu on Unsplash

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…

--

--

Emanuel Trandafir
CodeX

Hi, I'm Emanuel Trandafir, a Java developer from Romania. I have a strong passion for clean code, software design, and unit testing.