Re: [PATCH 06/19] reset.c: remove unnecessary variable 'i'
- From
Martin von Zweigbergk <martinvonz@gmail.com>
- Date
- Jan 10, 2013, 08:41 UTC
- Message-ID
- <CANiSa6jJcYRUyswSktq86pWttxBjZedZcPheRgPCf+EubC_kLg@mail.gmail.com>
- In-Reply-To
- <7vpq1e5ent.fsf@alter.siamese.dyndns.org>
On Wed, Jan 9, 2013 at 11:39 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 21 quoted lines
> Martin von Zweigbergk <martinvonz@gmail.com> writes: > >> Throughout most of parse_args(), the variable 'i' remains at 0. In the >> remaining few cases, we can do pointer arithmentic on argv itself >> instead. >> --- >> This is clearly mostly a matter of taste. The remainder of the series >> does not depend on it in any way. > > I agree that it indeed is a matter of taste between > > (1) look at av[i], check with (i < ac) for the end, and increment i to > iterate over the arguments; and > > (2) look at av[0], check with (0 < ac) for the end, and increment > av and decrement ac at the same time to iterate over the > arguments. > > When (ac, av) appear as a pair, however, adjusting only av without > adjusting ac is asking for future trouble. It violates a common > expectation that av[ac] points at the NULL at the end of the list.
Good points.
> If a code chooses to use !av[0] as the terminating condition and > never looks at ac, then incrementing only av is fine, but in such a > case, the function probably should lose ac altogether.
Makes sense. I've picked this style for now (i.e. dropped both 'i' and 'argc'). I was surprised by the style that referred to the variable in many places where it was know to be 0, but I'm no experienced C programmer, so if that's a common practice when it comes to argument parsing, I'm also happy to drop the patch. Let me know what you prefer.