I know there are countless ways to implement either adding or subtracting based on a flag and I have listed three of them. My last example is the most concise, however, I was surprised I didn't come up with a more semantic approach. If "best" is defined by being the most concise, intuitive and readable, what is the best approach to do this?
public function fromDaySeconds(bool $add=true):int
{
if ($add) {
return $this->getTimestamp() + $this->getDay()->getTimestamp();
}
else {
return $this->getTimestamp() - $this->getDay()->getTimestamp();
}
}
public function fromDaySeconds(bool $add=true):int
{
return $add
?$this->getTimestamp() + $this->getDay()->getTimestamp()
:$this->getTimestamp() - $this->getDay()->getTimestamp();
}
public function fromDaySeconds(bool $add=true):int
{
return $this->getTimestamp() + $this->getDay()->getTimestamp()*($add?1:-1);
}