Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I generally encounter a lot of if/else.

Here is my sample code. I would like to get some suggestions on my code:

if( $blnLogged && isset( $_POST['postReview'] ) )
{
    if( $_SESSION['REVIEW']['SUBMITTED'] != '' )
    {
        $arrSharedData['blnRetSave'] = $_SESSION['REVIEW']['SUBMITTED'];
        $blnShowForm = false;
        $blnReviewPosted = true;
    }else{
        if( $arrData['captcha'] != $_SESSION['REVIEW']['CODE'] )
        {
            $strErrMsg = 'Invalid Captcha Code...';
        }else{
            if( empty( $arrData['review_title'] ) || empty( $arrData[ 'review_description' ] ) || empty( $arrData[ 'overall_review' ] ) || empty( $arrData[ 'cleanliness' ] ) || empty( $arrData[ 'facilities' ] ) || empty( $arrData[ 'location' ] ) || empty( $arrData[ 'quality_of_service' ] ) || empty( $arrData[ 'room' ] ) || empty( $arrData[ 'value_of_money' ]) )
            {
                $strErrMsg = 'Required field missing...';
            }else{

                //do we need any processing...
                $arrData['business_id'] = $bID;
                $arrData['client_id']   = $_SESSION['site_user'];
                $arrData['website_id']  = WEBSITE_ID;
                $arrData['review_date'] = date('Y-m-d');

                //If field Transformation required do it...

                $objTripReview = SM_Loader::loadClass('Class_Reviews');
                $blnRetSave = $objTripReview->saveReview( $arrData );
                $_SESSION['REVIEW']['SUBMITTED'] = $blnRetSave;
                $arrSharedData['blnRetSave'] = $_SESSION['REVIEW']['SUBMITTED'];
                $blnShowForm = false;
                $blnReviewPosted = true;
            }
        }
    }
}

if( $blnShowForm === true )
{
    $_SESSION['REVIEW']['CODE'] = rand(9999,99999);
    $_SESSION['Review']['SUBMITTED'] = '';
}
share|improve this question
For a minor readability improvement you can omit === true since you control $blnShowForm and != '' if $_SESSION['REVIEW']['SUBMITTED'] will always be a string or null. – David Harkness Feb 25 '11 at 8:05

2 Answers

up vote 4 down vote accepted

One simple way to improve this is to package it in a function, and return (or throw an exception) after each $strErrMsg = ... line. This will flatten the function, and allow you to put the main functionality at the "top level" of the function.

Even better might be to move all the validation code to a separate function that throws an exception, and then handle it in the function that called it with the wrong parameters.

share|improve this answer

Instead of

if (x)
{
    // ...
}
else
{
    if (y)
    {
        // ...
    }
}

you can just write

if (x)
{
    // ...
}
else if (y)
{
    // ...
}

In addition to that, I would put the list of required fields in an array somewhere where it is easily maintainable, so it’s not buried somewhere deep in the complex code:

$requiredFields = array( 'review_title', 'review_description',
    'overall_review', 'cleanliness', 'facilities', 'location',
    'quality_of_service', 'room', 'value_of_money' );

Then your code will look like this:

if( $blnLogged && isset( $_POST['postReview'] ) )
{
    $requiredFieldsPresent = true;
    foreach( $requiredFields as $field )
        if( empty( $arrData[$field] ) )
            $requiredFieldsPresent = false;

    if( $_SESSION['REVIEW']['SUBMITTED'] != '' )
    {
        $arrSharedData['blnRetSave'] = $_SESSION['REVIEW']['SUBMITTED'];
        $blnShowForm = false;
        $blnReviewPosted = true;
    }
    else if( $arrData['captcha'] != $_SESSION['REVIEW']['CODE'] )
    {
        $strErrMsg = 'Invalid Captcha Code...';
    }
    else if( !$requiredFieldsPresent )
    {
        $strErrMsg = 'Required field missing...';
    }
    else
    {
        // do we need any processing...
        // etc.
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.