It depends whether arguments that are out of range may occur in a regular way or if such method calls are considered being abusive.
If such arguments are regular, then return null
(that's perfectly okay).
If such arguments are abusive, then throw an exception.
An exception should always catch an error and never be misused for a test.
If the purpose of the method is not only to return a sprite, but also to test whether a sprite is available for a set of parameters, then you could use the same pattern as with Dictionary<T>.TryGetValue
:
public bool TryGetSprite(int x, int y, out Sprite sprite)
{
...
}
Use it like this:
Sprite sprite;
if (obj.TryGetSprite(x, y, out sprite) {
// Sprite available
} else {
// No sprite available
}
If the result to be returned can be a non-nullable type and a magic number like 0
, -1
or Int32.MinValue
is not obvious or not available then this a good option. Even null
might be a regular result under certain circumstances; for instance, a dictionary is allowed to contain null
values. If you need this distinction between contains null and not found, the TryGet... approach is perfect.
The advantage over returning null
is that you don't need additional knowledge apart from the method's signature in order to know how it works. Otherwise you are not sure whether an exception will be thrown or whether null
or even an empty Sprite
will be returned without reading the documentation. The disadvantage of TryGetSomething
is that is looks somewhat cumbersome.
GetSprite
method used? And what is it used for? – Simon André Forsberg 2 days agoPoint
a value type? You'd have to return aNullable<Point>
for a null return value to work.. – Mat's Mug 2 days agoGetSprite
doesn't return a "Sprite", but we'd need more context to recommend better alternatives. – Mat's Mug 2 days ago