Re:Want cleaner code? Use the rule of six
by nicoburns on Sunday September 25, 2022 @08:01AM
(#32970558)
I like putting comments above paragraphs of code so that you can skim it by trusting the comments, but you can also inspect any individual paragraphs implementation without losing your context.
Re:Want cleaner code? Use the rule of six
by thebackup on Monday September 26, 2022 @12:38AM
(#32979489)
Agreed. A comment can also explain things that a function name cannot.
Iām all for splitting code into paragraphs with a well written comment above each.
Of course I also extract functions, but only when there are obvious benefit, like removing repetition.
But if you don't put it in a separate function, then all that code becomes something that you have no choice but look at as part of this function, because the text of it is right there in the function.
Sticking part of the code in a sub-function gives you the choice: Do I trust that this function does what it says, with no other effects that I have to worry about? Or do I go look at it to make sure?
But not sticking the code in a sub-function gives you no choice - it's right there as part of the function you're looking at, and you have to see what that code is doing as part of understanding the function. And having that code not in a separate function makes it more likely that it has side effects that can mess up the rest of the function (separate functions is a firewall against side effects).