News for hackers. Stuff that matters.
Re:Want cleaner code? Use the rule of six by davnicwil on Saturday September 24, 2022 @01:06PM (#32964540)
> the code can't be read top-to-bottom

The idea of the technique is to split out code at a different level of abstraction with a clear name communicating what it does, while hiding the details of the how, because you don't need to care about that detail at all to fully grok the code in the calling function.

Where this breaks down is when the code you're trying to split out is not at a different level of abstraction, and how it works is meaningful to the surrounding code in the calling function.

So I think the issue you are seeing isn't with the technique, it's with the technique being misapplied. I think this is likely the only difference between when it 'often works' and 'just as often doesn't' in the code you're working in :-)

Re:Want cleaner code? Use the rule of six by lamontcg on Saturday September 24, 2022 @05:13PM (#32966574)
Each function becomes something new that needs to stick in your brain.

Someone that applies "MORF" to their code winds up nearly inventing their own language in the file that they're writing. All that takes up more memory when you're reading their code, because due to leaky abstractions the actual implementation of whatever the function name that you replace it with is often important.

I have an actual track record of taking code that someone had MORF'd to hell and rewriting it, and making it about 40% shorter, with much fewer concepts to process.

Inventing a term like "MORF" is probably illustrative of the problem itself. Without looking at the blog post what exactly was that acronym again? That is just one more thing for you to try to memorize. The author is riffing on things like "DRY" and "YAGNI" that are well-known, but it isn't really helping with readability when you lift it out of that context.

Re:Want cleaner code? Use the rule of six by AnimalMuppet on Saturday September 24, 2022 @11:13PM (#32968617)
> Each function becomes something new that needs to stick in your brain.

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).

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.
Re:Want cleaner code? Use the rule of six by usrusr on Saturday September 24, 2022 @06:49PM (#32967275)
I agree, one of my favorite ide functions is the inline button. Because all too often that supposedly clear name turns out to be not clear at all.

Perhaps it would not be quite as bad if there was a clear distinction between "this is a function for reuse" and "this is a function that's naturally just elaborate code folding", but that does not really exist. Just stick to single-assignment variables and keep their scopes short and enjoy the good parts of sequential code.

Re:Want cleaner code? Use the rule of six by majormajor on Saturday September 24, 2022 @08:19PM (#32967817)
> All that takes up more memory when you're reading their code, because due to leaky abstractions the actual implementation of whatever the function name that you replace it with is often important.

"Better names" is a good answer for a sloppy pattern that looks like this:

fun saveRecord(record){

wireRecord = record.toWireRecord();

innerSaveRecord(wireRecord)

}

fun innerSaveRecord(wireRecord){

log("saving record " + wireRecord)

saveWireRecord(wireRecord)

}

That's a stupid level of nesting instead of a reasonably-named "convertAndLogAndSaveRecord()" - then you get into debates about if logging should be in that name, otherwise it's happening despite the name not saying it, etc.

So all that to say that moving into functions alone can be good, or can be bad.

The easiest-to-define benefit of the verbosely-named "convertAndLogAndSaveRecord()" is that it becomes easily testable vs having those lines inlined somewhere. (I'm often much more ambivalent on "reuse" benefits - if your function is specific it can't be made reusable without making it less well defined.)

But as far as I know, the world doesn't offer me a way to test that the function not only does what it says but doesn't do what it doesn't say. And it's similarly hard to talk other programmers out of shoehorning new logic into existing functions - now making them harder to reason about - rather than new functions that are composed differently or with the existing ones.

Because if "checkAccountingProcessResultsAgainstAudit" is going to give you readability benefits, it damn well better do what it says every time you call it instead of becoming a maze of conditionals and optional behavior.

Re:Want cleaner code? Use the rule of six by drewm1980 on Monday September 26, 2022 @03:05AM (#32980292)
Besides giving a block of code a name, functions also reduce the scope of the data the code can possibly be acting on. They also introduce a namespace that lets symbol names focus on locally relevant information. How did this huge refactoring patch you are describing hold up in code review?
Re:Want cleaner code? Use the rule of six by lifeisstillgood on Sunday September 25, 2022 @03:06AM (#32969518)
Err - OK - my google fu is failing me. What is MORF (I assume it's some kind of acronym for refactoring ...)
Re:Want cleaner code? Use the rule of six by Izkata on Sunday September 25, 2022 @03:27AM (#32969606)
It's defined in the article, the author seems to have invented it.
Re:Want cleaner code? Use the rule of six by wpietri on Saturday September 24, 2022 @02:06PM (#32965140)
This is my experience too. I love hiding detail for readability, but you have to hide the right details!

It's also definitely related to testability for me. If I'm pulling out the right details, then I'll often get a nice cluster of tests that pin down the higher-level concept in a way where it's both the production and test code that gets more readable.

Re:Want cleaner code? Use the rule of six by kristopolous on Sunday September 25, 2022 @01:11AM (#32969108)
The problem is those who create the messes to need that advice probably will take that advice and create even bigger messes

In practice, pithy adages don't get us any closer to sanity

Really the only news you can use is

1. Try to modify things with code you didn't write by not simply throwing parts away

2. If you think it's really difficult to deal with, figure out if other people agree with you.

3. understand why everybody thinks this

4. If you are doing that in your own code, then stop doing that.

You have to viscerally understand why a practice is bad and how doing it affects other people.

This is how you can intuitively avoid such practices in the future. Not through things that rhyme or acronyms that spell words but through social intelligence. It's fundamentally behavior

Re:Want cleaner code? Use the rule of six by danmaz74 on Sunday September 25, 2022 @02:17AM (#32969339)
I understand the idea, but it requires:

* great ability at naming methods - which isn't very common * massive discipline at RE-naming methods when they get changed even slightly to do something more

Regarding the latter, I've seen so many times methods which originally described what they were doing, but now they don't any more, that I never just trust the name to tell me what a method really does.

Re:Want cleaner code? Use the rule of six by catlifeonmars on Sunday September 25, 2022 @03:03AM (#32969505)
Naming is probably the single hardest part of writing scalable, legacy code. Cache invalidation is a cakewalk in comparison. I believe this is in part because naming is a miniature exercise in empathy for future readers particular of a piece of code.
Re:Want cleaner code? Use the rule of six by shellback3 on Saturday September 24, 2022 @01:34PM (#32964833)
I agree, my thoughts were along this line especially when I noticed the 'magic' number -3.