‹ BackHN Continuity

Thread

Small programming tricks

681 points · 294 comments · signa11

  1. winternewt · · focus · HN ↗
    Only a few of these are actual programming tricks. The problem with sharing them is that they'll typically seem obvious to you, since you know them. It's difficult to know what is actually unknown to other people, and if you share stuff everybody knows you risk coming off as arrogant.

    Here's one that I think more people should know: avoid branches. If I can do the same thing without an if statement and even a logical expression, the code typically both becomes easier to understand for people and easier to run for the CPU.

    1. cachvico · · focus · HN ↗
      I'm struggling to comprehend how branches can be avoided (or why one would want to, as they are the cornerstone of programming). I can only think how to obfuscate them, which is rarely useful.
      1. craftkiller · · focus · HN ↗
        Here&#x27;s an example of removing a branch that was posted to HN a little over a month ago: <a href="https:&#x2F;&#x2F;www.greyblake.com&#x2F;blog&#x2F;branchless-rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.greyblake.com&#x2F;blog&#x2F;branchless-rust&#x2F;
        1. metabagel · · focus · HN ↗
          OK, but the code with the branch is easier to understand.
          1. jamiejquinn · · focus · HN ↗
            Generally agree. As with many optimisations, branchless code can easily be less obvious than the branchy equivalent.
          2. robby_w_g · · focus · HN ↗
            Yeah, I don&#x27;t buy the premise that branchless code is intrinsically easier to understand. Maybe OP&#x27;s point is that adding unnecessary branches makes code harder to read? But that&#x27;s generally the case for any unnecessary code.
            1. winternewt · · focus · HN ↗
              I see stuff along the lines of:

                if (x == 0) {
                    return y;
                }
                
                y += 25*x;
                return y;
              
              and skipping the if just makes the function shorter and simpler, while also not involving the CPU branch prediction. Another one that doesn&#x27;t necessarily skip all branching but at least drops one - and more importantly makes the code simpler and easy to verify, is removing the if statement in code like

                if (count == 0) {
                    return;
                }
              
                for (int i = 0; i != count; i++) {
                  puts(&quot;hello&quot;);
                }
              1. GuB-42 · · focus · HN ↗
                Both of your examples are optimized by the compiler (gcc 16.1 -O3).

                In the first case, the compiler removes the first if&#x2F;return

                In the second case, if you don&#x27;t have the first if&#x2F;return the compiler will add it. That&#x27;s because it will actually convert your loop into a do&#x2F;while, with the test in the end, because it is more efficient. But it has to handle the count == 0 special case first, so it will do that early return even if it is not explicitly there.

                That&#x27;s the kind of optimization modern compilers are good at.

        2. cestith · · focus · HN ↗
          That&#x27;s a really nice example. Thanks.
        3. metabagel · · focus · HN ↗
          From the article:

          =====

          Should you go branchless?

          Most of the time, no. Branchless code is harder to read and easier to get wrong. Besides, compilers know a lot of tricks and already do a lot of this work for us.

          Only when a profiler points at a hot loop, and the loop contains a branch on unpredictable data this technique can pay off big.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.