Live data from Hacker News

Want cleaner code? Use the rule of six

davidamos.dev

341–350 of 352 posts

Re: Want cleaner code? Use the rule of six

#341
post #75

I enjoyed the article and agreed that working memory places a fundamental limit on the intelligibility of otherwise equivalent pieces of code. As a former psychologist with experience of memory research (though not quite this area), it might be useful to others if I add that: - The size of the short-term store is normally said to be 7 plus or minus 2 (the 'magic' number 7) - The Working Memory model has somewhat over…

Not high priority, but I'd love any references or names / key words I could look into it with. I'm traditional wide comp sci by academic training, but spend my day job as a low-code enabler for non-programmers with varied backgrounds. The working memory model explains and fits well with what I see them get and struggle with in day to day work, and I'd welcome references I could use to optimize my approach.

An overview of the model and its history: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4207727/

The Wikipedia entry for WM is also very good: https://en.wikipedia.org/wiki/Working_memory

It's a bit tricky to tell what you're doing exactly - perhaps drop me an email if you have any queries (using this account; my original parent post was on a throwaway account because I had login problems).

Re: Want cleaner code? Use the rule of six

#342
post #120

I enjoyed the article and agreed that working memory places a fundamental limit on the intelligibility of otherwise equivalent pieces of code. As a former psychologist with experience of memory research (though not quite this area), it might be useful to others if I add that: - The size of the short-term store is normally said to be 7 plus or minus 2 (the 'magic' number 7) - The Working Memory model has somewhat over…

This means it should be the rule of five. We can't count on everyone having full capacity all the time.

I see what you mean - five will fit with more room left over. But much like a hand that has a capacity to hold 7 or so marbles, if you grasp only five at a time, your overall productivity will (almost certainly) slow.

Re: Want cleaner code? Use the rule of six

#344

What the author is missing is that easy to read/reason/understand about is within the context of making a change to the code to fix a bug, add a feature or make some non-functional improvement to it. This is what most of the "easy to read" articles forget. Show me why it is easier to fix a bug, add a feature or make a non-functional improvement to the code with their style than without. For example, if you've extract…

> For example, if you've extracted something into its own function, are you then sharing this function and using it in other places as well? If you then change the body of that function, are you now possibly breaking other parts of the code that relied on its old behavior?

You absolutely are. Which is why I think that DRY was pushed too hard, and for the wrong reasons. Everyone said "you only have to change it once!" rather than "consider how many callers depend on this". There's a reason the "rule of 3" came around; and those reasons I've found were mostly experience and empirically based.

Re: Want cleaner code? Use the rule of six

#345

Earlier quoted context omitted.

Your example is artificially designed to make your point, but imagine a case like this: # Add the left & right margins width = calculatedWidth + 1 + 1 The "+1+1" might indeed be more readable than "+2".

You miss the point. The problem are not arithmetic operations.

Sorry if I've misunderstood your post.

The parent article was about replacing dense code with more lines of simpler syntax. I assumed you were in the shorter-is-better camp and willing to prove it with your example, where shorter undoubtedly is better. I found your example far stretched, and designed another case using your material (arithmetic operations) but applying only 2 repetitions, much in line with the spirit of the fine article.

I hope I managed to show that, although we can agree that "very very longer is worse", this not always implies "shorter is better".

Re: Want cleaner code? Use the rule of six

#347

Earlier quoted context omitted.

If you are unit testing you should not have to worry about tweaking a function and it breaking everywhere else.

> If you are unit testing you should not have to worry about tweaking a function and it breaking everywhere else. "should" is a word loaded with authority. Why? If you believe a unit tests is for turning an impure function into a pure function (so you can just test what it's doing and no other effects), then in many cases tweaking will break existing unit tests. If the function exists, it's assumed it's used by more…

No it doesn’t and I’d hate to have you working on my team with this mindset.

Re: Want cleaner code? Use the rule of six

#348

Earlier quoted context omitted.

> If you are unit testing you should not have to worry about tweaking a function and it breaking everywhere else. "should" is a word loaded with authority. Why? If you believe a unit tests is for turning an impure function into a pure function (so you can just test what it's doing and no other effects), then in many cases tweaking will break existing unit tests. If the function exists, it's assumed it's used by more…

No it doesn’t and I’d hate to have you working on my team with this mindset.

> even the internal dependencies necessarily breaks the known contracts with other units

The implication is that there are unit tests for those dependencies as well. Changing the signature used for them, breaks the same contracts as a wrapping function signature change. Ofc, you may already have tests to cover alternate signatures (overloading, et al) but conceptually it's the same pitfall.

Re: Want cleaner code? Use the rule of six

#349
post #337
post #90

Earlier quoted context omitted.

Python's lambdas can have as many lines as you want. Just wrap parens around it. Hissp uses this form as a compilation target. Its REPL shows the Python compilation. Play around with it til you get it: https://github.com/gilch/hissp

That’s misleading. While you can have multiple lines, you are not allowed to have multiple statements. Nobody wants to write code chained together with ternaries and and-operators. When someone says multi-line lambdas, what people actually mean is multi-statement lambdas.

The misleading part wasn't from me. I'm trying to correct a common misconception here. Both "multiple" and "lines" are wrong.

Lambdas can't have single statements either. The body is only an expression. So why are we saying "multiple" (a word to distinguish from "single") if you meant to multiply statements? The number of statements is zero.

Does a try/except fit on one line? That's a try statement. Does a class? Technically possible, but typically no. That's a class statement. It's not just the "class" word, or the first line. The class statement includes its body. Many Python statement types can't fit on one line, and many others don't have to. So why are we saying "lines" to mean statements when these are not remotely the same thing?

The statement/expression distinction is a holdover from FORTRAN. Statements are just expressions that don't return a value. (Yes, Smalltalk and Haskell can implement control structures with expressions just fine, thank you.) Many functional languages do away with the distinction altogether and just use expressions. You only really need lambdas in Python for the functional style, and in functional style, expressions are all you need!

Re: Want cleaner code? Use the rule of six

#350
post #343

Earlier quoted context omitted.

Quoted post unavailable.

Quoted post unavailable.

I don’t know Russian. Your point doesn’t line up with the topic at all. I know Ruby. I still think it’s unreadable. Can you try next time?

  I feel incredibly sorry for the poor kids you teach. They will be suffering aftereffects for the rest of their lives.
I feel bad that your account hasn’t been flagged for your behaviour on here. For someone who thinks they understand Ruby so well, you certainly haven’t taken any time to understand the rules. Personal attacks? Really? You just showed your cards. Weak arguments are followed with weak ad hominem.

It’s not just me, no one is teaching Ruby to kids because it’s not a good language for readability. Take it up with all of the teachers who see it just as I do.

Post reply on HN