3 ms·
Looking at the code there are not just fewer lines, but fewer conditionals too. It's much simpler to read and understand. I got it at a glance, whereas I skimme
by eloff 6y ago
Looking at the code there are not just fewer lines, but fewer conditionals too. It's much simpler to read and understand. I got it at a glance, whereas I skimmed the typical approach and would still need to go over it more carefully to be sure it is correct.
I think Linus is correct on this one.
- bigyikes 6y agoLinus’ solution seems to add a layer of indirection, which I think is harder to grok than a simple condition.
- eloff 6y agoThere's no accounting for taste ;)
- coldtea 6y agoBut there should be ;)
- alisonkisk 6y agoThe cs101 solution adds an unecessary extra variable "prev" that you have to study to figure out what it means. That's harder for me to grok.
- lordnacho 6y agoBut there's comments that explain it, so I think it's okay. I wouldn't leave a thing that has a mix of &, *, and -> without saying what it was supposed to do, but he does explain it. I'm ok with clever code when it's explained in words.
- GuB-42 6y agoThere is no extra layer of indirection. The idea here is to make the "p" pointer point on the "next" field of the current element instead of at the structure itself. There is the same amount of indirection in both solutions. The first solution does pointer->structure->pointer, the "elegant" solution does pointer->pointer->structure. The reason the first one may be easier to read is because the C syntax "prefers" it, as it is a more common construct.
- threatofrain 6y agoIMO harder to initially acquire but easier to grok at scale. Imagine combing through the behavior of blocks of code with conditionals in a larger system.
- ewindal 6y agoConditionals are so simple it’s basically the first thing anyone learns. Anyone can understand the first approach. The latter requires knowledge that is specific to lower level languages,concepts that are certainly not taught universally. The latter approach is definitely not the easiest to understand. It is, however, what Linus considers «good taste in coding.»
- nickelpro 6y agoIf you're coding in Linus's world you're going to understand indirection, the example is excellent given the context
- coldtea 6y ago>Conditionals are so simple it’s basically the first thing anyone learns. That's neither here, nor there though. The simplicity of a concept does not directly translate to how hard it makes the code (assembly is simpler in the sense of having fewer and simpler constructs than e.g. Python but much much harder to code in). Conditionals, and the exponential increase in state space they bring, are still one of the biggest source of errors and complexity in code.
- eloff 6y agoYes, excellently put. That's exactly why Linus' code is easier to understand. The extra control flow operations really increases the time to understand if the code is correct and likewise introduces many opportunities for error.