I am trying to convince others that the following first code snippet is bad practice and the second snippet is best practice.
Bad practice:
// There is only one implementation of Adapter
public function setAdapter(Adapter $adapter)
{
$this->adapter = $adapter;
}
public function persist(Entity $entity): void
{
if ($this->env->dryMode === true) {
return;
}
$this->adapter->persist($entity);
}
Best practice:
// Can inject different adapters, also something like a `DryModeDbAdapter`,
// that simply does nothing when `persist` is called.
public function setAdapter(AdapterInterface $adapter)
{
$this->adapter = $adapter;
}
public function persist(Entity $entity): void
{
$this->adapter->persist($entity);
}
I need references that back my thesis. Do you know any? Is there a specific keyword that refers to this programming situation? Like "implementation configuration agnostic" or something.