2 ms·
These functions can be thought of as two steps: 1. Find the thing to update 2. Update it Linus's version does just that. But the first version is more compl
by furyofantares 6y ago
These functions can be thought of as two steps:
1. Find the thing to update
2. Update it
Linus's version does just that.
But the first version is more complex because step 1 instead emits something that may or may not be the thing to update, and so step 2 has to reason about that. Additionally, it introduces a bookkeeping variable -- cur -- which becomes redundant before step 2 (by which point it's equal to target).
IMO Linus is right here. The form of his solution directly matches the problem and allows you to look at the code as two clean steps -- no reasoning about the output of the first step and no bookkeeping cruft variable that you have to ignore or remind yourself that it's the same as another variable after some point in the function. At least once you're used to working with double pointers I think that's a much easier function to read.