PDA

View Full Version : [SOLVED] naming convention for child's version of a function



jmartrican
July 16th, 2011, 03:55 AM
Hello,

Lets say I have a parent class named UpgradableAbility. And it has an abstract function named upgradeToNextLevel. This function has to check if the max level has been reached, if so it returns false.

Well I figured it would be cool if the parent function would do the max-level check so that the children do not have to do it. So the parent would have a non-abstract version of the function that would do the check, and if the check passes, would then call the abstract function. The children then do not have to implement this check

So my question is what should I name the child's version of the function (the abstract one) if the parent one is called "upgradeToNextLevel"? Is there a naming convention?

I was thinking of naming the abstract version "_upgradeToNextLevel" or "upgradeToNextLevelAbstract".

This is really not a big deal I just want to know if there is a naming convention that covers this.

thanks
jose

ve4cib
July 16th, 2011, 04:53 AM
One possible way of doing this would be as follows:



abstract class Parent
{
public boolean upgradeToNextLevel()
{
if (level < MAX_LEVEL)
{
level++;
return true;
}
return false;
}

...
}

class Child extends Parent
{
public boolean upgradeToNextLevel()
{
if(super.upgradeToNextLevel())
{
// do whatever needs to be done when the level was gained
}
else
{
// we were already at max level
}
}
}

You keep the standard behaviour in the superclass, and use polymorphism and inheritance to make use of it in the child classes.

Not sure if that answers the question, but it might give you something to think about as an alternative.

GeneralZod
July 16th, 2011, 06:44 AM
Prefixing with "do" is a common naming convention for this situation:

http://www.oodesign.com/template-method-pattern.html

PaulM1985
July 16th, 2011, 08:31 AM
Since the child functions are virtual functions, I have seen these prefixed with "V" or "Vf" before.

Paul

jmartrican
July 16th, 2011, 12:17 PM
Wow three valid answers. Thanks guys.

ve4cib, pretty slick man. I think I even did this before.... duh.


thanks
jose