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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • -Lost

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

    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_Da ta;
    }
    function TypeCheck()
    {
    if (HorribleCode_D ata == '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.
  • Erwin Moller

    #2
    Re: Is it possible to end the outermost function from an inner function?


    -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_Da ta;
    }
    function TypeCheck()
    {
    if (HorribleCode_D ata == '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("Horr iblecode 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/
    =============== =============

    Comment

    • =?ISO-8859-13?Q?Kristaps_K=FBlis?=

      #3
      Re: Is it possible to end the outermost function from an innerfunction?

      On Sep 30, 12:41 pm, Erwin Moller
      <Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
      -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_Da ta;
      }
      function TypeCheck()
      {
        if (HorribleCode_D ata == '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("Horr iblecode 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.

      Comment

      • C. (http://symcbean.blogspot.com/)

        #4
        Re: Is it possible to end the outermost function from an innerfunction?

        On 30 Sep, 12:21, Kristaps Kûlis <Kristaps.Ku... @gmail.comwrote :
        On Sep 30, 12:41 pm, Erwin Moller
        >
        >
        >
        <Since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
        -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_Da ta;
        }
        function TypeCheck()
        {
        if (HorribleCode_D ata == '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("Horr iblecode 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.

        Comment

        • -Lost

          #5
          Re: Is it possible to end the outermost function from an inner function?

          Response to Erwin Moller
          <Since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om>:

          <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("Horr iblecode 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.

          Comment

          • -Lost

            #6
            Re: Is it possible to end the outermost function from an inner function?

            Response to "C. (http://symcbean.blogsp ot.com/)"
            <colin.mckinnon @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.

            Comment

            • Erwin Moller

              #7
              Re: Is it possible to end the outermost function from an inner function?


              -Lost schreef:
              Response to "C. (http://symcbean.blogsp ot.com/)"
              <colin.mckinnon @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_Da ta;
              }

              Q1: What do you mean by "end" horribleCode()? Must the program terminate
              with exit()?
              Q2: What do expect TypeCheck does? Does it judge "HorribleCode_D ata"
              somehow on validity?


              function TypeCheck()
              {
              if (HorribleCode_D ata == 'bad')
              {
              return GoodCode_Data;
              }
              }

              Q3: If HorribleCode_Da ta == '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:



              $possibleHorrib leDataStructure = .....

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

              function test_horribleCo de($something){
              // do stuff with $something, eg:
              // We expect that $something has an element named "test"
              if (!isset($someth ing["test"])){
              throw new Exception('Prob lem: 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_horribleCo de() returns true if the passed thing is valid:
              $succes = @test_horribleC ode($possibleHo rribleDataStruc ture);
              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/
              =============== =============

              Comment

              • -Lost

                #8
                Re: Is it possible to end the outermost function from an inner function?

                Response to Erwin Moller
                <Since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om>:
                ><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
                >understandin g 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_Da ta;
                }
                >
                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_D ata" somehow on validity?
                >
                >
                function TypeCheck()
                {
                if (HorribleCode_D ata == '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_Da ta == '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_Da ta 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.

                Comment

                Working...