Tuple labels for public APIs

When learning Swift, I didn't realise that we can access nested tuple values using the tuple.0 or tuple.1 syntax. I also really liked the array.first and array.last properties similarly and since these avoid the indexing offset by 1. So I thought it could be more readable/understandable to add tuple labels for a public API for beginners using the function. In case the internal API changes and the first tuple value no longer returns an element that a user expected (a particular type, for instance), the labels could be clearer as an abstraction.

For example:

public mutating func next() -> (element1: Base1.Element,
                                element2: Base2.Element)? {

I received feedback:

  1. Generic labels such as element1 and element2 don't really add any clarity over .0 and .1.

  2. These labels don't improve the users' understanding, because they don't convey any useful information. It's impossible for us to give the tuple elements descriptive names because at this level of abstraction we have nothing to go on. To quote the API Design Guidelines:

Omit needless words. Every word in a name should convey salient information at the use site.

Open to your thoughts and feedback. I can see that the labels don't add much in this instance but maybe they could be useful in the future.

Thanks in advance

Number 1 is wrong: words are almost always clearer than numbers. 2 is questionable, as it depends on more context. This seems to be some sort of compound collection, so the elements may have particular meaning that isn't visible here.

Plus, if the end user really cared, they can use the numeric accessors whether you provide labels or not.

I think @LemonCupcake means specifically labeling tuple elements element1, element2, etc. I would agree with the feedback given that these do not improve understanding and aren't clearer than .0, .1, etc.

1 Like
Terms of Service

Privacy Policy

Cookie Policy