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.

Writing code with multiple overlapping side effects with a straight face

167 pointsby miqktalmost 8 years ago

17 comments

jxramosalmost 8 years ago
&quot;...who firmly believed that the terser your code, the faster it ran. The author crammed multiple side effects into a single expression, used ternary operators like they were going out of style, and generally believed that run time was proportional to the number of semicolons executed, and every variable killed a puppy.&quot;<p>How does one go about countering that mentality in some rule of thumb fashion if at all possible? Could a disassembly illuminate identical push&#x2F;pop operations on the memory stack as explicit intermediate variables would? Maybe a simple intermediate result expansion timing profile side by side the terse code&#x27;s profile?
评论 #14810769 未加载
评论 #14811008 未加载
评论 #14810698 未加载
评论 #14810662 未加载
评论 #14814590 未加载
评论 #14810618 未加载
评论 #14811371 未加载
评论 #14813580 未加载
评论 #14810696 未加载
评论 #14811640 未加载
userbinatoralmost 8 years ago
I wonder if the first one was meant to be<p><pre><code> a -= a * a; </code></pre> which evolved from<p><pre><code> a *= a; </code></pre> after the author thought &quot;I need to subtract its square from itself, not just square it&quot;. I can see how this could occur in numerical code.<p>I&#x27;ve noticed there are two opposing schools of thought on issues of &quot;insane code&quot; like this; on one side there&#x27;s &quot;use the language to the fullest if it makes sense to&quot;, and on the other is &quot;avoid anything that might be the slightest bit confusing&quot;. I think that taking either of those to their logical conclusion is not a good idea --- in the former case, you&#x27;ll end up with extremely dense and (initially) difficult-to-understand code, but the latter case will cause a gradual degradation of code into something approaching the verbosity of Asm, but with none of the benefits (e.g. &quot;ternary operators are confusing, don&#x27;t use them; multiple levels of precedence are confusing, so always fully parenthesise expressions; nested parentheses are hard to read, so don&#x27;t use them either and only do one operation per statement; boolean expressions are confusing, so always use if&#x2F;else; nested if&#x2F;else are confusing, so don&#x27;t nest and always refactor to use a function call, etc. etc.)<p>It does seem that different languages vary in where they are on this scale, with more &quot;exotic&quot; ones like APL tending highly towards the former (interesting related discussion: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13565743" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13565743</a> ) and C#&#x2F;Java towards the latter. The ideal is probably somewhere in the middle.
评论 #14811877 未加载
评论 #14811854 未加载
评论 #14813590 未加载
评论 #14811982 未加载
评论 #14811853 未加载
d--balmost 8 years ago
Well, maybe a good way to avoid this is to have the compiler email the manager rather than printing a warning.
评论 #14811498 未加载
评论 #14811780 未加载
评论 #14811297 未加载
schindlabuaalmost 8 years ago
I use the ternary operator all the time.<p><pre><code> result = boolean_flag ? do_this() : do_that(); </code></pre> I think it&#x27;s less noisy and nicer to look at then an if-else, provided the statements aren&#x27;t too long. God forbid multiple side effects though, even in languages with less complicated sequencing rules than C&#x2F;C++.
评论 #14810994 未加载
评论 #14811551 未加载
评论 #14811999 未加载
评论 #14811183 未加载
euyynalmost 8 years ago
&gt; First of all, there will be a lot of false positives. For example, you might write<p><pre><code> total_cost = p-&gt;base_price + p-&gt;calculate_tax(); </code></pre> &gt; This would raise the warning because the compiler observes that the calculate_tax method is not const, so it is worried that executing the method may modify the base_price, in which case it matters whether you add the tax to the original base price or the updated one. Now, you may know (by using knowledge not available to the compiler) that the calculate_tax method updates the tax locale for the object, but does not update the base price, so you know that this is a false alarm.<p>I think the warning in that case is still fair, and I&#x27;d be perfectly happy to get it. calculate_tax() could be modified to affect base_price with this compilation unit being none the wiser.
评论 #14813615 未加载
ridiculous_fishalmost 8 years ago
v8 at one point made inlining decisions based on character count. Terse code would be inclined more aggressively, and even adding comments could defeat inlining: <a href="https:&#x2F;&#x2F;top.fse.guru&#x2F;nodejs-a-quick-optimization-advice-7353b820c92e" rel="nofollow">https:&#x2F;&#x2F;top.fse.guru&#x2F;nodejs-a-quick-optimization-advice-7353...</a>
评论 #14811041 未加载
sgt101almost 8 years ago
Two unfashionable lessons that I learned when I was a practicing programmer rather than a hobbyist and loathsome manager.<p>1. Code is a communication system : between you and the next person to touch it. You are telling the other person how you made this work. Invest in this because the next person will probably be you in six months.<p>2. Compilers and type systems are your friends. Write code that extracts errors from the compiler by explicitly constraining things as much as you can afford to do. Avoiding typing or checking via long methods or low level datatypes is bad. Short typed, functional methods and high level data types are good.
nottorpalmost 8 years ago
After gaining a bit of experience programming, I&#x27;ve developed for myself the following rule:<p>If I feel particularly smart and proud after writing a piece of code, I look at it again and remove some smartness in favour of readability.<p>Unless it&#x27;s absolutely performance critical, in which case I try to comment the hell out of what&#x27;s going on.
评论 #14813317 未加载
drewg123almost 8 years ago
I used to think I had a pretty good grasp on how the compiler was going to encode things.<p>After spending months looking at disassembled FreeBSD kernel C code in a profiler, trying to hunt down cache misses, I&#x27;m realize that it is hard to count on anything and that my instincts, even after 25+ years of C programming, are often wrong.
bryanrasmussenalmost 8 years ago
I wonder if, from this: He gave as one example a book from an apparently-successful author (sales of over four million and counting) who firmly believed that the terser your code, the faster it ran. The author crammed multiple side effects into a single expression, used ternary operators like they were going out of style, and generally believed that run time was proportional to the number of semicolons executed, and every variable killed a puppy.<p>If the described author could be identified? Perhaps not absolutely, but they do have some specific stylistic quirks combined with a particular sales level.
评论 #14816766 未加载
wyldfirealmost 8 years ago
&gt; The people who would benefit from the warning don&#x27;t have the necessary background to understand it.<p>In my experience, this is accurate, but underscores a teaching opportunity. If you show Joe Beginner how to get the warnings, explain how they apply to the code and why the warning was good, they may slowly begin to appreciate the warnings. So that even if they don&#x27;t understand, they won&#x27;t necessarily dismiss the warning next time -- instead they&#x27;ll go ask for help.
评论 #14811825 未加载
mcguirealmost 8 years ago
From one of the comments there,<p>“<i>Nathan Reed</i><p>&quot;<i>July 19, 2017 at 4:34 pm</i><p>&quot; <i>Didn&#x27;t you just write such code with a straight face yourself only yesterday? ;) In your bytecode interpreter example:</i><p><pre><code> memory[NextUnsigned16()] = NextUnsigned8(); </code></pre> &quot;<i>Presumably the NextUnsigned8&#x2F;16 functions have side effects of advancing a buffer pointer, so this is nearly the equivalent of p[x++] = ++x.</i>&quot;
js8almost 8 years ago
Somehow I thought this would be about monads and using them to control side effects.
评论 #14811424 未加载
__salmost 8 years ago
Closest I&#x27;ve come to really embracing that kind of thing is <a href="https:&#x2F;&#x2F;github.com&#x2F;serprex&#x2F;pythonaes&#x2F;tree&#x2F;master&#x2F;aespython" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;serprex&#x2F;pythonaes&#x2F;tree&#x2F;master&#x2F;aespython</a><p>NB it really is faster<p>edit: nvm, Crumb is way more that kind of thing: <a href="https:&#x2F;&#x2F;github.com&#x2F;serprex&#x2F;Crumb&#x2F;blob&#x2F;master&#x2F;C3.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;serprex&#x2F;Crumb&#x2F;blob&#x2F;master&#x2F;C3.py</a> I forgot I concocted that little devil
评论 #14811804 未加载
naileralmost 8 years ago
What does:<p><pre><code> a -= a *= a; </code></pre> Do?<p>Looking through C# docs (which I assume this is, but don&#x27;t have experience with), it seems A&#x27;s value is changed to be the previous value of a, minus itself (ie 0) multiplied by a? In other words:<p><pre><code> a = 0 ?</code></pre>
评论 #14812093 未加载
pmarreckalmost 8 years ago
A lot of these pitfalls and concerns would just completely go away in a language that used immutable data structures pervasively
avipalmost 8 years ago
The pythonic example I&#x27;ve seen not once:<p>{&quot;some&quot;: &quot;dict&quot;}.setdefault(&quot;some&quot;, foo())<p>while expecting foo() to be short-circuited.
评论 #14812364 未加载
评论 #14811918 未加载
评论 #14812117 未加载
评论 #14811672 未加载