Re: [PHP] Setting flags versus checking for existing/nonexisting values

2006-08-15 Thread Brad Bonkoski



Chris W. Parker wrote:

Hello,

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?



Thanks,
Chris.

  

Pros: potentially more readable code.
Cons: Wasted energy typing unnecessary lines of code.
Really I would say it comes down to coder preference.

(and why would you avoid the latter all together?  Testing a boolean may 
be cleaner, but setting the boolean still relies on the value of $value, 
so if that value was fubar then the boolean would be too.)

-Brad

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Setting flags versus checking for existing/nonexisting values

2006-08-15 Thread Chris W. Parker
Brad Bonkoski mailto:[EMAIL PROTECTED]
on Tuesday, August 15, 2006 10:04 AM said:

 Pros: potentially more readable code.
 Cons: Wasted energy typing unnecessary lines of code.
 Really I would say it comes down to coder preference.
 
 (and why would you avoid the latter all together?  Testing a boolean
 may be cleaner, but setting the boolean still relies on the value of
 $value, so if that value was fubar then the boolean would be too.)

Thanks for the response. Those are basically the same assumptions I had.
I was curious to find out if there were more points I should be aware
of.

To answer your question, in case the cons outweigh the pros. If I felt
an overwhelming majority of the people on the list said, In my
experience you should always set flags because you'll run into a, b, c,
d, e, f, g, etc. I would probably agree to avoid the latter practice
altogether.


Chris.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Setting flags versus checking for existing/nonexisting values

2006-08-15 Thread Robert Cummings
On Tue, 2006-08-15 at 10:12 -0700, Chris W. Parker wrote:
 Brad Bonkoski mailto:[EMAIL PROTECTED]
 on Tuesday, August 15, 2006 10:04 AM said:
 
  Pros: potentially more readable code.
  Cons: Wasted energy typing unnecessary lines of code.
  Really I would say it comes down to coder preference.
  
  (and why would you avoid the latter all together?  Testing a boolean
  may be cleaner, but setting the boolean still relies on the value of
  $value, so if that value was fubar then the boolean would be too.)
 
 Thanks for the response. Those are basically the same assumptions I had.
 I was curious to find out if there were more points I should be aware
 of.
 
 To answer your question, in case the cons outweigh the pros. If I felt
 an overwhelming majority of the people on the list said, In my
 experience you should always set flags because you'll run into a, b, c,
 d, e, f, g, etc. I would probably agree to avoid the latter practice
 altogether.

If it's just the mere existence that determines the value then isset()
is fine. But if the value is determined by a boolean value of the
variable and defaults to some value when not set, then I almost always
assign to a flag so that subsequent checks don't need to perform both
the isset() check and the value check (presuming you care about E_NOTICE
which I do :)

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Setting flags versus checking for existing/nonexisting values

2006-08-15 Thread Richard Lynch
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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Setting flags versus checking for existing/nonexisting values

2006-08-15 Thread tedd

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:


-snip- flag example

I seldom use flags, but then again I never use if/elseif's either.

Flags introduce another variable you have to keep track of. When do 
you turn them on/off becomes another step in your logic that is 
directly dependant upon data in the first place. So, if logic 
dictates a branch, then I place the condition on the data.


As I see it, in this case, only three conditions can exist:

1. Data is there, or not there -- use isset() or !isset();
2. Data is relative to a single value -- use if with equalities 
(i.e., =, ==,...);

3. Data is relative to two, or more, values -- use switch.

That works for me. But, I'm sure that mileage for others vary.

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php