0

I'm writing a function to determine whether an element exists in a container. I don't know how to choose the function name between:

bool ContainerType::ContainsElement(const ElementType& elem);

and

bool ContainerType::DoesContainElement(const ElementType& elem);

Consider the following two scenario:

Version 1:

ContainerType coll;
ElementType   elem;
...

if (coll.ContainsElement(elem))
{
    cout << elem << " exists." << endl;
}

Version 2:

ContainerType coll;
ElementType   elem;
...

if (coll.DoesContainElement(elem))
{
    cout << elem << " exists." << endl;
}

As I understand it, I think version 1 is more like natural English. However, I also found version 2's style is used more widely.

What's your opinion?

Update:

FltIsOperationSynchronous
FltIsIoCanceled
FltIsVolumeWritable

The three function names above are excerpted from Microsft's documentation. If the prefix "Flt" are stripped, they are:

IsOperationSynchronous
IsIoCanceled
IsVolumeWritable

rather than

OperationIsSynchronous
IoIsCanceled
VolumeIsWritable

Why?

Robert Harvey
  • 168,684
  • 43
  • 314
  • 475
xmllmx
  • 33,981
  • 13
  • 121
  • 269

1 Answers1

1

contains is the most popular. containsElement could be, and I've never seen doesContainElement. Probably too long.

If you are trying to emulate English, think that bool functions are usually used with if. What sounds better:

    if( a.containsElement(b) )

    if( a.doesContainElement(b) )

    if( a.contains(b) )

? I think 3, then 1, then 2, don't you?

Mario Rossi
  • 7,231
  • 21
  • 36
  • Please review my update. I think Microsoft and many other companies seem to like 2 more. why? – xmllmx Sep 02 '13 at 03:43
  • @xmllmx One possible explanation is that by starting with an auxiliary verb (`is`, `does`, `has`, etc), it is clear that the function returns a `bool`. `contains` is not very far from it, but 1) you wouldn't use it alone in an English question, and 2) you need to reach the end to notice it's a verb, which is much longer than `is`, `does`, or `has`. – Mario Rossi Sep 02 '13 at 03:58