Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I'm making an API that will return data in JSON.

I also wanted on client side to make an utility class to call this API.

Something like :

JSONObject sendGetRequest(Url url);
JSONObject sendPostRequest(Url url, HashMap postData);

However sometimes the API send back array of object [{id:1},{id:2}]

I now got four choices ():

  • Make the method test for JSONArray or JSONObject and send back an Object that I will have to cast in the caller
  • Make a method that returns JSONObject and one for JSONArray (like sendGetRequestAndReturnAsJSONArray)
  • Make the server always send Arrays even for one element
  • Make the server always send Objects wrapping my Array

I going for the two last methods since I think it would be a good thing to force the API to send consistent type of data.

But what would be the best practice (if one exist).

Always send arrays? or always send objects?

share|improve this question
2  
recommended reading: Why is asking a question on “best practice” a bad thing? –  gnat Aug 20 '14 at 15:15
    
@gnat Yeah well I don't really agree with that. Crossing when traffic light are red would be a best practice. Non-zombie surely wouldn't live long. And recommending always the same abstract answer would be zombie-like too. Don't ask best practice, that's the best practice. –  Michael Laffargue Aug 20 '14 at 18:59
    
    
I was more on a "Don't make mistake others have already made, benefit from experience" approach rather than a "What should I do to follow the rules of Software Engineering Goddess (I prefer Goddess)" –  Michael Laffargue Aug 21 '14 at 7:24

1 Answer 1

up vote 2 down vote accepted

That should depend on what it is actually doing. If it is retrieving one or more objects, it makes logical sense to always return an array containing the results, even if it is an array with only one element. This makes it easy to then do a foreach on the returned array, for example.

If the format of the request causes it to return completely different kinds of data though, it would be clearer to have a different method for each context.

How this would work as a method is a good analogy. If you have some collection foo, and a function searchFoo that searches the collection for elements, you are not going to write it such that it returns an element if only one element matches, and otherwise it returns a list of elements. Either it always only finds one elements (like if the search is based on a unique key) in which case it also returns an object of the appropriate type, or it will find a variable number of elements in which case it will always return a list, regardless of how many elements were found.

share|improve this answer
    
Yeah, I think that's the way I'll be taking. I'm glad to have an opinion about my question. –  Michael Laffargue Aug 20 '14 at 19:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.