1

There are many situations where one solution could work better.

Case 1 A project has an id. If you search by id and can't find it then you could throw an exception.

Case 2 A project has a client that is optional(not assigned yet). You get the project then lazy load the client. Because it's optional a null should be returned instead of an Exception.

Case 3 A user searches for a project by name. The project might or might not exists. Is this an exceptional situation? You could say it's an exception because the person that makes the search should know what's he looking for. Or you could say you expect people to search for the wrong things and then you could return a null.

Anyway, there are 3 cases with different expected results. What's a good approach to solve this and still be consistent?

johnlemon
  • 20,761
  • 42
  • 119
  • 178

1 Answers1

0

It's a really interesting question you asked there. Even though I think the question is not really "answerable", I try to do so based on my personal opinion.

I would always pick returning null because a find method is there to find something (yeah, I am very funny) and when you try to find something in real life, you also just say "good, I didn't find that" instead of suiciding. Maybe that example isn't the best to be found but it explains my point of view pretty well. So returning null basically states that the find method didn't find anything matching the provided criteria. Exceptions are too 'hard' here in my opinion.

Niklas S.
  • 1,046
  • 10
  • 22