|
Posted by Ian Collins on 03/21/06 08:32
Jim Michaels wrote:
> This is a simple coding tip I learned to prevent comparisons from becoming
> assignments (and this becoming a hair-pulling session during debugging):
>
> in comparisons, try to put your constants on the left hand side of the
> operator and variables on the right hand side.
> example
> if (3==$x) {
> //do something
> }
>
>
>
> if ($x=3) {
> //do something
> }
> if this were intended as a comparison, it would never be flagged as an error
> by the interpreter or compiler (in PHP's case, interpreter).
> With this line, I missed an = and the interpreter won't complain. it simply
> assigns 3 to $x and does something every time. With the top line, you can
> fix your code quickly because of the error message generated if you miss an
> =.
> Consider it a good coding habit?
It's required in some shops. In others, it's a matter of taste. Decent
unit tests also help prevent this problem.
--
Ian Collins.
Navigation:
[Reply to this message]
|