Writing self-documenting Swift code
swiftbysundell.com
swiftbysundell.com
Documentation needs to be describing the actions of the program on a different abstraction level/perspective than the program itself.
Something can be easy to read and to understand but it cant be self documenting unless it actually literally writes it own documentation it being a sentient AI or something like that.
No, if you can write 30k lines of any computer language, you can at least write a 2 pager in English/preferred human language describing the architecture and other details of the code. Ridiculous that people expect code to be documentation to the point of not writing a single sentence in human language about what the code does or how it works.
Should not be more than 200 lines. Because everything over 200 lines gets flagged as a warning </pedantic>
It's just writing clearer code, structured to make more things utterly obvious (to the human, but in many cases also to the compiler).
Code written like this will still need some comments/documentation, but it won't need as much because more things will be completely obvious.
Comments are low-level, documentation is high level.
"self-documenting" removes the need for comments, not documentation. Ironically.
There are various forms of documentation, from terse READMEs to formal specs. Comments in the code are just one of the many forms documentation can take (in some cases, the only one).
“Regardless of how you feel about writing (and reading) documentation - I think most of us agree that we should always try to make our code, and the APIs we design, as easy to understand as possible.”
It is absolutely true that code cannot be fully self-documenting, and the idea of writing self-documenting code isn’t to suggest that it can be. E.g., why things were done a certain way is not readily expressible through code. And it’s often helpful to give a plain-language overview of the purpose and responsibility of a class, for example.
In my experience, writing as “self-documenting” code as possible is vastly preferable to a culture which cares more about writing comments for everything than writing readable code. People end up writing obligatory but worthless comments; comments are strewn everywhere making it even harder to read or skim code; and you end up getting vastly more comment rot.
I'm going to steal that use of 'typealias' when defining a closure that uses @escaping. Those method definitions are always so messy looking, this really cleans it up.
primitive Dollar: Decimal // no mixing with euros
primitive Meter: Double // no confusion with miles
You can do the same in Swift with typealias _but_ it will happily accept you passing in Euro where Dollar is declared because they’re both Decimals. This doesn’t happen in languages like F#.Also in F# you get the type Meter/Hour if you divide Meters by Hours. Which multiplied by an Hour value gives you the distance traveled at that speed in that time.
There is also: https://swiftweekly.github.io
Also not too focused on Swift, so useful also for other languages.
Most important, though, you are structuring the code in a way that provides better actionable understanding of the intent not only to the humans that work on it, but also your intrepid robot buddies, the Swift compiler / static analyzer. Who happen to be extremely helpful allies in this particular language.
In Swift, I would recommend using dedicated types like AccessToken and RefreshToken, instead of plain strings, 100% of the time.