Skip to content

UML State Diagrams / #UML Sequence Diagrams

Estimated time to read: 3 minutes

When programming, keeping modules simple is critical. If the design exceeds what developers can handle mentally, code issues may start to appear more often.

To assist with this, it is possible to evaluate the design complexity.

Since design complexity refers to both classes and methods within them, the term Module in this note will refer to any sort of programming unit such as these.

Evaluating Design Complexity

There are two terms used to evaluate design complexity, these are Coupling and Cohesion.

Coupling focuses on the complexity between modules, whilst Cohesion focuses on complexity within a module. Screenshot 2022-07-14 at 14.12.53.png

System Design

When you design your system, you combine various modules together. Think of a bad design like puzzle pieces, where your modules are the pieces. You can only connect a puzzle piece to another specific puzzle piece and nothing else.

On the other hand, let's think of a well-designed system like Lego blocks. You can connect any two Lego blocks without much trouble, and all Lego blocks are compatible with one another. When designing your system, you want to make it like Lego. That way, you can easily connect and reuse modules together.

Coupling

Coupling captures the complexity of connecting a module to other modules. When evaluating a design, it is preferential for your modules to be loosely coupled.

If a module is highly reliant on other modules, this is known being tightly coupled. Like puzzle pieces. Conversely, if a module finds it easy to connect to other modules, this is known as being loosely coupled. Like Lego.

Evaluating Coupling

Degree, ease, and flexibility need to be evaluated when evaluating at modules.

Degree

Degree is the number of connections between the module and others. With coupling, you want to keep the degree small.

For instance, if the module needed to connect to other modules through a few parameters or narrow interfaces, then degree would be small and coupling would be loose.

Ease

Ease is how obvious are the connections between the module and others. With coupling, you want the connections to be easy to make without needing to understand the implementations of the other modules

Flexibility

Flexibility is how interchangeable the other modules are for this module. With coupling, you want the other modules easily replaceable for something better in the future.

Cohesion

Whilst Coupling only looks at the complexity between modules, Cohesion is used to look within a module. Cohesion represents the clarity of the responsibilities of a module. Within programming, modules should have High Cohesion

High Cohesion

If a module performs one task, or has a clear purpose, it would be defined as having 'High Cohesion'.

Low Cohesion

If a module encapsulates more than one task, or has an unclear purpose, it would be defined as having 'Low Cohesion'.

Example

Low Cohesion, Tightly Coupled

public void get (int controlFlag){
 switch (controlFlag){
  case 0:
   return this.humidity;
   break;
  case 1:
   return this.temperature;
   break;
  default:
   throw new UnknownControlFlagException();
 }
}

The class in the example above has low cohesion, since it has two purposes and therefore unclear. It also is also tightly coupling, since its get method is not straightforward. Exploration would have to be undertaken by the user to see what the method does depending on the attributes passed.