Re: Setting flags versus checking for existing/nonexisting values

From: Date: Tue, 15 Aug 2006 22:03:30 +0000
Subject: Re: Setting flags versus checking for existing/nonexisting values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-240732@lists.php.net to get a copy of this message
On Tue, August 15, 2006 11:53 am, Chris W. Parker wrote: > Is it a better practice to set flags to determine the action of your > code or is it perfectly acceptable to have your code determine what it > should do based on the existence (or lack thereof) of data? > > For example: > > <?php > > if($value == 1) > { > $flag = true; > } > > if($flag === true) > { > echo "I wish I could come to the PHP meetup in Chicago! :("; > } > > ?> > > versus: > > <?php > > if($value == 1) > { > echo "I wish I could come to the PHP meetup in Chicago! :("; > } > > ?> > > Of course this is an overly simplistic example but you get the idea. > > Are there pros and cons to both sides or should I just avoid the > latter > example all together? If the test is as simple as $value == 1, then setting a flag and testing the flag is silly, over-engineered, and error-prone, and it leads to code cruft which leads to bugs as alternative branches are less clear than if the test is done in-line. If the logic to figure out $flag is complicated, with multiple inputs, and the result is used in multiple places, then, by all means, figure it out once, and give the flag A GOOD NAME VARIABLE so that you can reference it again and again later, rather than wade through complicated test code over and over. -- Like Music? http://l-i-e.com/artists.htm

« previous php.general (#240732) next »