While going though some code in a plugin (not one I wrote), I came across the following snippet:

if ( !isset($update_transient->checked) ) return $update_transient;
	else $themes = $update_transient->checked;

Looking at this makes me cringe. Here’s how I would write this:

if (!isset($update_transient->checked))
{
   return $update_transient;
}

$themes = $update_transient->checked;

There is no need for the else statement. If the condition is true, an early exit will occur. Otherwise we always want the $themes variable to be updated.

In my opinion, my version is much easier to read. But I’d like to know what the PHP community thinks.

Please feel free to comment and share your thoughts on this.

Which way is better?

2 thoughts on “Which way is better?

  • February 6, 2015 at 1:40 pm
    Permalink

    I use early exits like the above all the time. *shrugs*

    Reply
    • February 6, 2015 at 6:04 pm
      Permalink

      Thanks – I appreciate the reply. :o)

      Reply

Leave a Reply

Your email address will not be published.