views:

238

answers:

9

I get obsessed with the best names for arrays and variables that I use, I'll look up words in the thesaurus, dictionary, etc..

So I'm trying to name this array / structure:

$nameMe = array(
    '392' => TRUE,
    '234' => TRUE,
    '754' => TRUE,
    '464' => TRUE,
);

and it's used to check if that id has a certain property, like so

if(isset($name[$id])) {
    doSomething();
}

Problem being I'm getting really long variable names like

$propertyNameArrayIdIndexed

Any ideas for how I can better name this particular function of array? or better names in general

A: 

propertyNameable, IspropertyNameable.

Joel Coehoorn
+2  A: 

Nothing wrong with long variable names, as long as they describe what the variable is doing, rather than how it's declared or defined.

Paul Tomblin
+2  A: 

I would drop "Array" from variable names.

A function that used the array may be named something like:

IsPropertyAvailable?($id)

or just

IsAvailable?($id)

when properly encapsulated.

So the associated data structure for querying could be named

$availableIds

cfeduke
+1  A: 

You want your code to read as much like plain English as possible. In plain English you'd end up with something like;

If the car is red Do the red car stuff

So my recommendation is to avoid introducing unnecessary computerese ('array', 'property', 'index' etc.) into the naming of the variable. Your programming language is imposing "isset" on you. That's fine, that makes it clear that you have an array of booleans and means you can simply say;

if( isset(red[car_idx]) ) dosomething();

Summary: I think the array should be named simply as the property you are trying to test for. If the name of the property is a nice English language adjective that either applies to a noun or not, the boolean nature of the array is apparent even without isset(). So simply;

Red[], Oblong[], Large[]

Not IsRed[], IsOblong[], IsLarge[] because the extra "Is" in addition to the one in isset() is redundant.

Bill Forster
+4  A: 
$hasProperty[$id]

or

$isSomething[$id]

What is the property exactly?

$isOdd[$id]
$isWriteable[$id]
$hasAssociatedFile[$id]
Claudiu
A: 

is your array only going to contain true? If so, I'd say change your data structure to something like this:

$availableIds = array(392, 234, 754, 464);

and then your if statements are much more meaningful:

if (in_array($myId, $availableIds)) { ... }
nickf
that means trading amortized O(1) for O(n), not a good step if n can get big
Javier
+1  A: 

Name variables by their "role" (in the UML sense) not their type. So, the proper name for the variable should depend very much on where and how its used. Simply knowing the type of data structure is not enough to give it an apt name. So, say that you have an enumeration of properties, each of which might be renderable as an icon. I'd leave out any indication of type, and declare it something like Set<Property> displayableIcons.

Even if you are using Hungarian notation, the actual type shouldn't be part of the name, but some type-qualifier or indication of an informal sub-type would be alright, like String b64JpgMugshot.

erickson
A: 

I just use dah[].

stimpy77
A: 

I would agree with other commenters that the question seems to lack proper context for proper concise naming, but something generic like able['foo'], enabled['bar'] or ready['ack'] may work.

Adam K. Johnson