[net.lang] The Naming of Arrays

dick@tjalk.UUCP (Dick Grune) (06/02/86)

This may not be the most important thing in the world, but I like
the names of entities in my program to be so clear that I can still
tell what I meant by them a year from now. (Sounds good, doesn't it?)

Now arrays always give me some problems.  Say I need an array of
words; first I intuitively declare them as (examples are from
C, but you get the same in Pascal, Ada or WhatNot :-)

struct word words[N_WORDS];

but then I need the n-th element, I write   words[n]   and I think
it sounds funny, since I do not want  "words n"  but  "word n", not
the n-th words, but the n-th word.  So I change the declaration to

struct word word[N_WORDS];

which now looks funny, but then there is only one of them, and I can
now freely write   word[4]  .  (I agree that this account rambles,
but I do not write this to get an A in journalism.)

After a while, however, I begin to regret that I have used up my
precious singular form of the word for a relatively little used
item, the array.  I can no longer write locally

	for (i = 0; i < N_WORDS; i++)	{
		struct word word = word[i];
		...
	}
and I resort to constructions like
		struct word wordi = word[i];
or
		struct word wrd = word[i];

Lately I've been using

struct word word_array[N_WORDS];   and
		struct word word = word_array[i];

but I'm not really enthousiastic either.

To my feeling this gets worse if the plural is irregular: children[4]
looks a lot more odd to me than child[4].  I think it is even worse in
languages where the formation of plurals is more prominent than in
English;  the German form   "Woerter[4]"  is absurd compared to
"Wort[4]"  (I think).

Has anybody out there given this some explicit thought?  How do YOU
name your arrays?
					Dick Grune
					Vrije Universiteit
					de Boelelaan 1081
					1081 HV  Amsterdam
					the Netherlands

david@ztivax.UUCP (06/04/86)

>How do YOU name your arrays?

like any respectable C programmer: append a "v", or use a
pointer, with the first character sort of meaningful, and the second
and last a "p".

int cv[N], *cp;		/* Natural, yes? */

aglew@ccvaxa.UUCP (06/04/86)

~> Dick Grune and the naming of arrays

I, too, have been annoyed by how to name arrays.
I have found that the best thing to do is to use array names incorporating
the singular name of some composite, usually a composite proximate to the
intended use.

Words that are useful are `list', `table', `collection', `group', and, even,
`array'. Although I've never liked naming a thing in terms of its data
structure; it seems too LISPish (harold-and-maude-alist).

Therefore 

    struct word wordlist[99];
    struct word word;
    word = wordlist[i];

I usually find the same thing goes for types as well - I might use struct
wordnode rather than word above.

lied@ihlts.UUCP (Bob Lied) (06/08/86)

You're right: it's not the most important thing in the
world, but I agree that naming is sometimes annoying in
hindsight. I partially solved the problem for myself by
taking on a few conventions:

0)  Give up trying to make it sound like natural language.
It isn't.  It may sound funny when you read it; so what.

1)  Nothing is ever plural.  This also goes for file
and directory names.  Saves hundreds of valuable milliseconds
every day wondering whether there should be an 's',
and often saves one of those precious 8 (or 14) characters.
So what if it doesn't read like English?

2)  Very local variables (a small function or a block)
get single letter names:  struct word w

3)  Instances of structures always get the shortest
reasonable names, preferably one character.  After all
the dots and ->'s are added, they always end up too
long anyway.

	Bob Lied	ihnp4!ihlts!lied