By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Is it possible to end the outermost function from an inner function?

P: n/a
Is it possible to end the outermost function from an inner
function?

I've been tasked with implementing a feature check in some existing
code and I wondered if this could be done:

function HorribleCode()
{
TypeCheck(); // if GoodCode_Data is returned, "end" HorribleCode
()
return HorribleCode_Data;
}
function TypeCheck()
{
if (HorribleCode_Data == 'bad')
{
return GoodCode_Data;
}
}

Is this possible without implementing an if-else within
HorribleCode()?

For the, "Why would you want to do this?" crowd, I don't per se,
I'm just curious if it can be done and if so, how?

Thanks for your help!

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Sep 30 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a

-Lost schreef:
Is it possible to end the outermost function from an inner
function?

I've been tasked with implementing a feature check in some existing
code and I wondered if this could be done:

function HorribleCode()
{
TypeCheck(); // if GoodCode_Data is returned, "end" HorribleCode
()
return HorribleCode_Data;
}
function TypeCheck()
{
if (HorribleCode_Data == 'bad')
{
return GoodCode_Data;
}
}

Is this possible without implementing an if-else within
HorribleCode()?

For the, "Why would you want to do this?" crowd, I don't per se,
I'm just curious if it can be done and if so, how?

Thanks for your help!
Hi,

Maybe have a look at try/catch.
Make sure an exception/error occurs when the check fails.
Maybe throw it yourself:
throw new Exception("Horriblecode found a problem");

When you don't catch the exception, all was fine.
Regards,
Erwin Moller
--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Sep 30 '08 #2

P: n/a
On Sep 30, 12:41*pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
-Lost schreef:
Is it possible to end the outermost function from an inner
function?
I've been tasked with implementing a feature check in some existing
code and I wondered if this could be done:
function HorribleCode()
{
* TypeCheck(); // if GoodCode_Data is returned, "end" HorribleCode
()
* return HorribleCode_Data;
}
function TypeCheck()
{
* if (HorribleCode_Data == 'bad')
* {
* * return GoodCode_Data;
* }
}
Is this possible without implementing an if-else within
HorribleCode()?
For the, "Why would you want to do this?" crowd, I don't per se,
I'm just curious if it can be done and if so, how?
Thanks for your help!

Hi,

Maybe have a look at try/catch.
Make sure an exception/error occurs when the check fails.
Maybe throw it yourself:
throw new Exception("Horriblecode found a problem");

When you don't catch the exception, all was fine.

Regards,
Erwin Moller
--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why?http://improve-usenet.org/
============================
exceptions are costly - you should not use them for something like
invalid user input, but just in cases something really went wrong, for
example, when database connection fails.
Sep 30 '08 #3

P: n/a
On 30 Sep, 12:21, Kristaps KŻlis <Kristaps.Ku...@gmail.comwrote:
On Sep 30, 12:41 pm, Erwin Moller

<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
-Lost schreef:
Is it possible to end the outermost function from an inner
function?
I've been tasked with implementing a feature check in some existing
code and I wondered if this could be done:
function HorribleCode()
{
TypeCheck(); // if GoodCode_Data is returned, "end" HorribleCode
()
return HorribleCode_Data;
}
function TypeCheck()
{
if (HorribleCode_Data == 'bad')
{
return GoodCode_Data;
}
}
Is this possible without implementing an if-else within
HorribleCode()?
For the, "Why would you want to do this?" crowd, I don't per se,
I'm just curious if it can be done and if so, how?
Thanks for your help!
Hi,
Maybe have a look at try/catch.
Make sure an exception/error occurs when the check fails.
Maybe throw it yourself:
throw new Exception("Horriblecode found a problem");
When you don't catch the exception, all was fine.
Regards,
Erwin Moller
--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why?http://improve-usenet.org/
============================

exceptions are costly - you should not use them for something like
invalid user input, but just in cases something really went wrong, for
example, when database connection fails.
Certainly, the right way to do it is by returning an appropriate value
(or returning and setting and setting a pass-by-reference parameter).
You can have multiple return statements in a function.

C.
Sep 30 '08 #4

P: n/a
Response to Erwin Moller
<Si******************************************@spam yourself.com>:

<snip>
Maybe have a look at try/catch.
Make sure an exception/error occurs when the check fails.
Maybe throw it yourself:
throw new Exception("Horriblecode found a problem");

When you don't catch the exception, all was fine.
Neat idea for sure, but I'm afraid that would introduce a fair
amount of overhead -- not something I want to do as a first
impression.

I would like to ask though, how would a single try-catch in a
function call be leveraged to cease an outermost function?

I can visualize it as code placed within each function that I need
to verify the data of, but not as a single function instance with
only calls to said function (in each bad function). I hope that
made sense.

Thanks for the input!

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Sep 30 '08 #5

P: n/a
Response to "C. (http://symcbean.blogspot.com/)"
<co************@gmail.com>:

<snip>
Certainly, the right way to do it is by returning an appropriate
value (or returning and setting and setting a pass-by-reference
parameter). You can have multiple return statements in a
function.
Would mind giving me an example? I'm having a difficult time
understanding what you mean.

Thank you!

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Sep 30 '08 #6

P: n/a

-Lost schreef:
Response to "C. (http://symcbean.blogspot.com/)"
<co************@gmail.com>:

<snip>
>Certainly, the right way to do it is by returning an appropriate
value (or returning and setting and setting a pass-by-reference
parameter). You can have multiple return statements in a
function.

Would mind giving me an example? I'm having a difficult time
understanding what you mean.

Thank you!
Hi Lost,

Well, I could give you some fantasycode since I don't know your current
code of course.
But before I can can you an example with try/catch I must understand
what you expect your code to do excactly, see questions below:
function HorribleCode()
{
// if GoodCode_Data is returned, "end" HorribleCode()
TypeCheck();
return HorribleCode_Data;
}

Q1: What do you mean by "end" horribleCode()? Must the program terminate
with exit()?
Q2: What do expect TypeCheck does? Does it judge "HorribleCode_Data"
somehow on validity?
function TypeCheck()
{
if (HorribleCode_Data == 'bad')
{
return GoodCode_Data;
}
}

Q3: If HorribleCode_Data == 'bad', Why do you return GoodCode_Data?
Is that a modification on the bad_data so that it is good now?
Here is an example of try/catch.
You can find much more here:
http://nl3.php.net/manual/en/language.exceptions.php
$possibleHorribleDataStructure = .....

try {
test_horribleCode($possibleHorribleDataStructure);
} catch (Exception $e) {
echo 'Problem with HorribleCode: ', $e->getMessage();
// exit? Up to you how to handle this
}

function test_horribleCode($something){
// do stuff with $something, eg:
// We expect that $something has an element named "test"
if (!isset($something["test"])){
throw new Exception('Problem: test is missing!');
}
}

You can nest functions like this, as long as you throw an exception and
catch it in your main code.

Alternatively, you can use errorsupression, and test for succes later.
That would look like:

$succes = true;
// suppose test_horribleCode() returns true if the passed thing is valid:
$succes = @test_horribleCode($possibleHorribleDataStructure) ;
if (!succes){
// handle it.
}

from: http://us.php.net/operators.errorcontrol
Error Control Operators

PHP supports one error control operator: the at sign (@). When prepended
to an expression in PHP, any error messages that might be generated by
that expression will be ignored.

If the track_errors feature is enabled, any error message generated by
the expression will be saved in the variable $php_errormsg. This
variable will be overwritten on each error, so check early if you want
to use it.

Hope that helps.

Regards,
Erwin Moller
--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
Oct 1 '08 #7

P: n/a
Response to Erwin Moller
<Si******************************************@spam yourself.com>:
><snip>
>>Certainly, the right way to do it is by returning an
appropriate value (or returning and setting and setting a
pass-by-reference parameter). You can have multiple return
statements in a function.

Would mind giving me an example? I'm having a difficult time
understanding what you mean.

Thank you!
Well, I could give you some fantasycode since I don't know your
current code of course.
But before I can can you an example with try/catch I must
understand what you expect your code to do excactly, see
questions below:
function HorribleCode()
{
// if GoodCode_Data is returned, "end" HorribleCode()
TypeCheck();
return HorribleCode_Data;
}

Q1: What do you mean by "end" horribleCode()? Must the program
terminate with exit()?
Terminate only the rest of HorribleCode(). The application should
continue on with the data returned by TypeCheck() (GoodCode_Data)
as if HorribleCode had returned it.
Q2: What do expect TypeCheck does? Does it judge
"HorribleCode_Data" somehow on validity?
function TypeCheck()
{
if (HorribleCode_Data == 'bad')
{
return GoodCode_Data;
}
}
Exactly, yes. The existing code basically checks for the existence
of non-existing data and attempts an incorrect parsing of that
data.

We need to check for the RIGHT data and apply the RIGHT parsing
algorithm to said data.

Return that data to the wrapper and cease the rest of the wrapper.

They gave me the added task of not destroying the rest of the
wrapper... which is what made me curious about my (inner) function
ending the (outer) wrapper function.
Q3: If HorribleCode_Data == 'bad', Why do you return
GoodCode_Data? Is that a modification on the bad_data so that it
is good now?
It is actually parsing HorribleCode_Data into GoodCode_Data then
appending MORE data and finally returning it unparsed for use
elsewhere.
Here is an example of try/catch.
<snip>
Alternatively, you can use errorsupression, and test for succes
later. That would look like:
<snip>

Thanks, I'll try both of these suggestions. I was hoping for a
solution where I could simply place the call to my method inside
the troublesome code and alter the output of said troublesome code,
but I'm starting to like your suggestion of throwing an Exception
so to speak. The application has a custom Error class that I might
be able to extend.

Thanks for ALL of your feedback and persistence in this matter.
You are most helpful as are your insights.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Oct 1 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.