Ask HN: What are some examples of good code?
Not Ruby but for C code, I like Redis&#x27;s code:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;antirez&#x2F;redis&#x2F;tree&#x2F;unstable&#x2F;src" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;antirez&#x2F;redis&#x2F;tree&#x2F;unstable&#x2F;src</a><p>I don&#x27;t personally use the product, but I find the source well written and always share it as an example of nicely done C code.<p>Here is some nice Erlang code I like -- network packet parsing:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;msantos&#x2F;pkt&#x2F;tree&#x2F;master&#x2F;src" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;msantos&#x2F;pkt&#x2F;tree&#x2F;master&#x2F;src</a><p>Notice how concise this is:<p><pre><code>    codec(
       &lt;&lt;4:4, HL:4, ToS:8, Len:16,
       Id:16, 0:1, DF:1, MF:1, %% RFC791 states it&#x27;s a MUST
       Off:13, TTL:8, P:8, Sum:16,
       SA1:8, SA2:8, SA3:8, SA4:8,
       DA1:8, DA2:8, DA3:8, DA4:8,
       Rest&#x2F;binary&gt;&gt;
    ) when HL &gt;= 5 -&gt; ...
</code></pre>
This is due to the beauty of binary pattern matching. You could kind of do it in C by casting to a struct but notice here it is also calculating header length (HL) as part of the match operation. So it can do a bit more than just casting a binary blob to a struct.<p>Another thing here is that it is also big endian by default so there is not need for htons(), htonl() and such functions sprinkled throughout the code.