Re: [PATCH 8/8] worktree: simplify search for unique worktree
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Sep 10, 2020, 20:01 UTC
- Message-ID
- <CAPig+cQ871p8+oQdBBY=ebDdjDWfa5NvEMroitEmk4Db8DfLvA@mail.gmail.com>
- In-Reply-To
- <CAN0heSrUWiZ_xar3G5rZG-c=8OVp5-eByS6rMXOw9wfTA8kmbA@mail.gmail.com>
On Thu, Sep 10, 2020 at 3:48 PM Martin Ågren <martin.agren@gmail.com> wrote:
Show 15 quoted lines
> On Thu, 10 Sep 2020 at 21:28, Eric Sunshine <sunshine@sunshineco.com> wrote: > > Although this change appears to be correct and does simplify the code, > > I think it also makes it a bit more opaque. With the explicit > > `nr_found == 1`, it is quite obvious that the function considers > > "success" to be when one and only one entry is found and any other > > number is failure. But with the revised code, it is harder to work out > > precisely what the conditions are. > > Thanks for commenting. I found the original trickier than it had to be. > It spreads out the logic in several places and is careful to short-cut > the loop. My first thought was "why doesn't this just use the standard > form?". But I'm open to the idea that it might be a fairly personal > standard form... If there's any risk that someone else comes along and > simplifies this to use a `nr_found` variable, then maybe file this under > code churning?
Maybe. Dunno. Even with the suggested function comment, I still find that the revised code has a higher cognitive load because the reader has to remember or figure out mentally what `found` contains by the `return found;` at the end of the function. Compare that with the old code, in which the reader doesn't have to remember or figure out anything. The final `return nr_found == 1 ? found : NULL;` condition spells out everything the reader needs to know -- even if the reader didn't pay close attention to the meat of the function. So, we each have a different take on the apparent complexity.
Show 7 quoted lines
> > Having said that, I think a simple > > comment before the function would suffice to clear up the opaqueness. > > Perhaps something like: > > > > /* If exactly one worktree matches 'target', return it, else NULL. */ > > That's a good suggestion regardless.
The function is so small that the increased cognitive load (for me) in the rewrite probably doesn't matter at all, so I don't feel strongly one way or the other. Keeping the patch (amended with the suggested comment) or dropping it are both suitable options.