Hi…
IMNHO, decoupling is just good.
Decoupling is an interesting one though, because it often clashes head on with duplication:
// personel.php
function raise_pay($staff, $percent) {
foreach ($staff as $person) {
$person->pay *= (100 + $percent)/100;
}
}
// products.php
function special_offer($price, $reduction) {
return $price * (100 - $reduction)/100;
}
Aha! Nasty smelling code. Yuk. I’ll hold my nose long enough to refactor…
// personel.php
function raise_pay($staff, $percent) {
foreach ($staff as $person) {
$person->pay = increase_by_percent($percent, $person->pay);
}
}
// products.php
function special_offer($price, $reduction) {
return increase_by_percent(-$reduction, $price);
}
// percentages.php
function increase_by_percent($percent, $value) {
return $value * (100 + $percent)/100;
}
So before we had two independent modules that could be shipped separately. Now we have three modules and we have to ship or reuse them as two groups.
Are pay rates and product offers really related?
A couple of weeks later, once we’ve written a whole bunch of deployment scripts, someone points out that special offers are going to be a fixed amount off :(.
If in doubt, decouple. Duplicated code is irritating and boring, but at least it’s only irritating and boring. We are bored because we understand the code. We understand that we must repeat the changes and that’s annoying. After a couple of times it’s a no brainer to refactor. A good days work.
Compare that with code that’s coupled all over the place. Even getting to the point of understanding it is a serious problem. Often you cannot make any change for fear of the consequencies. Oh, if only I was only bored.
Decoupling trumps duplication. Praise be to decoupling!
yours, Marcus