2

So, I have a question about how my class and struct interfaces should look like in Swift. For example I have a class or struct which has method:

func getAllObjectsFromSomewhere() -> [Int]

but, imagine that this method not always return you some array of Int, so there are few different ways how to handle it.

  • func getAllObjectsFromSomewhere() -> [Int]?
  • func getAllObjectsFromSomewhere() throws -> [Int] Throws custom ErrorType with types like: NotFound or EmptyArray
  • func getAllObjectsFromSomewhere() -> [Int] { /* some code and then just return an empty Int array */ return [Int]() }

So, my question is, when and why I should take which approach of organising the interface? Does it make much difference? Is there any guidelines?

2
  • Do you have a specific use case or more details about this method? Or is this purely hypothetical? Commented Sep 6, 2016 at 13:10
  • @RyanPoolos I faced this during building architecture for one of my apps, but I think I want the most common answer, so yes, this is purely hypothetical. Commented Sep 6, 2016 at 13:11

2 Answers 2

2

All three approaches are valid, depending on a situation:

  • When getting objects may or may not be possible, and impossibility does not necessarily mean an error, use [Int]?. This lets you differentiate between an empty array and no array at all.
  • When getting objects must always be possible, and impossibility to get them means you have an error, use throws
  • When inability to get objects should always be treated as an absence of objects, use [Int] and an empty array.
Sign up to request clarification or add additional context in comments.

4 Comments

That is what was in my mind, but, I am not quite sure about application. For example, I am trying to get a list of objects from some abstract database. Which one I should use? Does it depends on which level of architecture I want to get this?
Because, for example, if it is low level model, I want to deal with throws, if it is something close to view, like viewModel, I don't want to deal even with nil values.
And one more time for example, if I executed request to database, and I got 0 results, should I return empty array? or nil?
@PavelGatilov When DB request returns successfully with zero rows, this means that there are no rows, but they could be there. Hence, you should return an empty array. Returning nil or throwing an exception should be an option only when there is an error with the request. You should always throw if the request itself is invalid due to a programming error.
1

This is just my opinion and I don't have any guidelines or anything to throw behind it, but...

Straight out of the box I think this is a perfect case for optionals, meaning this signature:

func getAllObjectsFromSomewhere() -> [Int]?

But (as always :)) it depends on the situation. Here are my thoughts on your scenarios.

func getAllObjectsFromSomewhere() -> [Int]?

This is what I'd normally use if I was to implement that function.

You have a normal case where you expect to get some Ints returned in an array.

If for some reason there are no elements, then you get a nil in return which you can then check for using a guard or an if let.

Normally, an empty array should't be enough reason to actually start throwing exceptions, but it depends on the specific case of course.

func getAllObjectsFromSomewhere() throws -> [Int]

If however, you actually expect that your getAllObjectsFromSomewhere must return a value, then it would make sense to throw an error if for some reason there wasn't any content to return.

func getAllObjectsFromSomewhere() -> [Int]

This is the solution I like the least.

When you call your getAllObjectsFromSomewhere you still need to do a check and see if the returned array actually contains anything (if getAllObjectsFromSomewhere().count > 0) before you decide what to do....why not use guard or if let for that?

As said, this was just my thoughts on the three scenarios, hope you can use them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.