I have a public method which searches for an image and if the image exists, it returns it as a byte array.
What the method should return, if the image doesn't exist? (the image not existing is not an exception, but expected possibility)
Should I return an empty byte[]
array, or should it return byte?[]
set to null instead?
Which one is more in tune with conventions?
Returning null is often a violation of the fail fast programming principle. The null can appear due to some issue in the application. The issue can even go to production if the developer has not implemented proper exception handling, which can help quickly detect the issue.
Collections. emptyList() : method used to return an empty list. Collections. emptySet() : method used to return an empty set.
An empty collection isn't the same as null . An empty collection is actually a collection, but there aren't any elements in it yet. null means no collection exists at all.
To handle empty collections, use the DefaultIfEmpty() method in C#. If an array is empty, then using this method will show the default method instead of displaying an error.
I would return null
(just as a byte[]
- all arrays are reference types). Returning an empty array will lead the caller to think they can try to load that data as an image - which they can't. Returning a null
reference makes it very easy to differentiate between "present and empty" vs "not present at all".
In other cases where the image being missing is an indication of a significant system problem, I'd consider an exception though. It sounds like that's not the case here.
Note that this is significantly different to a normal method returning a collection of individual elements - you don't care about each byte as a separate entity in the way that you would for a method returning List<Person>
for example; here the byte[]
is effectively a blob. I would regard it as a single chunk of data which happens to be represented as an array rather than a collection. To put it another way: think about it as if your method were declared to return a Blob
type instead of an array... would you return a null reference, or an instance of Blob
with no data?
Neither alternative is good for situations when something does not exist: returning an empty array of bytes is wrong, because the users cannot distinguish between finding an image that is actually empty, and not finding an image. Returning null
is slightly better, but the users may forget to null
-check the return.
Two alternatives that work better are returning a not found result as a bool
while setting the result in an out
parameter, and throwing an exception. Both approaches make the "image not found" condition explicit to the users of your API, making it harder to miss.
bool TryLookUpImage(string name, out byte[] image) {
...
}
...
byte[] image;
if (TryLookUpImage(imageName, out image)) {
// Display image
} else {
// Display error
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With