Reply to Re: coding tip to prevent comparisons becoming assignments

Your name:

Reply:


Posted by Jim Michaels on 03/23/06 04:30

"David Haynes" <david.haynes2@sympatico.ca> wrote in message
news:%UQTf.114239$kp3.46045@fe48.usenetserver.com...
> 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? Then again there are books on this (if
>> they are still published).
> One issue is that it only works if the left-hand side is a constant.
>
> -david-
>

yeah. I know. the rest is visual and normal debug methods, like the point
you mention. but, it's there for whatever it's worth.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация