Search⌘ K

Temporal Coupling

Explore the concept of temporal coupling in PHP, where method calls depend sequentially on each other, reducing code clarity. Learn how applying immutability removes side effects and state mutations, enabling cleaner, more predictable functional programming practices.

What’s temporal coupling?

Temporal coupling deters code-readability. It occurs whenever a class’s design is such that two or more of its methods follow an invocation order that closely binds the result of a method call to that of the one that directly follows it. Class setters are enforcers of this anti-pattern.

Problem scenario

Take a close look at the snippet below.

PHP
<?php
$calculator = new PriceCalculator();
$calculator->updateCurrencyRates([
'usd' => 1.25,
'ugx' => 3.2
]);
$price = $calculator->computePrice([
'lapel pin' => 8,
'notebook' => 14
]);

The invocation order in the snippet ...