top | item 41249112

(no title)

klauserc | 1 year ago

I once had the questionable pleasure to write code in an environment where "at most one method call per statement" was strictly enforced. Had to constantly come up with names for intermediate values that would only get used once, in the very next statement.

It has kind of radicalized me against "pointless names". Names take up valuable space in the working memory of the reader. They need to carry their weight.

discuss

order

TrianguloY|1 year ago

Not only that, but when you save something to a variable the reader needs to remember it in case you use that variable later again. For me personally that's exhausting.

I've seen streams-like code with a variable for almost every step, immediately used the line below. I can understand when the reason is to "explain" the meaning of the intermediate step...but even so a comment is far better on my opinion.

I always try to inline every 1-use variable/method, unless there is a good reason for not doing so.

InitialLastName|1 year ago

I feel like Raymond Hettinger's guideline that line complexity should be scaled to the limitations of human working memory, so should be limited to 7(+/-2) tokens [0], is a good compromise. This leaves some margin for nesting, but keeps in mind the capacity of the human brain to understand what a line is doing. If you can explain what the line of code is doing in a single sentence, you can probably also find a way to either name the variable sensibly or factor the code so that your step is a (well-named) method instead.

[0] https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus...