Code should not be self documenting because code only knows its function, not it's purpose.
Code should be written so its function is clear. Comments and documentation should make its purpose and existence clear.
Code should not be self documenting because code only knows its function, not it's purpose.
Code should be written so its function is clear. Comments and documentation should make its purpose and existence clear.
When I started out cutting code in 1978, I thought 'elegant' was important. Once I started working on other people's code I realised that 'clear' always trumps 'elegant'.
Nothing like watching five years of people maintaining your 'elegant' code with a hammer and a fist full of 'if' statement to clarify that you should have been more explicit.
True. I've mentioned here before, but one of my favourite ever comment blocks I found in 15yr old legacy code basically said
"If you don't understand this next block of code, *please* don't change it. It took a long time to get it working as it should!"
Of COURSE there weren't any comments helping said viewer understand the code?
My guess is that the original person thought 'anyone bright enough to understand this code, will understand the comment'. No idea - it was a while ago.
@bytebro @troublewithwords elegant is
[$a => $b] -> [$a <= $b]
Clear is
a if a > b else b
(sorry, @simoncozens)
@jyrgenn @troublewithwords @simoncozens @bytebro my brain reads that as (a if a) > (b else b)
Python3 is not clear. It’s emulating one of the less clear parts of Perl here.
The older and…or (if its pitfall doesn’t apply) is clearer. Or, if needs must, the Ansible-Jinja ternary filter.
Clearest is the C ternary operator with parenthesēs if a term is more than a word.
I know nothing of Python even though I have the allegedly definitive reference book right here on my shelf next to my copy of _Programming Perl_! 🙂
Perl is 'clear', within its limits. I may be wrong but I think even Larry Wall might have said as much.
If you absolutely have to prioritise 'safety' over clarity, then yeah, you probably ought to be writing Rust or something, but that's not my background at all.
Absolutely. Or the old one about zeroing a thing by XORing it with itself. In assembly language, absolutely - knock yourself out, everybody does that.
But in a higher level language? Don't do that, mainly bcs the compiler will work that out anyway, and you are obfuscating the important stuff which is 'the code'.
I have a whole rant about how every CLI app needs a new option. Like ‘-help’ (or ‘-h’ or ‘-?’) which tells you *what*, except it's ‘-e’ or ‘-explain’ and it tells you *why* and *for what*.
@jackwilliambell @troublewithwords this reminds me of cobra (go) and click (python) both having gentle and not so gentle nudges about documenting what each flag or option does.
Both awesome libraries for writing cli tools in those languages too.
> code is self documenting
I feel like people that are actually serious about what they're doing don't say this. Sure you can read a declarative program and see that given inputs go through a pipe to produce a thing but why by so snotty about it? It isn't like comments make you lesser in some way. It's more like gatekeeping the code because, uh, ego?