Reputation: 6197
class Operation
{
private $op;
public function __construct()
{
$this->op = [];
for ($i = 1; $i < 1000; $i++)
{
$this->op[] = rand(1,9999);
}
}
public function getOps()
{
return $this->op;
}
}
class TestTellDontAsk
{
public function doItBAD(Operation $op)
{
foreach($op->getOps() as $item)
{
echo $item.'; ';
}
}
public function doItGOOD(array $ops)
{
foreach($ops as $item)
{
echo $item.'; ';
}
}
}
$m = [
memory_get_peak_usage(true),
memory_get_peak_usage(false),
memory_get_peak_usage(true),
memory_get_peak_usage(false)
];
$op = new Operation();
switch(mt_rand(0,1))
{
case 0: (new TestTellDontAsk())->doItBAD($op); break;
case 1: (new TestTellDontAsk())->doItGOOD($op->getOps()); break;
}
echo '<hr>';
echo memory_get_peak_usage(true) - $m[0].'<br>';
echo memory_get_peak_usage(false) - $m[1].'<br>';
echo memory_get_peak_usage(true) - $m[2].'<br>';
echo memory_get_peak_usage(false) - $m[3].'<br>';
this demostrates the whole in BAD and GOOD usage.
The doItBAD()
is bad, because passing an object is an unnecessary knowledge for the function but MEMORY-GOOD, since it just passes a reference, not the whole array itself
The doItGOOD()
is good, because it only passes an array but MEMORY-BAD, since it just passes a all the data instead of a reference.
Now how to decide which one to use?
Upvotes: 0
Views: 44
Reputation: 14903
This is a much wider question than simply "tell don't ask". Here you have two concepts. Its very common to trade off one against the other.
Both concepts are subjective. What constitutes good code is argued about endlessly. "Efficient" software has multiple different meanings depending on context (eg: efficient on CPU, or efficient on memory).
Therefore any answer to this question WILL BE SUBJECTIVE AND OPEN TO DEBATE. Some will say this is not a good stack overflow question.
When to write "less good" code for the sake of efficiency?
This is experience.
If you don't have the experience then here's some tips of what to think about:
Rule of thumb
BAD: If you have no performance requirement then never worry about performance. I've spent too many years fixing those people's code. It's a false economy.
BETTER: If you have no specific reason to worry about performance then don't... But test performance as you go. That way your code might actually work when it goes into production.
More Clearly: Go for good design at the expense of performance. Only push for performance when you need to.
Upvotes: 1