Conditions in Guard

RobotFan's Avatar

RobotFan

25 Mar, 2015 09:56 AM

Hello,

Firstly, thank you guys for this framework, I love it.

I would like to know, what do you think about more conditions in Guards.
So, what do you think, this approve function is valid and "best practice" or not:

//SedanAndGoodEngineGuard.as
public function approve():Boolean
{
  return this.carModel.type == "sedan" && this.engineModel.status == "good";
}

Or I should split it to 2 seperated Guard, like this:

//TypeIsSedanGuard.as
public function approve():Boolean
{
  return this.carModel.type == "sedan";
}

//EngineIsGoodGuard.as
public function approve():Boolean
{
  return this.engineModel.status == "good";
}

Which is the better? And What should I do, if I have to check the conditions with || and not &&?

  1. Support Staff 1 Posted by Ondina D.F. on 25 Mar, 2015 03:25 PM

    Ondina D.F.'s Avatar

    Hello RobotFan,

    I'm glad to hear that you love robotlegs:)

    Let's take a look at the guardsApprove:

    https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...
    As you can see, it loops through all the guards, and it returns true only if all the guards returned true.

    If we had a command mapped like this:

    commandMap.map( SomeEvent.SOME_TYPE )
        .toCommand( SomeCommand )
        .withGuards(  FirstGuard, SecondGuard );
    

    if one of the guards returned false, the command won't be executed.

    So, if you want the command to run on an OR condition, use a single guard

    public function approve():Boolean
    {
        return firstCondition ||  secondCondition;
    }
    

    Guards are nothing more than a conditional expression that must evaluate to true in order for 'something' to happen (program execution). How you use them, it's up to you.

    If you are always checking for :
    this.carModel.type == "sedan" && this.engineModel.status == "good";
    then one guard would be just fine.

    If you need to use this.carModel.type == "sedan" in one place and this.engineModel.status == "good" somewhere else, and this.carModel.type == "sedan" || this.engineModel.status == "good" yet elsewhere, then maybe 3 guards are required:
    TypeIsSedanGuard.as, EngineIsGoodGuard.as, and
    SedanORGoodEngineGuard.as (for the OR case)

    If you need an AND, as in SedanAndGoodEngineGuard.as, you can just add the guards like I did in my example:

    .withGuards( TypeIsSedanGuard, EngineIsGoodGuard );

    Does this clarify your question?

    Ondina

  2. 2 Posted by RobotFan on 26 Mar, 2015 07:52 AM

    RobotFan's Avatar

    Hey Ondina,
    I also thought so, thank you for your confirmation! :)

  3. Support Staff 3 Posted by Ondina D.F. on 28 Mar, 2015 08:58 AM

    Ondina D.F.'s Avatar

    No problem:)

  4. Ondina D.F. closed this discussion on 28 Mar, 2015 08:58 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac