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/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html