To return first on error or on success
<p>Every day I always come across this dilemma - is it best practice to check for an undesired state at the start of a function and return straight away or, is it best practice to check for a desired condition and continue else return false.</p>

<p>I understand that there wont be a 1 fits all solution and that this is really a style issue but I am just interested in what other developers would typically do.</p>

<p>I always find my self being indecisive every time this situation comes up.</p>

<p>As an example (coded in php):</p>

<p>Check for the undesired condition first...</p>

<pre><code>function myFunction($myVal)
{
    if ($myVal != 'desiredVal') {
        return false;
    }

    //Continue here with main function code

    return true;
}
</code></pre>

<p>Or, check for the desired condition first...</p>

<pre><code>function myFunction($myVal)
{
    if ($myVal == 'desiredVal') {
        //continue here with main function code

        return true;
    }

    return false;
}
</code></pre>
