[swift-evolution] Fwd: Propagating Optionals

Trans transfire at gmail.com
Fri Oct 7 16:56:41 CDT 2016


On Mon, Sep 26, 2016 at 4:08 AM, Haravikk <swift-evolution at haravikk.me> wrote:
>
>> On 25 Sep 2016, at 21:19, Trans via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> "john.residence.numberOfRooms" could just behave one way or the other
>
> While I understand where you're coming from, I think the problem is that whichever version we specified as a guess would be wrong some of the time anyway, which is why it's better to just force the developer to think about it and put the correct symbol(s) for their use case.
>
> For example, if we assumed a behaviour of !, then the user is going to get runtime errors that might not always occur (say the residence is nil 5% of the time, that's potentially only a 5% chance of triggering the error during testing). If we assume ? as the correct behaviour then the developer could end up with an error much further down their code depending upon when and how they use the result of the statement, and looking back at the code it won't be as obvious where that optional came from (especially if you meant to put an exclamation mark but didn't, so are assuming a non-optional value).

If it is already declared an optional so it is already to be expected.
Any ease of tracking down bugs this way is relatively minor -- akin to
the advantages of using Hungarian notation
(https://en.wikipedia.org/wiki/Hungarian_notation), and few suggest we
use that anymore. Plus, if you have more than one in the chain of
calls you are still going to have figure out which is the culprit. And
it gets worse, b/c what if you decide later that it should not be
optional (or vice-versa)? Then you have work your way though all your
code removing the `?`. THAT REALLY SUCKS.

> It's just one of those cases where I think it's better to force the developer to be explicit, rather than try to guess what they meant to do.

I think being explicit once is enough -- declaring the optional.
Having to be explicit on every use is just annoying. It would be like
having to declare the type on every use.


-- 
People with courage and character always seem sinister to the rest.
--Hermann Hesse

Trans <transfire at gmail.com>
7r4n5.com      http://7r4n5.com


More information about the swift-evolution mailing list