I very much disagree with most statements about usability/readability. Here is a chunk of some legit production code (a rewrite of UITableView):
for i in checkedIndices {
let prefHeight = heightSource(i)
let delta = prefHeight - verticals[i]!.height
if delta != 0 {
// Update the vertical
verticals[i]!.height = prefHeight
// Modify all rows below
if let nextIndex = i.next(in: dimensions) {
for k in IndexRange(from: nextIndex, dimensions: dimensions) {
// FIXME: This might accumulate floating point error, adjust for pixel density
verticals[k]!.y += delta
}
}
// ...
— with the suggested spelling, this will have to be rewritten into
for i in checkedIndices {
let prefHeight = heightSource(i)
let delta = prefHeight - unwrap(verticals[i], "Should be there").height
if delta != 0 {
// Update the vertical
var vertical = unwrap(verticals[i], "Should be there")
vertical.height = prefHeight
verticals[i] = vertical
// Modify all rows below
if let nextIndex = i.next(in: dimensions) {
for k in IndexRange(from: nextIndex, dimensions: dimensions) {
// FIXME: This might accumulate floating point error, adjust for pixel density
var vertical = unwrap(verticals[i], "Should be there")
vertical.y += delta
verticals[i] = vertical
}
}
// ...
— and giving that it's impossible to return inout
-like references, there is no way around copying and rewriting the (potentially large) structure that I need to modify in the dictionary.
Frankly, I really dislike the result. It also does not really at all make sense in this code to ever document the unwrapping, since it has already been made sure in my code that when this particular function runs, the assertion has been met — especially that all the unwrapping happens for the same key i
and the item never gets deleted — so, while a comment like "Should be there"
could be considered "lazy", there's simply nothing else to say. It is even much easier to see that verticals[i]
never gets deleted in the original spelling, because all assignments happen to a member, not verticals[i]
itself.
I hope that this piece serves as a strong example that there are legitimate cases where excessive spellings make the code quite obtuse and potentially even less performant.
More generally though, I personally don't quite understand why some particular topics (like force-unwrapping or unowned
references) receive such strong attention, while a vast majority of other cases, where assertions are exactly as critical (for instance, division by zero), do not have such recognition. If one would ban force-unwrapping and maybe unowned
references, then we either would have a very inconsistent language, where optionals are treated with much more piety than other constructs — or the whole dialogue should be held much more holistically, where one considers all possible trapping behaviour and each possible trap receives a personalized spelling.