overloading vs. default argument question

P

perkins45

So lets say I have this pretend function that compares a given serial
number against the one true serial VALID_SERIAL:

bool validateSerial(string serialNum)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}


I want to add a function that does the same thing, but will also pad
the given serialNum with a prefix of "0" if the original serialNum
does not check out. I was thinking I could change the original
function to something like:

bool validateSerial(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
else
{
// padding functionality...unimportant for now
}
}


but that would seems ugly to have one function that should be really
be two. I could also overload it with a new function like

bool validateSerial(string serialNum, bool padFlag)
{
//padding functionality
}

but that function would not even use the padFlag variable.




Any suggestions on how I should organize these public functions from
an OO perspective?

Thanks
 
P

pasalic.zaharije

(e-mail address removed) je napisao:
So lets say I have this pretend function that compares a given serial
number against the one true serial VALID_SERIAL:

bool validateSerial(string serialNum)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}


I want to add a function that does the same thing, but will also pad
the given serialNum with a prefix of "0" if the original serialNum
does not check out. I was thinking I could change the original
function to something like:

bool validateSerial(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
else
{
// padding functionality...unimportant for now
}
}


but that would seems ugly to have one function that should be really
be two. I could also overload it with a new function like

bool validateSerial(string serialNum, bool padFlag)
{
//padding functionality
}

but that function would not even use the padFlag variable.




Any suggestions on how I should organize these public functions from
an OO perspective?

Thanks
From OO perspective both forms are same. I will probalby use

bool validateSerial(string serialNum, bool padFlag = false)

and implement both functionality in private methodes. But, this is
just main opinion, not some guide.

Best,
Zaharije Pasalic
 
G

Gavin Deane

So lets say I have this pretend function that compares a given serial
number against the one true serial VALID_SERIAL:

bool validateSerial(string serialNum)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}

I want to add a function that does the same thing, but will also pad
the given serialNum with a prefix of "0" if the original serialNum
does not check out. I was thinking I could change the original
function to something like:

bool validateSerial(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
else
{
// padding functionality...unimportant for now
}
}

That function doesn't do what you say. Your words say you want to pad
if the serial number doesn't check out, but your code says you want to
pad if the padFlag is set. Which is it? If the words are right and the
code is wrong, should the code really be this?

bool validateSerial(string serialNum, bool padFlag = false)
{
if(serialNum == VALID_SERIAL)
return true;
else
{
if (padFlag)
{
// padding functionality...unimportant for now
}
return false;
}
}

That seems to me to be what your words described, and seems tidy
enough. I'd consider putting the padding functionality in a function
of its own if it's anything more than a few lines.

On the other hand, if it's your code that's right and your words that
are wrong, then this would seem to do what you need:

bool validateSerial(const string& serialNum)
{
return serialNum == VALID_SERIAL
}

bool someFunctionName(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
return validateSerial(string serialNum)
}
else
{
// padding functionality...unimportant for now
// but it will need to return a bool somehow
}
}

Again, the padding functionality could go in its own function. This
seems to do the same as your code above, but I am struggling to think
of a name for someFunctionName because padding and validating seem
like totally different things that should not be in the same function
at all.

One final point - I feel I may have missed something as throughout all
this you are passing your string by value so any changes you make
during padding will only affect the local copy, not the string passed
by the caller. Should the string be being passed by reference?

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top