# IsSomething function and variable - there a standard convention to use?

This topic is 4605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If you have a boolean function to test if something "is" something, like IsMovable(). Is there a standard convention to name the variable? Like is it: bool IsMoveable() { return m_IsMovable; } or is it bool IsMoveable() { return m_Movable; } Do you put the "is" on the variable too? Is there a standard or is it up to the programmer?

##### Share on other sites
There is no one coding standard. As long as you follow a good, consistent standard, you're on the right track.

It's really up to you.

Personally I always put the is in front of the variable too, so that it makes it blatently obvious what the variable is for. (not that it wouldn't be obvious without it, but if the function and variable have pretty much the same name, you can guess that m_isMoveable is retured by the IsMoveable method)

##### Share on other sites
For one datapoint, my style is more like:

bool isMovable { return movable; }

However, I would normally not end up writing something like this *at all*, because it usually smells like code outside the object is doing work that the object should be doing itself. E.g.

// badbool Thing::isMovable { return movable; }void Thing::Move() { /* actually move */ }if (myThing.isMovable()) { myThing.Move(); }// betterbool Thing::tryToMove() {   if (!movable) return false;   /* actually move */;  return true;}myThing.tryToMove();// if for some reason we need to do something else if not movable, we// can still check that, but we're free to ignore the return value in// the common case.

This general approach sometimes puts a little strain on clarity because of the method names ("tryToMove" doesn't really convey the idea that something will be returned), but usually results in much better encapsulation.