Live data from Hacker News

Want cleaner code? Use the rule of six

davidamos.dev

1–10 of 352 posts

Re: Want cleaner code? Use the rule of six

#2
I know it was just used as an example, but in reality when I see code like this I think "I really don't want to reinvent URL parsing for this, I'll import the library for that instead," resulting in much cleaner code.

But I do agree on keeping individual lines, if not entire statements, small and simple. The ruby chainsaw is a great tool for that. I find a chain of simple statements arranged in a flow to be more readable than using lots of intermediate variables.

Re: Want cleaner code? Use the rule of six

#4

in the example given, I started with 10 things to keep in working memory. now that we've added a named function or a named variable, we have 11. I suggest it is at least as important to name things well (or add comments) as it is to break lines up.

A nice feature of working memory is that although it's limited to around 6† "things", each thing can be any size, if your brain considers it a single unit. This is called "chunking".

So, it's easier to remember the three numbers 34, 765, 812 than the eight numbers 3, 4, 7, 6, 5, 8, 1, 2.

Refactoring code into separate functions with descriptive titles is probably a lot like combining numbers.

---

† Well, the article says 4–6; I'd always heard the average was around 7.

Re: Want cleaner code? Use the rule of six

#7
post #5

This seems like an interesting heuristic for anything that could automatically either generate or "format" code - a little more semantic than just relying on a text parser

That is a cool idea. Show the code the way you understand it best without even having to change the underlying text.

Re: Want cleaner code? Use the rule of six

#8
If there is some legitimate reason (say performance) to keep a tighter form (inline assembly, Python 1-liner, whatever), then making the unfurled equivalency as a comment nearby to allow the next developer to have a fighting chance would be really helpful. Also, error handling tends to be not included in the 1-liners.

Re: Want cleaner code? Use the rule of six

#10
Coming from a background in lower level languages where you can only express one simple thing per line my tendency has always been to be more verbose than my colleagues. The worst time I ever had was when I had to work on someone's Perl code that one time. So I really like this heuristic to make code more readable.
Post reply on HN