Early Returns

As with loops, it is possible to optimize function execution by implementing a function in such a way as to allow the optional skipping of body statements. Using early returns is a method of checking as many reasons as possible for not continuing to execute the function. What you basically want to do is make sure that reserving memory, doing complex calculations and calling other functions is only done when it is clear that the efforts will be useful. Listing 8.15 shows an example function that uses no early returns.

Code Listing 8.15. No Use of Early Returns
struct Object
{
    int  color;
    int  material;
    int  city;
    char *name;
} ;


Object *CreateObject(char *name)
{
    Object *pObj = new Object;
    strcpy(pObj->name, name);

    pObj->color   = CurrentStatus->FashionColor();
    pObj->material = CurrentStatus->FashionMaterial();
    pObj->city     = CurrentStatus->CurrentCity();

if (    pObj->city     == UNDEFINED ||
        pObj->color    == UNDEFINED ||
        pObj->material == UNDEFINED)
    {
        delete [] pObj->name;
        delete pObj;
        return NULL;
    }

    return
						
						 pObj;
}

The function CreateObject() endeavors to create a new object with a given name. This new object will take its other values (color, material, and city) from a global database that should contain current status information. This way the new object will have the color and material which are most fashionable at the moment of creation, and will be produced in the city with the best plants. It is possible, however, that some status information is not defined at object creation time, in which case it is not necessary to create a new object yet.

Listing 8.15 shows the way functions are often used in the field. When you look closely at the fail conditions, found at the end of the function, you notice that it is possible to speed up function execution considerably in the case when no object is created. This becomes increasingly useful as likelihood of failure increases. By evaluating fail conditions earlier, it is possible to skip time-consuming object creation and deletion. Listing 8.16 demonstrates this for the CreateObject() function of Listing 8.15.

Code Listing 8.16. Using Early Returns
Object *CreateObject(char *name)
{
    int col, mat, cit;

    if (UNDEFINED == (col = CurrentStatus->FashionColor()))
        return NULL;
    if (UNDEFINED == (mat = CurrentStatus->FashionMaterial()))
        return NULL;
    if (UNDEFINED == (cit = CurrentStatus->CurrentCity()))
        return NULL;

    Object *pObj = new Object;
    strcpy(pObj->name, name);
    pObj->city     = cit;

    pObj->color    = col;
    pObj->material = mat;

    return
						
						 pObj;
}

Note the complete absence of a delete instruction from Listing 8.16. Also, a call to strcpy is made only when necessary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset