Alex Rivera | Logout

Should I return 'null' or an empty array?

Asked 2008-12-01T21:10:32.563
18

Suppose you have a method that should create and return an array of some sort. What if the array doesn't get populated. Do you return an empty array or null/nothing?

Edit
Report

2 Answers

0

Unless for some reason there is a differance in meaning in your code for a result being a null array verses a plain old blank one, I would return a blank one. This also might be a language specific thing. In some languages, you may only have one option.

answered 2008-12-01T21:18:36.353
0

I'd imagine this depends on the way that the language handles it.

In C, I pass pointers around and always include a pointer that is the size of the array. The calling function then has to provide two things: the array, and the size of the array. if the function doesn't populate the array, it just returns the same pointer back, and doesn't do anything to the array size. Usually i just re implement a vector type structure and work with that.

In perl, array size is handled implicitly, so i just return an empty array, or if an error, I modify the error code variable and return an undef.

In java, i use ArrayLists, and use the size property.

answered 2008-12-01T21:21:27.923

Your Answer