Meaning of "length", "size", and "count"

Alejandro Colomar alx@kernel.org
Wed Oct 9 20:31:43 GMT 2024


Hi Jakub,

On Wed, Oct 09, 2024 at 09:40:11PM GMT, Jakub Łukasiewicz wrote:
> On 2024-10-09 20:48 CEST, Alejandro Colomar <alx@kernel.org> wrote:
> > countof() is a new term, so it doesn't yet have a meaning (except as
> > given by the attribute), but it naturally fits more with number of
> > elements.
> 
> How would you call, for example, a function that returns how many times a
> value is contained in a data structure (be it array, linked list, or any
> other)?

list_count() or similar would be a good name.

It's length that's dangerous to overload because (1) it's already used
by strings, and (2) strings have the danger of the NUL terminator which
is not counted by its length.

But for example, it's not dangerous to misuse size for the number of
elements of an array, because they're so obviously different that you'll
not introduce a bug easily.  I think it's okay to say wcslcpy() gets a
size as the third parameter, even if pedantically it's a number of
elements.  So, using "count" for both arrays, and user-defined types
such as linked lists, is just fine.  The only _dangerous_ term is
length.


Have a lovely night!
Alex

> 
> ~ J.Ł.

-- 
<https://www.alejandro-colomar.es/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20241009/d346b0db/attachment-0001.sig>


More information about the Gcc-patches mailing list