I had this discussion with my senior once. It is about whether I should return false or throw an exception when we cannot find a user by id. I argued that we should throw an exception because I wanted to trust the method that it will return User objects only.
We settled with throwing exceptions. Tonight while thinking about it. I thought I should ask you guys what are your thoughts about this.
Let’s say I have this method
public function findUserById($ id)
What it does is fetch a user from a storage and return User
object.
What should I do if the method failed to find the user while assuming that the provided $ id
is a valid input and it successfully connected to and searched the storage?
Should I throw an Exception saying that the User is not found
or should I just return false/null
?
Also, for context, we are using PHP in this.