Aristotele's avatar

Code design

Hi guys,

I have a method pause() and a method isPausable(). From a design perspective is better to:

  1. include isPausable() inside pause() as guard and maybe throw an exception like UnpausableException
  2. leave the 2 methods separated and each time wanna call pause() first check if pass isPausable(); in this case if check fails, isPausable() will return always a boolean (an exception will be good?)

Asking also from a testing point of view. @martinbean @bobbybouwmann

Thanks to anyone will answer

0 likes
2 replies
martinbean's avatar
Level 80

@aristotele If an object can’t be paused then yes, you want to throw an exception when trying to call pause() on that object. If you want to re-use an existing isPausable() method to check this, then that would be fine:

public function pause()
{
    if (! $this->isPausable()) {
        throw new NotPausableException();
    }

    // Mark object as paused here...
}

You don’t want to push that check to the caller, because if they forget to first call isPausable() then that’s going against your business logic and leave your application in a state you don‘t want it in (an object has been paused when it shouldn’t have been possible to).

1 like

Please or to participate in this conversation.