Avoid actions without a request


#1

Sometimes it’s necessary to determine dynamically whether an action can be executed on the current page or not. With this intention you may write the following piece of code (pseudo code) :

MyAction
{
    execute()
    {
        if(button.exists())
        {
            loadPageByClick(button);
        }
    }
}

In case the button is present, the request will be fired. Okay so far. But if the button is not on the current page, we’d still execute the Action but do exactly nothing in it’s body. Neither a new page is loaded nor the current one is updated (for example via XHR response). This is to be avoided. Please keep in mind that an Action counts as a PageView. In the current case this would distort the results.

A better approach is to check the availability of the button before running the action, what might look like:

if(button.exists())
{
    new MyAction().execute();
}

… and the corresponding MyAction class will have much cleaner code:

MyAction
{
    execute()
    {
        loadPageByClick(button);
    }
}