Confusion when reading large open source library codebases

Extensions can be used to structure code — but they are not particular well suited to do so.
You have to do everything by yourself, without support from neither standard tooling nor the language itself, and there is no way to guarantee that

  • the "protocol extension" only contains code which is relevant for the protocol
  • all code relevant for that protocol is contained in the extension

It is very easy to mix things up, and wrong structure does more harm then no structure at all.
Maybe in the future things will change, but until then, comments have some big advantages over extensions: They can't give you that false sense of security — and they have tooling support, as Xcode will show structuring comments in the method menu.

That was the driving motivation for adding fileprivate and adding stronger restrictions to private (SE-0025) — but I have sad news for you: SE-0169, which shredded that guarantee, turning it into another source for false sense of security.
The irony in this story is that SE-0169 was motivated by the "cult" of using conformances to structure code… therefor, I think it's valid to consider that ritual as harmful, as it is responsible for a big complication in the system of access rights.

Still, I don't say you should stop using "same-file-extensions" — I'm fine with that.
However, I wish all people would think more about pros and cons themselves instead of repeating claims from blogposts they read.
Above all, everyone should be free to decide that it's more reasonable to put their methods in the main declaration, without having to justify this preference (to be clear: I'm not blaming anyone here for suppression — but the cult is much bigger ;-).

1 Like