[UPHPU] short syntax without the else?

Dave Smith dave at thesmithfam.org
Sun Jan 20 20:16:05 MST 2008


Scott Hill wrote:
>
> I agree with this.  The expression $x = $y==1; is valid but it's hard 
> to read.  In order for our code to have any kind of legacy, it might 
> be best to type a few more characters and use
> $x = false;
> if ($y == 1)
>     $x = true;
>  
> or whatever else we can type to help others after us figure out what 
> we meant to do (or at least get close);

I emphatically agree that code ought to be written with readers in mind. 
In fact, I think that human readers are more important than the computer 
interpreter/compiler. However, I disagree that your example is more 
readable. For example, I consider this:

   $x = ( $y == 1 );

To be more readable than this:

  $x = false;
  if ( $y == 1 )
    $x = true;

Simply because there is less to read!

By the same argument, I prefer boolean function return values like this:

   return ( $y == 1 );

Over this:

  if( $y == 1 )
    return true;
  else
    return false;

Because it is less to read and less error prone.

I think my version conveys more meaning in fewer characters. So you may 
ask: what about programmers who aren't familiar with the syntax? Well, I 
opine that they ought to learn it because it's very expressive. If you 
don't know that syntax, it might mean you have a misunderstanding of how 
your language deals with boolean expressions (not to mention its 
assignment operator), which is quite fundamental to any language.

Just my 2 cents.

--Dave


More information about the UPHPU mailing list