Having one of those days, booleans

Man of Honour
Joined
29 Jun 2004
Posts
21,615
Location
Oxfordshire
Morning!

So this should be easy, but for some reason my brain isn't functioning at the moment

Code:
public static bool CheckOCRLanguagePageLoadCorrectly(IWebDriver driver)
        {
            Selenium.ClickLinkByText(driver, "OCR Language");

            bool success = PageObjects.CheckOldPageActiomBarLoadsCorrectly(driver);

            success = PageObjects.CheckSearchOptionsLoadsCorrectly(driver);

            success = PageObjects.CheckDataGridLoadsCorrectly(driver);

            return success;
        }

The problem I'm having is, say the top one returns false, but the other two return true, the false value will be lost and the function that uses this return value will see it as true, when really it should be a failure. Sometimes there could be up to 12 of these calls, so don't really want 12 seperate bool variables, so is there a way of doing this using the same variable, but will drop out the function if one returns true, failing that, will run through the entire function but return false if any of the statements at any point returned false?

Thanks guys, apologies if I explained that badly, like I said, just one of those days that my brain isn't firing! :D
 
Last edited:

AJK

AJK

Associate
Joined
8 Sep 2009
Posts
1,722
Location
UK
On each subsequent test, you want the assignment statement to be

Code:
success = test && success;

That way the eventual value of success will only be true if all the tests succeeded.
 
Man of Honour
OP
Joined
29 Jun 2004
Posts
21,615
Location
Oxfordshire
Thank you very much kind sir!

I never knew about that either. What's that wizadry called out of curiosity?

EDIT - Boolean logical operators, I now feel slightly dumb that I did not know these existed!
 
Last edited:

AJK

AJK

Associate
Joined
8 Sep 2009
Posts
1,722
Location
UK
Just a logical AND operator :) The value of x && y is true if and only if both are true, otherwise false.

EDIT - that's alright, now you do! :)
 
Associate
Joined
10 Dec 2008
Posts
1,857
Location
Somewhere over there
Wait why couldn't you do,

Code:
public static bool CheckOCRLanguagePageLoadCorrectly(IWebDriver driver)
{
    Selenium.ClickLinkByText(driver, "OCR Language");

    if (!PageObjects.CheckOldPageActiomBarLoadsCorrectly(driver)) return false;

    if (!PageObjects.CheckSearchOptionsLoadsCorrectly(driver)) return false;

    if (!PageObjects.CheckDataGridLoadsCorrectly(driver)) return false;

    return true;
}

So it returns false as soon as one fails and carries on if true? Or am I missing something and being derpy? xD

edit: unless you want to run all of the methods then I guess that makes sense :)
 
Man of Honour
OP
Joined
29 Jun 2004
Posts
21,615
Location
Oxfordshire
That would work as well!

I think I'll stick with the first option only because it means I'm still able to run all of the methods if I need too! But on some where I want it to quit out asap then yours is the better one for me!

Thanks guys
 
Soldato
Joined
16 Feb 2004
Posts
4,811
Location
London
Code:
public static bool CheckOCRLanguagePageLoadCorrectly(IWebDriver driver)
{
	Selenium.ClickLinkByText(driver, "OCR Language");

	return PageObjects.CheckOldPageActiomBarLoadsCorrectly(driver) 
		&& PageObjects.CheckSearchOptionsLoadsCorrectly(driver) 
		&& PageObjects.CheckDataGridLoadsCorrectly(driver);
}

:p
 

AJK

AJK

Associate
Joined
8 Sep 2009
Posts
1,722
Location
UK
DanF's code will also work, of course, but note that it will stop executing tests after the first false result (since && is a conditional operator).
 
Soldato
Joined
29 Jul 2004
Posts
9,680
Location
Somerset
Multiple return paths are generally bad news. Very annoying to debug if it goes wrong, hard to maintain, refactor or add to. Bad practice in general. Boolean == Test() && Boolean is the way to do it.
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Multiple return paths are great if used appropriately.
I'd much rather use multiple return paths that try to keep try of things with local variables.
Imagine you have multiple checks that involve network calls. I'd much rather return from the method when I know the return value won't change with any further calls than try to keep track of it all with a local variable.

Plus, when used in a switch statement in C# they prevent you from having to call break; if you want to return immediately from the switch.

In the OP's example I'd probably go for something like DanF has posted. The && operator short circuits so it won't evaluate more than it needs to and it's concise and clear what is going on.
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
Haircut> Surely it depends on what you are trying to achieve?

Steedie, put up a finished example :)

Yes, obviously you have to deal with things on a case by case basis.
The majority of time I prefer to have multiple return points in a method though.
I find it more expressive and readable as generally you'll either need to introduce local variables, more nesting or possibly both to have a single return point.
 
Back
Top Bottom