nesting functions

Discussion in 'Javascript' started by Philip WATTS, Oct 31, 2003.

  1. Philip WATTS

    Philip WATTS Guest

    I am trying to carry out multiple checks on some input data. I am doing this
    by the running the data through a number of functions. i.e. I have an
    onclick that calls a function, which in turn calls the test functions.

    My problem is getting the testing to stop if one of the tests fails and
    await for the input to be amended.
    I believe that this is because the when the testing function has finished it
    returns the script to the point immediately AFTER the function was called,
    i.e. it simply carries on to the next test and hence to completetion of the
    script.

    How do I overcome this problem?

    Many thanks
    Phil
     
    Philip WATTS, Oct 31, 2003
    #1
    1. Advertising

  2. > I am trying to carry out multiple checks on some input data. I am doing this
    > by the running the data through a number of functions. i.e. I have an
    > onclick that calls a function, which in turn calls the test functions.
    >
    > My problem is getting the testing to stop if one of the tests fails and
    > await for the input to be amended.
    > I believe that this is because the when the testing function has finished it
    > returns the script to the point immediately AFTER the function was called,
    > i.e. it simply carries on to the next test and hence to completetion of the
    > script.
    >
    > How do I overcome this problem?


    Have each function return true if the input is ok. Then use if statements.
    if (test1()) {
    if (test2()) {
    if (test3()) {
    ...

    http://www.crockford.com/#javascript
     
    Douglas Crockford, Oct 31, 2003
    #2
    1. Advertising

  3. Philip WATTS

    e Guest

    Have each function return a boolean indicating it's success. Sorry js isn't
    my forte I'm just lurnking while I wait for an answer to one of my posts :p,
    so syntax could be way off. But general idea is this:

    function checkTheInput()
    {
    if (firstCheck())
    {
    if (secondCheck())
    {
    if (thirdCheck())
    {
    alert('all 3 cheks were ok');
    }
    else
    {
    alert('thirdCheck() failed');
    }
    }
    else
    {
    alert('secondCheck() failed');
    }
    {
    else
    {
    alert('firstCheck() failed');
    }
    }

    function firstCheck()
    {
    //if data is ok return true, otherwise return false
    }

    function secondCheck()
    {
    //ditto
    }

    etc...

    "Philip WATTS" <> wrote in message
    news:bnug4o$ts6$...
    > I am trying to carry out multiple checks on some input data. I am doing

    this
    > by the running the data through a number of functions. i.e. I have an
    > onclick that calls a function, which in turn calls the test functions.
    >
    > My problem is getting the testing to stop if one of the tests fails and
    > await for the input to be amended.
    > I believe that this is because the when the testing function has finished

    it
    > returns the script to the point immediately AFTER the function was called,
    > i.e. it simply carries on to the next test and hence to completetion of

    the
    > script.
    >
    > How do I overcome this problem?
    >
    > Many thanks
    > Phil
    >
    >
     
    e, Oct 31, 2003
    #3
  4. Philip WATTS wrote:

    > My problem is getting the testing to stop if one of the tests fails and
    > await for the input to be amended.
    > I believe that this is because the when the testing function has finished it
    > returns the script to the point immediately AFTER the function was called,
    > i.e. it simply carries on to the next test and hence to completetion of the
    > script.
    >
    > How do I overcome this problem?


    Instead of using nested if-statements you could simply `return false'
    if a check fails. I find this more practical since you can add tests
    without further nested block statements (and without indentation which
    should have been done then for the sake of legibility):

    function testMe()
    {
    if (!test1())
    return false;
    if (!test2())
    return false;
    if (!test3())
    return false;

    return true; // passed all tests
    }


    PointedEars
     
    Thomas 'PointedEars' Lahn, Oct 31, 2003
    #4
  5. Thomas 'PointedEars' Lahn <> writes:

    > function testMe()
    > {
    > if (!test1())
    > return false;
    > if (!test2())
    > return false;
    > if (!test3())
    > return false;
    >
    > return true; // passed all tests
    > }


    That sounds like a job for short-circuit boolean operators!

    function testMe() {
    return test1() && test2() && test3();
    }

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Oct 31, 2003
    #5
  6. Lasse Reichstein Nielsen wrote:

    > Thomas 'PointedEars' Lahn <> writes:
    >> function testMe()
    >> {
    >> if (!test1())
    >> return false;
    >> if (!test2())
    >> return false;
    >> if (!test3())
    >> return false;
    >>
    >> return true; // passed all tests
    >> }

    >
    > That sounds like a job for short-circuit boolean operators!
    >
    > function testMe() {
    > return test1() && test2() && test3();
    > }


    Not if you, like the OP, want to know *which*
    test failed (which I omitted in the above code):

    function ...(...)
    {
    if (!test1())
    alert("Test 1 failed!");
    return false;
    }
    ...
    return true; // passed all tests
    }


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 1, 2003
    #6
  7. Philip WATTS

    Evertjan. Guest

    Thomas 'PointedEars' Lahn wrote on 01 nov 2003 in comp.lang.javascript:

    > Not if you, like the OP, want to know *which*
    > test failed (which I omitted in the above code):
    >
    > function ...(...)
    > {
    > if (!test1())
    > alert("Test 1 failed!");
    > return false;
    > }
    > ...
    > return true; // passed all tests
    > }


    Thet you should put an extra { where it belongs:

    function testing() {
    if !test1() {
    alert("Test 1 failed!");
    return false;
    }
    ...
    alert("passed all tests");
    return true;
    }



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 1, 2003
    #7
  8. Philip WATTS

    Grant Wagner Guest

    Thomas 'PointedEars' Lahn wrote:

    > Philip WATTS wrote:
    >
    > > My problem is getting the testing to stop if one of the tests fails and
    > > await for the input to be amended.
    > > I believe that this is because the when the testing function has finished it
    > > returns the script to the point immediately AFTER the function was called,
    > > i.e. it simply carries on to the next test and hence to completetion of the
    > > script.
    > >
    > > How do I overcome this problem?

    >
    > Instead of using nested if-statements you could simply `return false'
    > if a check fails. I find this more practical since you can add tests
    > without further nested block statements (and without indentation which
    > should have been done then for the sake of legibility):
    >
    > function testMe()
    > {
    > if (!test1())
    > return false;
    > if (!test2())
    > return false;
    > if (!test3())
    > return false;
    >
    > return true; // passed all tests
    > }
    >
    > PointedEars


    This is known as a "gauntlet" <url: http://mindprod.com/jgloss/gauntlet.html />,
    specfically an "Early Return Style Gauntlet".

    I used to detest this type of code, it seemed sloppy and ugly, however, as I write
    more and more code, I find myself using the style more and more often, because as
    Roedy points out "I like this style because the conditions are independent and
    uniform. You can shuffle the order easily and add new conditions without having
    the adjust the existing code.".

    --
    | Grant Wagner <>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *
    http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-developer/upgrade_2.html
     
    Grant Wagner, Nov 4, 2003
    #8
  9. Evertjan. wrote:

    > Thomas 'PointedEars' Lahn wrote [...]:
    >> Not if you, like the OP, want to know *which*
    >> test failed (which I omitted in the above code):
    >>
    >> function ...(...)
    >> {
    >> if (!test1())
    >> alert("Test 1 failed!");
    >> return false;
    >> }
    >> ...
    >> return true; // passed all tests
    >> }

    >
    > Thet you should put an extra { where it belongs:
    >
    > function testing() {
    > if !test1() {


    Also nitpicking, the `if' statement requires
    parantheses around the conditional expression.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 23, 2003
    #9
  10. Philip WATTS

    Evertjan. Guest

    Thomas 'PointedEars' Lahn wrote on 23 nov 2003 in comp.lang.javascript:

    >> Thet you should put an extra { where it belongs:
    >>
    >> function testing() {
    >> if !test1() {

    >
    > Also nitpicking, the `if' statement requires
    > parantheses around the conditional expression.


    Pick nit and be my guest ;-)

    "requires" by definition or by erroring out ?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 23, 2003
    #10
  11. "Evertjan." <> writes:

    > Thomas 'PointedEars' Lahn wrote on 23 nov 2003 in comp.lang.javascript:


    >> Also nitpicking, the `if' statement requires
    >> parantheses around the conditional expression.


    > "requires" by definition or by erroring out ?


    Requres by the syntax rules of Java/ECMAScript. Without them, it
    is not an if statement, just a syntax error.

    (I don't know what "erroring out" means).

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 23, 2003
    #11
  12. Philip WATTS

    Evertjan. Guest

    Lasse Reichstein Nielsen wrote on 23 nov 2003 in comp.lang.javascript:

    > "Evertjan." <> writes:
    >> Thomas 'PointedEars' Lahn wrote on 23 nov 2003 in comp.lang.javascript:
    >>> Also nitpicking, the `if' statement requires
    >>> parantheses around the conditional expression.

    >
    >> "requires" by definition or by erroring out ?

    >
    > Requres by the syntax rules of Java/ECMAScript. Without them, it
    > is not an if statement, just a syntax error.


    Since when is a statement not a statement if the subsequent syntax is not
    according to the rule book? In your definition, which is not mine, a
    statement can never be syntactically incorrect, it seems.

    > (I don't know what "erroring out" means).


    Does it give an error or does it work as intended [by me and anyone
    reasonable]? There is a big difference between syntactical correctness and
    a working script, in both possible scenarios: Some syntactical incorrect
    scripting works, some syntactical correct scripting doesn't and then you
    get an error, a crash or an unforseen and inintended result. In those two
    circumstances it the working incorrect script seems preferable.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 23, 2003
    #12
  13. "Evertjan." <> writes:

    > Since when is a statement not a statement if the subsequent syntax is not
    > according to the rule book?


    What do you mean by "subsequent"?

    The syntax of an "if" statement is:
    keyword "id"
    left parenthesis, "("
    expression
    right parenthesis, ")"
    statement
    (optional: keyword "else" + statement)

    Omitting the parentheses makes it no more an "if" statement that
    omitting "function" makes something a function declaration.

    > In your definition, which is not mine, a
    > statement can never be syntactically incorrect, it seems.


    Exactly.
    In fact, if the syntax is incorrect, not only isn't it a statment,
    it's not even Javascript (or maybe more correct, not even ECMAScript,
    since Javascript doesn't have as exact a definition).

    A syntax error is just that: an error. It might have been intended
    to be a statement, but it isn't.

    >> (I don't know what "erroring out" means).

    >
    > Does it give an error or does it work as intended [by me and anyone
    > reasonable]?


    It gives an error. It fails to compile correctly, which also means
    that the entire file/script element it is in, is ignored.

    > There is a big difference between syntactical correctness and
    > a working script, in both possible scenarios: Some syntactical incorrect
    > scripting works,


    Correct. Some interpreters allow things not in the official syntax,
    like function declarations inside block statements or <!-- for comments.

    This is not one of those. You can no more omit the parenteses of an
    if expression than you can those of a while, switch or for expression.

    > some syntactical correct scripting doesn't and then you
    > get an error, a crash or an unforseen and inintended result.


    That would be a runtime error, e.g., accessing a property of the null
    value.

    > In those two circumstances it the working incorrect script seems
    > preferable.


    Not working at all is preferable to working incorrectly. :)

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 23, 2003
    #13
  14. Philip WATTS

    Evertjan. Guest

    Lasse Reichstein Nielsen wrote on 23 nov 2003 in comp.lang.javascript:
    >> In those two circumstances it the working incorrect script seems
    >> preferable.

    >
    > Not working at all is preferable to working incorrectly. :)


    You misread my point.

    No, no, an incorrect (specs wise) script could work as intended.

    And a correct (specs wise) script could work incorrectly.

    An example comes to mind: the Jscript toFixed().


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 23, 2003
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DeadlyTedly
    Replies:
    2
    Views:
    2,585
    Ted Bradley
    Oct 16, 2003
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,663
    Victor Bazarov
    Jun 21, 2004
  3. newbai

    nesting of member functions

    newbai, Jan 13, 2007, in forum: C++
    Replies:
    3
    Views:
    452
    Rolf Magnus
    Jan 13, 2007
  4. Boltar

    Nesting variadic functions

    Boltar, Sep 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    527
    CBFalconer
    Sep 17, 2007
  5. Trans
    Replies:
    10
    Views:
    327
    Sean O'Halpin
    Sep 16, 2005
Loading...

Share This Page