useful do while(0) trick

 

This article is about an useful programming trick to avoid writing clumsy and highly nested if-else conditions. The perfect example of this could be - "error handling code". Let me present you with a clumsy error - handling code which we can replace with a simple and elegant do-while loop.

Most of the times, while doing validations of the user's input in PHP we might have to write heavily nested if - else conditions e.g.


$errflag = false;
$errstring = “”;

if (isset($_POST['var1'])) {
    if (isValidInput($_POST['var1']) == true) {
        $value1 = $_POST['var1'];
    } else {
        $errflag = true;
        $errstring = "invalid value for var1";
    }
} else {
    $errflag = true;
    $errstring = "var1 is missing";
}

The code is already getting ugly :( But I can get it much uglier if I go on and add one more variable for validation:


if (($errflag == false) && isset($_POST['var2'])) {
    if (isValidInput($_POST['var2']) == true) {
        $value2 = $_POST['var2'];
    } else {
        $errflag = true;
        $errstring = "invalid value for var2";
    }
} else {
    if (! isset($_POST['var2'])) {
        $errflag = true;
        $errstring = "var2 is missing";
    }
}


Not only the above code looks bad and hard to understand, but it’s also prone to error in itself. We might miss one condition or the other unknowingly and assign invalid error condition.

Now, let’s look at the do-while translation of the above code:


$errflag =  false;
$errstring = "";

do {
    if (!isset($_POST['var1'])) {
        $errflag = true;
        $errstring = "var1 missing";
        break;
    }

    if (isValidInput($_POST['var1']) == false) {
        $errflag = true;
        $errstring = 'Invalid value for var1';
        break;
    }
    $value1 = $_POST['var1'];
   
    if (!isset($_POST['var2'])) {
        $errflag = true;
        $errstring = "var2 missing";
        break;
    }

    if (isValidInput($_POST['var2']) == false) {
        $errflag = true;
        $errstring = 'Invalid value for var2';
        break;
    }
    $value2 = $_POST['var2'];
} while (0);

if ($errflag == true) {
    //display the $errstring here.
}

I hope the above code-snippet is self-explanatory. Both the code snippets are doing exactly the same thing, but the do-while approach is not only easy to grasp, it also leaves lesser chance for manual mistakes.

 

One more example where I have found this trick very helpful is in situations, where we have to sequentially process a particular variable. And at each level of processing, if some error or invalid condition occurs, we have to avoid the sequential processing and do an exit.


status = process_first();
if (status == true) {
    status = process_second();
    if (status == true) {
        status = process_third();
            //(and the nesting continues…)
    }
}

Above if-else ladder can simply be written as:

#define CALL_AND_RETURN(x)  if ( x() == false) break;
do {
    CALL_AND_RETURN(process_first);
    CALL_AND_RETURN(process_second);
    CALL_AND_RETURN(process_third);
    //(simply add other calls here)
} while (0);

Hope that helps in one way or the other in writing code that is easy to comprehend.

- Pankaj

Happy Programming !!

comments powered by Disqus