TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How much information is too much information? (2022)

89 pointsby gus_leonelalmost 2 years ago

20 comments

klausercalmost 2 years ago
Interestingly, I had the exact opposite reaction. I found the second function harder to reason about because it heavily relies on mutation and the fact that individual loop iterations commute with respect to one another. (To understand what the final value of `min_value` is, you first have to understand that `&lt;` being transitive means that the iteration order doesn&#x27;t matter)<p>The first function, on the other hand, has only static assignments (one name is only ever bound to a single &quot;definition&quot; at any one time). In terms of working memory, that means that each variable in the first function only occupies a single &quot;slot&quot;. In the second function, the mutable `min_value` variable necessarily occupies multiple &quot;slots&quot;, one for each point in the program where the variable could change. So you&#x27;d have to keep track of a &quot;min_value[0] = 9999&quot;, a &quot;min_value[1] = number (if number odd &amp;&amp; &lt; min_value)&quot; and _maybe_ a &quot;min_value[2] = min_value (if number even || &gt; min_value)&quot;.
评论 #36680325 未加载
评论 #36679712 未加载
评论 #36679791 未加载
评论 #36679973 未加载
评论 #36698991 未加载
zellynalmost 2 years ago
I agree with all of this, except the part where they assume extracting a function is essentially free.<p>Our profession seems to consistently underestimate or even ignore the complexity of interactions between many little pieces.<p>I would rather work on a module with 10 clearly-written 500-line functions than one with 500 clearly-written 10-line functions. Keeping in my mind the complex and variable interactions of a whole constellation of tiny functions is much harder than grinding my way through understanding a few more complex pieces of code. Have you ever worked in a codebase where the template method pattern was combined with too many levels of inheritance and each subsequent method is implemented at a different level of the hierarchy?<p>Likewise for microservices and lambdas. The organizational complexity of an app atomized into 100 lambdas is mind-boggling, because you have to understand the implicit and emergent orchestration of distributed state as those lambdas transition from one to another.<p>All that said, I definitely like this metric better than most others for the static complexity of a single function.
评论 #36680824 未加载
评论 #36680583 未加载
UglyToadalmost 2 years ago
It feels like their calculation missed 2 crucial confounders, stack depth and type tracking.<p>1) Stack depth. In their refactored example if I truly want to understand the code I need to now store everything I was thinking about in the current function and jump to their new `is_paid_today` function, then into each of the 3 dependent functions, I&#x27;d say as finger in the air type estimates each stack frame should add 2 * depth and then some multiplier for breadth or something. This is why I prefer big functions like the initial state read from top to bottom with no indirection, abstraction or surprises where reasonable. (curly braces wouldn&#x27;t go amiss to give visual indicators of scope...) This is completely counter to &#x27;clean code&#x27; dogma but clean code dogma is... bad.<p>2) Type tracking. To reignite this holy war if I have to track the type information mentally then I&#x27;d say every parameter and variable has an additional +1 working memory overhead. What is employee, what is employee_database, what is `is_end_of_month` and where on earth is it coming from? Try as I might I just cannot understand people who onboard to big codebases without type hints at least, y&#x27;all are a different breed, you should work as like super-rememberers.
评论 #36680308 未加载
评论 #36680434 未加载
Ensorceledalmost 2 years ago
I have a hard time getting developers to follow this type of advice and a lot of developers will &quot;clean up&quot; and &quot;optimize&quot; the example code in introducing_variable_version by eliminating the paid_today variable back into a single <i>if</i> statement.<p>I recently had a developer &quot;improve&quot; a security sensitive series of checks (think a series of if statements: if logged in, if not inactive, if in required group, etc.) by merging the statements into a single if statement that, unsurprisingly, broke the check and left a security hole.
评论 #36679509 未加载
评论 #36680275 未加载
评论 #36680706 未加载
bluetomcatalmost 2 years ago
The &quot;rules&quot; are overly defined with respect to the static elements of source code. Cognitive load in understanding code is not linearly tied to the number of variables, function calls, conditional checks, etc.<p>For example, &quot;volume = width * length * height&quot; is less demanding than, say, &quot;employee_qualifies_for_bonus_after_midterm(emp)&quot;. I think it comes down to limiting the spatial scope of variables and fragments of code, naming things in a way that makes sense to most people, and containing complexity locally. It&#x27;s also called &quot;abstraction&quot;.
评论 #36680634 未加载
tra3almost 2 years ago
Complexity related -- &quot;Don&#x27;t wake up the programmer&quot; (<a href="https:&#x2F;&#x2F;alexthunder.livejournal.com&#x2F;309815.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;alexthunder.livejournal.com&#x2F;309815.html</a>).<p>This crystallized for me why you shouldn&#x27;t interrupt devs when they are working and keep interruptions to a minimum.<p>Briefly, the author asserts, that writing software is like sleeping. For a lot of people getting to sleep takes a while and if you wake them up even for a minute, they have to go through the &#x27;getting to sleep process&#x27; again and it&#x27;s non trivial.<p>Software complexity is the same, it takes a while to go through the cognitive exercise of grasping what the code does. If you interrupt me, I lose the context and have to go through the &quot;code loading&quot; steps again..<p>I tried to explain this to my wife, unfortunately she&#x27;s a rare breed that can fall asleep on a dime so this explanation did not make quite the impact that I was hoping for.
pkkmalmost 2 years ago
I don&#x27;t see any code being improved in this article. The author does the same thing I&#x27;ve noticed <i>Clean Code</i> adherents do: he focuses on the busywork of splitting functions into tiny pieces while missing the actual place that requires the most mental effort to understand and is the most likely to become a source of bugs. In this case, it&#x27;s the complex boolean expression that relies on Python&#x27;s operator precedence. It could be rewritten like so:<p><pre><code> if ( ( has_passed_probation(employee) and is_paid_monthly(employee) and is_end_of_month ) or ( is_paid_weekly(employee) and is_end_of_week ) ): </code></pre> to make it clearer that the <i>has_passed_probation</i> check only applies in the monthly case.
TexanFelleralmost 2 years ago
Having low working memory combined with my working memory slots being overwritten my random digressions my brain decides to pursue, AKA ADHD, reducing the amount of state I need to track is one of the most significant factors for productive coding. I guess that&#x27;s why I find FP style code that eschews mutable variables and collections and anything that breaks referential transparency(AKA the ability to understand code locally, without surrounding context) appealing.
JackFralmost 2 years ago
The big difference is that the first function may be be wrong, but the second is quite obviously wrong.<p>The smallest odd of a list of numbers &gt; 9999 is not 9999.<p>The smallest odd of an empty list is 9999 is not 9999.<p>The smallest odd of a list of even numbers is not 9999.<p>The first function does seem easier to understand but relies on global state (is_end_of_week, is_end_of_month) and side effecting functions (run_payroll).<p>Neither of these functions should survive code review.
drittichalmost 2 years ago
I&#x27;m still left wondering whether the code is correct, in that an employee that is paid weekly does not require passing probation to get paid. Is that the correct logic, or is it the (very common) error of forgetting an extra set of parentheses when using OR?<p>Given that there would probably be more complexity in the real code I would create separate functions expressing the rules for each employee type.
评论 #36679807 未加载
评论 #36679759 未加载
评论 #36679864 未加载
Izkataalmost 2 years ago
First one is far far easier for me to understand for two reasons:<p>* The table-like structure of the conditions instead of nesting like the second one. The two-dimensional relationship makes it trivial to reason about - understanding it works at a higher level than the individual variables.<p>* The body of the conditional is entirely self-contained and can be understood separately from the conditional instead of having to understand them both at the same time.<p>The refactor they suggest at the end only works so simply because of my second reason here. It&#x27;s kind of an improvement (self-contained logic that can be reused), but kind of not (now you have to look in two locations to actually understand what it&#x27;s doing), but either way isn&#x27;t really necessary if you read code at a higher level than token-by-token.
评论 #36680635 未加载
heikkilevantoalmost 2 years ago
I believe a lot of this is affected about how familiar idioms are used. The second example simplifies to me as something like &quot;Loop to find the smallest&quot;, &quot;check parity&quot;. The thing that seemed to take most of my cognitive capacity was the unstated assumption that 9999 was clearly bigger than any of the possible numbers.<p>The first example did have a bit more complex condition, but it was fairly logical and not hard to understand. Again what bothered me was that there must have been a hidden way for the result of run_payroll(name, employee.salary) to pass its result to write_letter(name)
评论 #36680121 未加载
tialaramexalmost 2 years ago
The second function bothers me because it has this arbitrary looking value, presumably as a sentinel, and that&#x27;s a hidden requirement of the function. This function is actually minimum_odd_number_or_9999 for some reason, and chances are nobody actually wants that. Instead languages should encourage distinguishing None from actual answers - and then sure as an optimisation you&#x27;d consider using a sentinel to achieve that mechanically, but it doesn&#x27;t alter the program meaning.<p>On the other hand, in this style the first program could have any amount of hidden traps.
jacknewsalmost 2 years ago
&quot;We can then separate out that complexity even further if we move the determination of the value of paid_today into its own function.&quot;<p>Ouch, and this immediately makes it less obvious what&#x27;s going on, because you have to look up the function. In this case, the name is self-documenting, but that&#x27;s far from always the case. And even then the extracted function seems to conflate 2 concepts, &#x27;pay day&#x27;, and &#x27;eligible to be paid&#x27;, and the code even looks suspect&#x2F;buggy; which is more clearly unambiguous:<p><pre><code> (one and two or three) (one and (two or three)) </code></pre> This kind of extraction is only useful if you need to re-use the encapsulated calculation IMHO, otherwise just comment each term, or use something more powerful than &quot;if and or then&quot;, eg decision tables, etc.
cborensteinalmost 2 years ago
Love the concept of a working memory score for code.<p>&gt; the number of distinct pieces of program state that a developer needs to keep in their head when analysing a function and the impact this has on the understandability and maintainability of code.<p>The same concept applies not just to writing code, but to any context that you need to load up in order to do your work. E.g. context when jumping into a meeting or when reviewing a new project spec.<p>In those cases, one of the key things is being able to offload what you don&#x27;t need in your working memory right now to some other solution (usually notes). More on that here <a href="https:&#x2F;&#x2F;www.stashpad.com&#x2F;blog&#x2F;working-memory" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.stashpad.com&#x2F;blog&#x2F;working-memory</a>
jagged-chiselalmost 2 years ago
&gt; the top function is fairly obviously harder to understand<p>What? It’s one conditional branch, and in that branch there are three “high-level” operations, all named very well (assuming they’re correct names.) What’s harder to understand here?<p>I spent much longer understanding what was going on in the second function.<p>Further, the variable name “paid_today” is terrible because the result of the condition does not tell us that the employee was indeed paid, but rather that they <i>should be</i> paid. They’re not <i>paid</i> until after the code in the conditional branch runs.<p>Extracting this information to its own function is certainly an improvement (even if the function name still isn’t great.)<p>I would have liked the article to spend more time making the second function take less time to understand.
TRiG_Irelandalmost 2 years ago
I&#x27;ve certainly come across the magic number seven plus or minus two before, in study of simultaneous interpreting. The interpreter is usually a sentence or two behind the speaker. The trick, they say, is to increase the size of the chunks, as you can&#x27;t do much about the number of them. Try to process a sentence in clauses, not in words. (You need to do that anyway, really, as the grammar of the languages may not match up.) I suspect that something similar applies here.
vander_elstalmost 2 years ago
Are there any implementations of this? So that it&#x27;s possible to measure it for a given piece of code?
quantum_statealmost 2 years ago
is there a tool to scan the code and output the working memory metrics for a given cognitive context such as line, etc.?
hmmokidkalmost 2 years ago
Functional Programming solves this problem.