Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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.


// bad
bool Thing::isMovable { return movable; }
void Thing::Move() { /* actually move */ }

if (myThing.isMovable()) { myThing.Move(); }

// better
bool 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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this