Low Coupling

Principle

Ensure that classes are loosely coupled to one another.

Object-Oriented designers use the word coupling to describe the dependency between classes. When a class collaborates with another class, there is some sort of dependency between them and therefore they are coupled with each other.

Loose Coupling

Classes should not interact (collaborate) with too many other classes. Moreover, if a class A interacts with another class B, this interaction should be loose, which means that A should not know too much (or make too many assumptions) about B.

Why does this matter?

  • Applications that are easy to change consist of classes that are loosely coupled.
  • A class that is tightly coupled to other classes, may need to change if those classes change.
  • In a system that is strongly coupled, when a class changes, there's a greater risk of breaking several other classes (that depend on the changed one).

How to figure out if the coupling is high?

  • When you look at the UML Class diagram of your design, every "relationship" is an indication of coupling. The more relationships you have (and the stronger the relationships are)1, the higher the coupling.

  • Sometimes, coupling does not manifest itself in a UML diagram. This is when, for instance, a class depends on the inner working of another class. This is the strongest (and nastiest) form of coupling.

For example, consider the implementation of getScore inside the Student class:

public class Student {
    private String name;
    private String email;
    private GradeBook grades;


    public double getScore() {
        double quiz = grades.quiz();

        double project = 0;
        for (Double iteration: grades.project()) {
            project += iteration;
        }

        double homework = 0;
        for (Double grade: grades.homework()) {
            homework += grade;
        }

        return 0.1 * quiz + 0.3 * homework + 0.6 * project;
    }
}

The getScore method knows a lot about the GradeBook class! If we change anything in the GradeBook class, this method may break down. The Student and the GradeBook are tightly coupled. To reduce the coupling, you can let the GradeBook class calculate the score:

public class Student {
    private String name;
    private String email;
    private GradeBook grades;

    public double getScore() {
        return grades.totalScore();
    }
}

Now the Student class does not need to know anything about what is in the GradeBook class other than it has a totalScore method that it can call to get the student's score!

Take home message

You should strive to minimize coupling in your design.


1 Association, for instance, is a stronger indication of coupling than a dependency relationship.