Hacker News new | past | comments | ask | show | jobs | submit login

What would you suggest instead of an empty array for a 0 item return?



The person you replied to doesn't like returning "null". He/she would probably prefer returning an array that contains 0 items.


He called it a "null array" which I interpret as an array containing zero items. If it was just a plain old null then yeah, that's super annoying.


This makes sense now. If I did a GET to posts and got a "null", I would think I made some kind of error. If I get an empty array, I would (correctly) assume there are no posts yet.

Basically, GET to posts would always return an array of posts. Whether there are 0 (empty array), 1 (just one object in the array), or many (array with n length). This makes API logic way easier to handle without having to check what structure I received even with a 200 status code


Also makes a lot of iteration easier... just a for loop over the array to dump something on the page with no edge cases.

You can add a warning by just checking the length against that array blindly.


If you return an empty array, I can always process the result by looping through the array.

If it's sometimes an array and sometimes null, I need branched logic for each call.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: