Firstly it is not an exception since it is part of the normal flow of operations. I tried to return a null object but this was not allowed. I can get by returning a Point with negative coordinates but it feels like a hack. For example if I return (-1, -1) then instead of checking for a negative number, conceivably some use of my library might directly check for equality with -1 which will then break if my internal implementation changes
Share
You could return a nullable Point by changing your return type to
Point?. That way you can also returnnull.Otherwise you can create a static Point instance somewhere with (−1, −1) and always compare against that.
PointUtils.InvalidPointis probably nicer to read thanp.x == -1 && p.y == -1.