note 65842 added to control-structures.alternative-syntax
| From: | php-general at lists dot php dot net | Date: | Mon, 08 May 2006 21:51:19 +0000 |
| Subject: | note 65842 added to control-structures.alternative-syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-110901@lists.php.net to get a copy of this message | ||
matheo's example is the classic act of using a hammer to set a screw. (No offense...)
As programmers, we <b>must</b> become comfortable that the result of a logic-expression
<i>is a value</i> just like any other.
Therefore instead of:
<?php
$a = 1 ;
$b = 2 ;
$is_a_bigger = ($a > $b) ? 1 : 0 ;
echo $a_is_bigger ;
# returns 0
?>
Use this:
<?php
$a = 1;
$b = 2;
$a_is_bigger = $a > $b;
...
?>
You read it as "variable $a_is_bigger is set to the result of the comparison of $a to $b".
Compare that to the read-thru of the upper example: "variable $is_a_bigger is set to true if
the is-greater comparison of $a to $b is true, and set to false if the comparison is false."
How redundant. The only thing more redundant is the ever increasingly popular 'duh' logic
of today's <i>modern</i> PHP programming:
<?php
$a = 1;
$b = 2;
if( $a > $b )
{
$a_is_bigger = true ;
}
else if( $b >= $a )
{
$a_is_bigger = false ;
}
?>
8 lines to do what one simple construction can do ... which if we <i>learn to recognize
it</i> vastly speeds the reading and appreciation of the purpose of the arbitrary algorithm.
The <b>only constructive criticism</b> that I've heard about the use of both
ternary operators and logic-evaluation-direct-assign is that neither of them are easy to debug by
adding 'prints' or 'echos' to their interiors. Point well made. Yet, if they
are as simple as the above examples, it must be argued that the use of 8 lines to accomplish what a
single comparison-assignment could do may well have resulted in more potential errors. (For
instance, did I remember to put the '=' in the second comparison as the exact compliment
of the first?)
rlynch AT lynchmarks DOT com
----
Server IP: 66.163.161.117
Probable Submitter: 64.161.113.75
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/control-structures.alternative-syntax.php
Edit -- http://master.php.net/note/edit/65842
Del: integrated -- http://master.php.net/note/delete/65842/integrated
Del: useless -- http://master.php.net/note/delete/65842/useless
Del: bad code -- http://master.php.net/note/delete/65842/bad+code
Del: spam -- http://master.php.net/note/delete/65842/spam
Del: non-english -- http://master.php.net/note/delete/65842/non-english
Del: in docs -- http://master.php.net/note/delete/65842/in+docs
Del: other reasons-- http://master.php.net/note/delete/65842
Reject -- http://master.php.net/note/reject/65842
Search -- http://master.php.net/manage/user-notes.php