I'm programming an implementation of incremental phi* in Java which is an algorithm designed to find any-angle paths in 3D space. The most important method in my api is LinkedHashSet<Point> planPath()
. Obviously there are cases where there are no possible paths. Everything works and I'm able to detect when a path doesn't exist, but I'm not sure how my API should relay this event to the user. I could either throw an exception, change the return type to Optional<LinkedHashSet<Point>>
, or return null
. I'm not sure what the best case is. Also if I were to throw an exception, should it be a checked exception?
Asked
Active
Viewed 57 times
1

Helios
- 859
- 9
- 16
-
I think this is primarily opinion based, but it may also be a duplicate of [Should a retrieval method return 'null' or throw an exception when it can't produce the return value?](http://stackoverflow.com/q/175532/1945631) – Andy Brown Aug 06 '15 at 07:06
1 Answers
0
Well, my general rule when creating a public API is to favor to return an empty collection rather than to return a null
value.
The reason is because, the collection class has the isEmpty()
function to check whether it has any value or not. and also, the Optional
class is only exists after java 1.8. So, I don't think it is a good idea to use that class.
because it is not an error, I don't think that throwing an exception can be meaningful here :).

kucing_terbang
- 4,991
- 2
- 22
- 28