Skip to contentSkip to conversation

strvec: use correct member name in comments

PATCH In review Linus Arver via GitGitGadget 4 participants 7 messages Jan 12, 2024 – Jan 14, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 strvec: use correct member name in comments

From: Linus Arver <[email protected]>

In d70a9eb611 (strvec: rename struct fields, 2020-07-28), we renamed the "argv" member to "v". In the same patch we also did the following rename in strvec.c:

-void strvec_pushv(struct strvec *array, const char **argv) +void strvec_pushv(struct strvec *array, const char **items)

and it appears that this s/argv/items operation was erroneously applied to strvec.h.

Rename "items" to "v".

---
strvec: use correct member name in comments

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1640%2Flistx%2Ffix-strvec-typos-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1640/listx/fix-strvec-typos-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1640

 strvec.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
strvec.h +4 -4
index 9f55c8766ba..4715d3e51f8 100644
@@ -4,8 +4,8 @@
 /**
  * The strvec API allows one to dynamically build and store
  * NULL-terminated arrays of strings. A strvec maintains the invariant that the
- * `items` member always points to a non-NULL array, and that the array is
- * always NULL-terminated at the element pointed to by `items[nr]`. This
+ * `v` member always points to a non-NULL array, and that the array is
+ * always NULL-terminated at the element pointed to by `v[nr]`. This
  * makes the result suitable for passing to functions expecting to receive
  * argv from main().
  *
@@ -22,7 +22,7 @@ extern const char *empty_strvec[];
 
 /**
  * A single array. This should be initialized by assignment from
- * `STRVEC_INIT`, or by calling `strvec_init`. The `items`
+ * `STRVEC_INIT`, or by calling `strvec_init`. The `v`
  * member contains the actual array; the `nr` member contains the
  * number of elements in the array, not including the terminating
  * NULL.
@@ -80,7 +80,7 @@ void strvec_split(struct strvec *, const char *);
 void strvec_clear(struct strvec *);
 
 /**
- * Disconnect the `items` member from the `strvec` struct and
+ * Disconnect the `v` member from the `strvec` struct and
  * return it. The caller is responsible for freeing the memory used
  * by the array, and by the strings it references. After detaching,
  * the `strvec` is in a reinitialized state and can be pushed

base-commit: a54a84b333adbecf7bc4483c0e36ed5878cac17b

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 6 lines · Linus Arver via GitGitGadget

From: Linus Arver <[email protected]>

In d70a9eb611 (strvec: rename struct fields, 2020-07-28), we renamed the "argv" member to "v". In the same patch we also did the following rename in strvec.c:

Quoted 7 lines

-void strvec_pushv(struct strvec *array, const char **argv) +void strvec_pushv(struct strvec *array, const char **items)

and it appears that this s/argv/items operation was erroneously applied to strvec.h.

Rename "items" to "v".

Good catch. The source of the problem is that the patch originally used "items" in the struct, too, but after review we settled on the more concise "v". I'd almost certainly have then flipped the name in the struct definition and relied on the compiler to help find the fallout. But of course it doesn't look in comments. :)

As you note, we still call use "items" for the vector passed in to pushv. I think that is OK, and there is no real need to use the terse "v" there (it is also purely internal; the declaration in strvec.h does not name it at all).

So this patch looks great to me. Thanks!

-Peff

#2 lore raw
Quoted 2 lines · Jeff King

The source of the problem is that the patch originally used "items" in the struct, too

Ah, that makes sense.

Quoted 4 lines

As you note, we still call use "items" for the vector passed in to pushv. I think that is OK, and there is no real need to use the terse "v" there (it is also purely internal; the declaration in strvec.h does not name it at all).

Indeed. Perhaps I should have included this in my commit message.

Side note: should we start naming the parameters in strvec.h? I would think that it wouldn't hurt at this point (as the API is pretty stable). If you think that's worth it, I could reroll to include that in this series (and also improve my commit message for this patch).

#3 lore raw
Quoted 4 lines · Linus Arver

Side note: should we start naming the parameters in strvec.h? I would think that it wouldn't hurt at this point (as the API is pretty stable). If you think that's worth it, I could reroll to include that in this series (and also improve my commit message for this patch).

I am not sure if it adds more value to outweigh the cost of churning. When the meaning of the parameters are obvious only by looking at their types, a prototype without parameter names is easier to maintain, by allowing the parameters to be renamed only once in the implementation. When the meaning of parameters are not obvious from their types, we do want them to be named so that you only have to refer to the header files to know the argument order.

"void *calloc(size_t, size_t)" would not tell us if we should pass the size of individual element or the number of elements first, and writing "void *calloc(size_t nmemb, size_t size)" to make it more obvious is a good idea.

On the other hand, "void *realloc(void *, size_t)" is sufficient to tell us that we are passing a pointer as the first parameter and the desired size as the second parameter, without them having any name.

Are there functions declared in strvec.h you have in mind that their parameters are confusing and hard to guess what they mean?

Thanks.

#4 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Linus Arver

Side note: should we start naming the parameters in strvec.h? I would think that it wouldn't hurt at this point (as the API is pretty stable). If you think that's worth it, I could reroll to include that in this series (and also improve my commit message for this patch).

Quoted 7 lines

I am not sure if it adds more value to outweigh the cost of churning. When the meaning of the parameters are obvious only by looking at their types, a prototype without parameter names is easier to maintain, by allowing the parameters to be renamed only once in the implementation. When the meaning of parameters are not obvious from their types, we do want them to be named so that you only have to refer to the header files to know the argument order.

This sounds like a good rule to me.

Quoted 8 lines

"void *calloc(size_t, size_t)" would not tell us if we should pass the size of individual element or the number of elements first, and writing "void *calloc(size_t nmemb, size_t size)" to make it more obvious is a good idea.

On the other hand, "void *realloc(void *, size_t)" is sufficient to tell us that we are passing a pointer as the first parameter and the desired size as the second parameter, without them having any name.

Thanks for the illuminating examples. Agreed.

Quoted 2 lines

Are there functions declared in strvec.h you have in mind that their parameters are confusing and hard to guess what they mean?

TBH I only learned recently (while writing the patch in this thread) that parameter names in prototypes were optional. I got a little confused initially when looking at strvec.h for the first time because none of the parameters were named. Having thought a bit more about these functions, none of them have repeated types like in your example where naming is warranted, so I think they're fine as is.

OTOH if we were treating these .h files as something meant for direct external consumption (that is, if strvec.h is libified and external users outside of Git are expected to use it directly as their first point of documentation), at that point it might make sense to name the parameters (akin to the style of manpages for syscalls). But I imagine at that point we would have some other means of developer docs (beyond raw header files) for libified parts of Git, so even in that case it's probably fine to keep things as is.

Thanks.

#5 lore raw
Quoted 8 lines · Linus Arver

OTOH if we were treating these .h files as something meant for direct external consumption (that is, if strvec.h is libified and external users outside of Git are expected to use it directly as their first point of documentation), at that point it might make sense to name the parameters (akin to the style of manpages for syscalls). But I imagine at that point we would have some other means of developer docs (beyond raw header files) for libified parts of Git, so even in that case it's probably fine to keep things as is.

I think this is mostly orthogonal to libification. Whether the audience is other parts of Git or users outside of Git, they need to know how to call the function. Our main source of documentation there is comments above the declaration (we've marked these with "/**" which would allow a parser to pull them into a separate doc file, but AFAIK in the 9 years since we started that convention, nobody has bothered to write such a script).

Naming the parameters can help when writing those comments, because you can then refer to them (e.g., see the comment above strbuf_addftime). Even without that, I think they can be helpful, but I don't think I'd bother adding them in unless taking a pass over the whole file, looking for comments that do not sufficiently explain their matching functions.

I don't doubt that some of that would be necessary for libification, just to increase the quality of the documentation. But I think it's largely separate from the patch in this thread.

-Peff

#6 lore raw
Quoted 19 lines · Jeff King
Quoted 8 lines · Linus Arver

OTOH if we were treating these .h files as something meant for direct external consumption (that is, if strvec.h is libified and external users outside of Git are expected to use it directly as their first point of documentation), at that point it might make sense to name the parameters (akin to the style of manpages for syscalls). But I imagine at that point we would have some other means of developer docs (beyond raw header files) for libified parts of Git, so even in that case it's probably fine to keep things as is.

I think this is mostly orthogonal to libification. Whether the audience is other parts of Git or users outside of Git, they need to know how to call the function. Our main source of documentation there is comments above the declaration (we've marked these with "/**" which would allow a parser to pull them into a separate doc file, but AFAIK in the 9 years since we started that convention, nobody has bothered to write such a script).

Quoted 5 lines

Naming the parameters can help when writing those comments, because you can then refer to them (e.g., see the comment above strbuf_addftime). Even without that, I think they can be helpful, but I don't think I'd bother adding them in unless taking a pass over the whole file, looking for comments that do not sufficiently explain their matching functions.

So in summary you are saying that the comments are the most important source of documentation that we have currently, and unless naming the parameters improves these comments, we shouldn't bother naming these parameters. I agree.

Quoted 3 lines

I don't doubt that some of that would be necessary for libification, just to increase the quality of the documentation. But I think it's largely separate from the patch in this thread.

I agree with both statements. Thanks.