Loading ...
Sorry, an error occurred while loading the content.

Re: Circular Function Definitions

Expand Messages
  • Jordan
    I think the confusion here is that you shouldn t be using the circular function pattern you are using. In addition, you are using function declarations
    Message 1 of 17 , Jan 5, 2011
    • 0 Attachment
      I think the confusion here is that you shouldn't be using the "circular function" pattern you are using. In addition, you are using function declarations rather than assigning a function statement to a variable.

      What you want to do, it seems, is immediately run requestCommentary after a matchIsStillRunning response comes back from the server.

      What I can think of off the top of my head is:

      var onCommentaryReceived = function (commentary, matchIsStillRunning) {
      // write live commentary: ...
      if (matchIsStillRunning) {
      sendToServer('Berlin:Munich', onCommentaryReceived);
      }
      },
      requestCommentary = function () {
      return onCommentaryReceived(null, true);
      };

      Alternatively, and with less refactoring:
      var onCommentaryReceived, requestCommentary;
      onCommentaryReceived = function (commentary, matchIsStillRunning) {
      // write live commentary: ...
      if (matchIsStillRunning) {
      requestCommentary();
      }
      };
      requestCommentary = function () {
      sendToServer('Berlin:Munich', onCommentaryReceived);
      };

      --- In jslint_com@yahoogroups.com, "Felix E. Klee" <felix.klee@...> wrote:
      >
      > On Tue, Jan 4, 2011 at 7:35 PM, Erik Eckhardt <erik@...>
      > wrote:
      > > no extra Boolean parameter is needed to indicate whether to call a
      > > hard-coded function name.
      >
      > There may be a misunderstanding.
      >
      > Thus, for your pleasure, below a more real-life example. Asides from
      > being more verbose, the only difference to my original example is the
      > addition of the parameter "liveCommentary". Together with the flag
      > "matchIsStillRunning" (formerly: "moreToDo") it forms the data returned
      > from the server.
      >
      > var sendToServer; // defined elsewhere
      >
      > function onCommentaryReceived(commentary, matchIsStillRunning) {
      > // write live commentary: ...
      > if (matchIsStillRunning) {
      > requestCommentary();
      > }
      > }
      >
      > function requestCommentary() {
      > sendToServer('Berlin:Munich', onCommentaryReceived);
      > }
      >
      > Naturally, one could rewrite this:
      >
      > var sendToServer; // defined elsewhere
      >
      > function requestCommentary() {
      > sendToServer('Berlin:Munich',
      > function (commentary, matchIsStillRunning) {
      > // write live commentary: ...
      > if (matchIsStillRunning) {
      > requestCommentary();
      > }
      > });
      > }
      >
      > The second form avoids the circular function definition, but is it more
      > readable?
      >
    • Felix E. Klee
      ... The question is: Why? Is that considered bad practice? If so, a reference please. ... See my original post. There I mentioned that I would like to enforce
      Message 2 of 17 , Jan 6, 2011
      • 0 Attachment
        On Thu, Jan 6, 2011 at 6:27 AM, Jordan <ljharb@...> wrote:
        > I think the confusion here is that you shouldn't be using the
        > "circular function" pattern you are using.

        The question is: Why? Is that considered bad practice? If so, a
        reference please.

        > In addition, you are using function declarations rather than assigning
        > a function statement to a variable.

        See my original post. There I mentioned that I would like to enforce
        that convention for the whole project using JSLint. Only I wonder why
        there is no such option.

        > What I can think of off the top of my head is:
        >
        > [...]
        > requestCommentary = function () {
        > return onCommentaryReceived(null, true);
        > };

        That would work, but it's confusing to read. At least the function name
        "onCommentaryReceived" should be changed.

        > Alternatively, and with less refactoring:

        See my original post. It's there already.
      • Felix E. Klee
        On Fri, Dec 31, 2010 at 6:33 PM, Felix E. Klee ... Just figured that this is akin to: http://en.wikipedia.org/wiki/Mutual_recursion And I
        Message 3 of 17 , Jan 8, 2011
        • 0 Attachment
          On Fri, Dec 31, 2010 at 6:33 PM, Felix E. Klee <felix.klee@...>
          wrote:
          > If I write code such as the following

          Just figured that this is akin to:

          http://en.wikipedia.org/wiki/Mutual_recursion

          And I checked: The latest version of JSLint, 2011-01-06, still
          complains about an undefined function.
        • Douglas Crockford
          ... ` var sendToServer; ` ` function f() { ` sendToServer( xyz , function (moreToDo) { ` // do something ... ` if (moreToDo) { `
          Message 4 of 17 , Jan 8, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Felix E. Klee" <felix.klee@...> wrote:
            >
            > If I write code such as the following, then JSLint complains that "f" is
            > not defined.
            >
            > var sendToServer;
            >
            > function callback(moreToDo) {
            > // do something ...
            > if (moreToDo) {
            > f();
            > }
            > }
            >
            > function f() {
            > sendToServer('xyz', callback);
            > }
            >
            > What's the most elegant solution to get rid of the error message?


            ` var sendToServer;
            `
            ` function f() {
            ` sendToServer('xyz', function (moreToDo) {
            ` // do something ...
            ` if (moreToDo) {
            ` f();
            ` }
            ` });
            ` }
          • Felix E. Klee
            On Sat, Jan 8, 2011 at 4:05 PM, Douglas Crockford ... Thanks for the suggestion! I guess I ll take this approach and put the lengthy do something into a
            Message 5 of 17 , Jan 8, 2011
            • 0 Attachment
              On Sat, Jan 8, 2011 at 4:05 PM, Douglas Crockford
              <douglas@...> wrote:
              > ` var sendToServer;
              > `
              > ` function f() {
              > ` sendToServer('xyz', function (moreToDo) {
              > ` // do something ...
              > ` if (moreToDo) {
              > ` f();
              > ` }
              > ` });
              > ` }

              Thanks for the suggestion! I guess I'll take this approach and put the
              lengthy "do something" into a separate function.

              Just thinking about it: I assume that having JSLint to *not* report an
              error on circular function definitions is non-trivial. That is, if one
              still wants to be warned about cases were an undefined function would be
              called, such as:

              function f() {
              g();
              }

              f();

              function g() {
              // do something
              }
            Your message has been successfully submitted and would be delivered to recipients shortly.