Re: Re: [PHP] Long Live GOTO

2012-02-06 Thread Tim Streater
On 06 Feb 2012 at 15:05, Robert Cummings rob...@interjinn.com wrote: 

 I've had a strong opinion on goto for a very long time. I was one of the
 proponents who argued on internals for its inclusion several years ago.
 I stand by its utility and refer the reader to the fact that many open
 source projects, especially ones that use some kind of parser, have goto
 hidden within their implementation. You can find it in the C code for
 the PHP, MySQL, and Apache to name a few easily recognizable projects.

All of which is no doubt true but that doesn't mean I have to like it, although 
obviously I'll have to put up with it. Anyway, discussions of this sort tend to 
be, or become, futile.

--
Cheers  --  Tim

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

Re: Re: [PHP] Long Live GOTO

2012-02-06 Thread Tim Streater
On 06 Feb 2012 at 09:48, Adam Richardson simples...@gmail.com wrote: 

 On Mon, Feb 6, 2012 at 4:25 AM, Adam Richardson simples...@gmail.comwrote:

 On Mon, Feb 6, 2012 at 4:07 AM, Tim Streater t...@clothears.org.uk wrote:

 I disagree that the nested function is a straw-man. I (just as the other
 authors I'd linked to describing the arrow pattern of code) have seen
 plenty of examples of similar code.

I guess what I meant was, that I'd never have written it that way in the first 
place, so as an example it felt contrived. Amateurs or people with no training 
(in particular physicists at CERN 40 years ago) should be kept well clear of 
the goto. I'd probably write your function like this:

function val_nested ($name = null, $value = null, $is_mutable = false)
 {

 static $values   = array();
 static $mutables = array();

 if  ($name===null)  return $values;

 if  ($value===null)  return isset($values[$name]) ? $values[$name] : null;

 if  (isset($values[$name]))
  {

      if (!$val_is_mutable = in_array($name, $mutables))// Set existing 
value
   {
   $msg = 'The value ' . $name . ' is immutable and has already 
been set to ' . $values[$name] . '.';
   throw new Exception ($msg);
   }

  return $values[$name] = $value;

  }

 if ($is_mutable)  $mutables[] = $name; // Set new value
 $values[$name] = $value;

 return $value;

 }


I always add blank lines for clarity. Remove those and the above is 30% shorter 
than yours - as far as I could tell, none of the else clauses was required.

My approach is:

1) deal with the trivial and error cases first

2) deal with the real work next

--
Cheers  --  Tim

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

Re: Re: [PHP] Long Live GOTO

2012-02-06 Thread Adam Richardson
On Mon, Feb 6, 2012 at 11:58 AM, Tim Streater t...@clothears.org.uk wrote:

 On 06 Feb 2012 at 09:48, Adam Richardson simples...@gmail.com wrote:

  On Mon, Feb 6, 2012 at 4:25 AM, Adam Richardson simples...@gmail.com
 wrote:
 
  On Mon, Feb 6, 2012 at 4:07 AM, Tim Streater t...@clothears.org.uk
 wrote:

  I disagree that the nested function is a straw-man. I (just as the other
  authors I'd linked to describing the arrow pattern of code) have seen
  plenty of examples of similar code.

 I guess what I meant was, that I'd never have written it that way in the
 first place, so as an example it felt contrived. Amateurs or people with no
 training (in particular physicists at CERN 40 years ago) should be kept
 well clear of the goto. I'd probably write your function like this:

 function val_nested ($name = null, $value = null, $is_mutable = false)
  {

  static $values   = array();
  static $mutables = array();

  if  ($name===null)  return $values;

 if  ($value===null)  return isset($values[$name]) ? $values[$name] :
 null;

 if  (isset($values[$name]))
  {

   if (!$val_is_mutable = in_array($name, $mutables))// Set
 existing value
   {
   $msg = 'The value ' . $name . ' is immutable and has
 already been set to ' . $values[$name] . '.';
throw new Exception ($msg);
}

   return $values[$name] = $value;

   }

  if ($is_mutable)  $mutables[] = $name; // Set new
 value
  $values[$name] = $value;

  return $value;

 }


 I always add blank lines for clarity. Remove those and the above is 30%
 shorter than yours - as far as I could tell, none of the else clauses was
 required.

 My approach is:

 1) deal with the trivial and error cases first

 2) deal with the real work next

 --
 Cheers  --  Tim


Thanks for providing your example, Tim. Bailing early through guard clauses
towards the top of the function body is a nice approach (I used it in my
second example, but I also used conditional grouping and factoring out
functions so I could display all three techniques listed in the post.)

I might try performing some experiments using the different versions of the
code and test for things like:
- Time it takes to add some additional piece of functionality to the code.
- Number of bugs in the revision.
- Time it takes for one to write a new function using only one of the
possible techniques (deep nesting, guard clauses, pulling out functions,
goto, etc.)
- Providing function input and testing accuracy of predicted output

Thanks for the time you've taken to provide your PHP coding preference in
this situation.

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com