I have this calculator class that performs business calculations distributed among small methods. I'm split between choosing to save the state, or just calculate everytime a method is called. Am I doing unnecessary micro-optimization or not?
class Bill
{
private $value1;
private $rate1;
private $value2;
private $rate2;
// ...code here
function one ()
{
return $this->value1 * $this->rate1;
}
function two ()
{
return $this->value2 * $this->rate2;
}
funciton oneAndTwo ()
{
return $this->one() + $this->two();
}
function modifyOneAndTwo ()
{
return $this->oneAndTwo() * 2;
}
// ...more code here
function save ()
{
$one = $this->one();
$two = $this->two();
$oneAndTwo = $this->oneAndTwo();
$modifiedOneAndTwo = $this->modifyOneAndTwo();
//save all values above to database
}
}
Notice that everytime I call save() it would eventually call one() and two() thrice, and oneAndTwo() twice. This would be my first "real" application for a real business, and so I'm clueless when it comes to optimization. I've read some articles, but I didn't find any pertaining to these kind of scenarios.