473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

function change - is this a poor option

bob
Hi,
I'm working on fixing issues/bugs in a legacy codebase.

I'd like to ask experts opinion on a decision I've made regarding a
function. I don't have my Scott Meyers at hand but I'm fairly sure he
mentions this issue.

consider virtual function;

void foo()
{

if (something)
{
do_x();
}
else
{
do_y();
}
}
Now for reasons we don't need to know in this post, do_x() is the
correct behaviour for the scenario I'm debugging. However as it stands
the code always goes to do_y();
I want to change the signature of the function to;

virtual void foo(bool myFlag = false);

I've also updated the other classes that override this virtual function
with the new default argument (no function hiding etc)..

Now in the code path that I'm interested in (to fix this bug), I call
foo(true). I make no changes to any other code, and so myFlag is always
false in that scenario.

The logic of the function now looks like;

void foo(bool myFlag = false)
{

if ((something) || myFlag)
{
do_x();
}
else
{
do_y();
}
}


Now this will work for me. However Scott Meyers (vaguely from memory) I
*think* says this is not a good idea. Would people out there agree? Is
this a recipe for disaster or a sound approach.

thanks much.

Graham

Feb 15 '06 #1
8 1335

"bo*@blah.com" <Gr**********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi,
I'm working on fixing issues/bugs in a legacy codebase.

I'd like to ask experts opinion on a decision I've made regarding a
function. I don't have my Scott Meyers at hand but I'm fairly sure he
mentions this issue.

consider virtual function;

void foo()
{

if (something)
{
do_x();
}
else
{
do_y();
}
}
Now for reasons we don't need to know in this post, do_x() is the
correct behaviour for the scenario I'm debugging. However as it stands
the code always goes to do_y();
I want to change the signature of the function to;

virtual void foo(bool myFlag = false);

I've also updated the other classes that override this virtual function
with the new default argument (no function hiding etc)..

Now in the code path that I'm interested in (to fix this bug), I call
foo(true). I make no changes to any other code, and so myFlag is always
false in that scenario.

The logic of the function now looks like;

void foo(bool myFlag = false)
{

if ((something) || myFlag)
{
do_x();
}
else
{
do_y();
}
}


Now this will work for me. However Scott Meyers (vaguely from memory) I
*think* says this is not a good idea. Would people out there agree? Is
this a recipe for disaster or a sound approach.

thanks much.

Graham


The problem with this scenario is that after you're done with the debugging
and fixed the function, your function foo() is now going to have a bool
parm. Any programmer coming along later (or even you later) are going to
wonder why the heck you have this bool parmater that isn't used.

If you're going to make sure to clean the function back up, it shouldn't be
a problem, except for the fact that 1 time out of 10 using this method
you'll forget. The problem then becomes you have code that makes no real
sense (why pass a bool parm that isn't used?) and someone is going to spend
time trying to figure out why before removing it, or they'll just leave it
alone for the next programmer to spend time trying to figure out.

If you insist on doing it this way, at least make the bool name indicative
of what it's there for.

void foo(bool ForDebugUseOnly = false)
Feb 15 '06 #2
Why can't you create THREE functions:
1) void foo() - orginal one, calls fooImpl
2) void mySpecificFoo() - with new hacked logic, calls fooImpl
3) void fooImpl(bool myFlag) - legacy code + tricky new flag.

Feb 15 '06 #3

bo*@blah.com wrote:
Now for reasons we don't need to know in this post, do_x() is the
correct behaviour for the scenario I'm debugging. However as it stands
the code always goes to do_y();
I want to change the signature of the function to;

virtual void foo(bool myFlag = false);


Use the debugger. Is "something" a variable? If not make in one.
Then break after assignment to that variable or just before the if and
assign true to that variable. Then "continue" and there you go.

Feb 15 '06 #4
In article <11*********************@g44g2000cwa.googlegroups. com>,
"bo*@blah.com" <Gr**********@gmail.com> wrote:
Hi,
I'm working on fixing issues/bugs in a legacy codebase.

I'd like to ask experts opinion on a decision I've made regarding a
function. I don't have my Scott Meyers at hand but I'm fairly sure he
mentions this issue.

consider virtual function;

void foo()
{

if (something)
{
do_x();
}
else
{
do_y();
}
}
Now for reasons we don't need to know in this post, do_x() is the
correct behaviour for the scenario I'm debugging. However as it stands
the code always goes to do_y();
Change the code to call do_y() directly?
Fix the code so that 'something' is set correctly in all cases?

I want to change the signature of the function to;

virtual void foo(bool myFlag = false);
Do you really want to give every client the ability to bypass
'something' and call 'do_y' like that? If so, make do_y() public and let
them call it.
Now this will work for me. However Scott Meyers (vaguely from memory) I
*think* says this is not a good idea. Would people out there agree? Is
this a recipe for disaster or a sound approach.


I'm not sure what Scott Meyers would say, but I don't like it.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 15 '06 #5
bo*@blah.com wrote:

[ ... ]
consider virtual function;

void foo()
{

if (something)
{
do_x();
}
else
{
do_y();
}
}
[ ... ]
I want to change the signature of the function to;

virtual void foo(bool myFlag = false);

I've also updated the other classes that override this virtual function
with the new default argument (no function hiding etc)..

Now in the code path that I'm interested in (to fix this bug), I call
foo(true). I make no changes to any other code, and so myFlag is always
false in that scenario.


There are a couple of concerns here.

One is about the basic structure of the program. You seem to want foo()
to do two fundamentally different things under different circumstances.
If you basically want the ability to call a portion of foo() directly,
you should probably restructure that part into a function of its own,
and then call it directly when needed, and have foo() call it when it
needs to. In this case, at least as you've shown the code here,
foo(true) is equivalent to calling do_x(), so it would almost certainly
be better to call do_x directly when/if that's what you really want. I
realize you may have simply condensed whatever was really into the if
statement and represented it as do_x, but if so, you should simply
create a do_x (with a suitable name, of course) and use it when that's
what you really want.

The second issue is with using a bool as a parameter. You might really
need (or at least want) to keep the code inside of foo (e.g. if foo
does other things before and/or after calling do_x or d_y, and it would
be difficult to separate out into a number of separate functions). In
this case, leaving the code as a single function may be perfectly
reasonable -- but you can almost certainly come up with something more
description than a bool to describe what it does in each case. If you
can represent the cases as a bool, then you can also represent them as
an enum with two possible values. The difference is that you can give
your enum a more meaningful name. Consider something like:

struct disk {
virtual void write(bool x = false) {
if (x || something)
write_to_disk();
else
write_bypassing_cache();
}

And then consider instead:

struct disk {
enum caching { cache, direct };

virtual void write(enum caching type = cache) {
if (type == cache || something) {
write_to_disk();
else
write_bypassing_cache();
}
};

Seeing a call to 'x.write(disk::direct)' gives me a fair idea of what
it's actually doing and what the parameter means. A call to
'x.write(true)' is far less informative.

--
Later,
Jerry.

Feb 15 '06 #6

"bo*@blah.com" <Gr**********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi,
I'm working on fixing issues/bugs in a legacy codebase.

I'd like to ask experts opinion on a decision I've made regarding a
function. I don't have my Scott Meyers at hand but I'm fairly sure he
mentions this issue.

consider virtual function;

void foo()
{

if (something)
{
do_x();
}
else
{
do_y();
}
}
Now for reasons we don't need to know in this post, do_x() is the
correct behaviour for the scenario I'm debugging. However as it stands
the code always goes to do_y();
I want to change the signature of the function to;

virtual void foo(bool myFlag = false);

I've also updated the other classes that override this virtual function
with the new default argument (no function hiding etc)..

Now in the code path that I'm interested in (to fix this bug), I call
foo(true). I make no changes to any other code, and so myFlag is always
false in that scenario.

The logic of the function now looks like;

void foo(bool myFlag = false)
{

if ((something) || myFlag)
{
do_x();
}
else
{
do_y();
}
}


Now this will work for me. However Scott Meyers (vaguely from memory) I
*think* says this is not a good idea. Would people out there agree? Is
this a recipe for disaster or a sound approach.


Is this just for debugging purposes? If so, then the suggest from
"roberts.noah" is good.

If you want another way to run a specific test, without using the debugger,
you could use the preprocessor, like this:
#ifdef FOR_TESTING_FOO
if (true)
#else
if (something)
#endif
{
do_x();
}
else
{
do_y();
}
#endif

(I don't know what Mr. Meyers might have to say about your method, because
you haven't provided sufficient info to know what the topic he'd be
addressing is.)

-Howard

Feb 15 '06 #7

"Howard" <al*****@hotmail.com> wrote in message
news:ml********************@bgtnsc05-news.ops.worldnet.att.net...


Is this just for debugging purposes? If so, then the suggest from
"roberts.noah" is good.

That should be "suggestion", not "suggest", naturally.
If you want another way to run a specific test, without using the
debugger, you could use the preprocessor, like this:
#ifdef FOR_TESTING_FOO
if (true)
#else
if (something)
#endif
{
do_x();
}
else
{
do_y();
}
#endif


Oh, and then simply define FOR_TESTING_FOO (in your project or command line
or whatever you use) when you want to force the do_x() behavior.

-Howard

Feb 15 '06 #8
bob
thanks for the responses. I think I should make more explicit the fact
that the logic contained in the code is *correct* when called from
elsewhere within the codebase. Under a certain scenario (i.e. for the
bug I fix) I need the code to call do_x(). i.e. When *I* call foo(bool
MyFlag =false ) I will do so using foo(true). All other *existing* code
will call foo() and therefore get the behaviour thats currently in
place. I need to make sure I call do_x()..... the "something" condition
is actually some very heavy quants mathematics dependent variable which
is greek to me (geddit... the greeks:).
One is about the basic structure of the program. You seem to want foo()
to do two fundamentally different things under different circumstances.


You've grasped the issue here. I want it to behave EXACTLY as it is
EXCEPT when I call it from my bugfix code. Hence the approach taken.
When the function is called from the codepath I am fixing, "something"
is false. I need to have do_x() invoked when this "something" is false.
You see where I'm coming from.

Anyways, I think I have got the behaviour I need. The solution works as
desired. I've not been informed of any "side" effects of the addition
of the default argument so I am happy enough. The defaulted argument is
well documented and it works fine.

thanks for the responses.

Graham

Feb 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
3
by: Gary Besta | last post by:
I am trying to add a simple case statement to a stored procedure or user defined function. However when I try and save the function/procedure I get 2 syntax errors. Running the query in query...
3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
36
by: lauren quantrell | last post by:
I'm hoping someone can tell me if there is any performance benefit in my applications between distinguishing a code routine as a Function or a Sub or if there is a performance benefit between...
6
by: hharry | last post by:
Hello All, Is it possible to force a function argument to fall with a range of values ? For example: I have a function which searches a database table and accepts a query type argument...
1
by: devine | last post by:
Hi All, I have a form which checks which Submit button has been pressed and also shows a textarea dependant on an option selected. The problem I have is that when I include my "display text"...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
3
by: Hanoodah | last post by:
Good morning all, Please I need help in a form that I have designed to fill user information. This form needs two drop-down list about specific date, one about contract date, and the other about...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.