I've now ported quite a few thousand lines of C and Objective-C to Swift, enough to detect
patterns. I've tried out coding conventions and so on. I've rejected many and kept a few.
One issue has come up frequently enough to bang me on the head the threshold number
of times to make itself known.
I have gotten very used to optionals now, as they really become a major part of your coding
life with Swift. And many of the methods I have written return optionals to good effect.
But what about methods that should return arrays of objects, and when called, a particular
execution has no objects to return in the array. In this case the programmer has the option to
return either an empty array or to return nil. Looking through my code I discover that I
am doing both with little discrimination as to which is the better (or at least consistent)
approach.
But I like consistency. So I've been going back through and converting all my:
func method (...) -> [SomeType]
methods into
func method (...) -> [SomeType]?
methods. Of course I could go the other way. One criteria I like to use is to choose the
approach the minimizes the number of lines of code that need to be written. It is still
not clear to me which solution meets this criteria better. After a few more thousand lines
of code that may be clearer.
Anyone else come across this issue and willing to share thoughts?
Tom in Massachusetts