Re: Re: [PHP] Multiple SQLite statements

2011-10-11 Thread Tim Streater
On 11 Oct 2011 at 03:03, Paul M Foster pa...@quillandmouse.com wrote: 

 On Mon, Oct 10, 2011 at 04:14:00PM +0100, Tim Streater wrote:

 I would like to use the SQLite3 (not PDO) interface to SQLite, and I
 would like to be able to supply a string containing several SQL
 statements and have them all executed, thus saving the overhead of
 several calls. It *appears* that this may be how it actually works,
 but I wondered if anyone could confirm that.

 --
 Cheers  --  Tim


 The docs appear to agree that this is allowed. See:

 http://us.php.net/manual/en/function.sqlite-exec.php

That's the SQLite interface, though, rather than the SQLite3 one. The latter 
just says: Executes an SQL query 

--
Cheers  --  Tim

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

Re: Re: [PHP] Multiple SQLite statements

2011-10-11 Thread David Robley
Tim Streater wrote:

 On 11 Oct 2011 at 03:03, Paul M Foster pa...@quillandmouse.com wrote:
 
 On Mon, Oct 10, 2011 at 04:14:00PM +0100, Tim Streater wrote:

 I would like to use the SQLite3 (not PDO) interface to SQLite, and I
 would like to be able to supply a string containing several SQL
 statements and have them all executed, thus saving the overhead of
 several calls. It *appears* that this may be how it actually works,
 but I wondered if anyone could confirm that.

 --
 Cheers  --  Tim


 The docs appear to agree that this is allowed. See:

 http://us.php.net/manual/en/function.sqlite-exec.php
 
 That's the SQLite interface, though, rather than the SQLite3 one. The
 latter just says: Executes an SQL query 
 
 --
 Cheers  --  Tim

Not to be a smartass or anything, but what about TIAS ?


Cheers
-- 
David Robley

I don't eat snails... I prefer FAST food!
Today is Prickle-Prickle, the 65th day of Bureaucracy in the YOLD 3177. 


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



Re: Re: Re: [PHP] Multiple SQLite statements

2011-10-11 Thread Tim Streater
On 11 Oct 2011 at 10:47, David Robley robl...@aapt.net.au wrote: 

 Tim Streater wrote:

 On 11 Oct 2011 at 03:03, Paul M Foster pa...@quillandmouse.com wrote:

 On Mon, Oct 10, 2011 at 04:14:00PM +0100, Tim Streater wrote:

 I would like to use the SQLite3 (not PDO) interface to SQLite, and I
 would like to be able to supply a string containing several SQL
 statements and have them all executed, thus saving the overhead of
 several calls. It *appears* that this may be how it actually works,
 but I wondered if anyone could confirm that.

 The docs appear to agree that this is allowed. See:

 http://us.php.net/manual/en/function.sqlite-exec.php

 That's the SQLite interface, though, rather than the SQLite3 one. The
 latter just says: Executes an SQL query 

 Not to be a smartass or anything, but what about TIAS ?

What that?

--
Cheers  --  Tim

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

Re: Re: Re: [PHP] Multiple SQLite statements

2011-10-11 Thread David Robley
Tim Streater wrote:

 On 11 Oct 2011 at 10:47, David Robley robl...@aapt.net.au wrote:
 
 Tim Streater wrote:

 On 11 Oct 2011 at 03:03, Paul M Foster pa...@quillandmouse.com wrote:

 On Mon, Oct 10, 2011 at 04:14:00PM +0100, Tim Streater wrote:

 I would like to use the SQLite3 (not PDO) interface to SQLite, and I
 would like to be able to supply a string containing several SQL
 statements and have them all executed, thus saving the overhead of
 several calls. It *appears* that this may be how it actually works,
 but I wondered if anyone could confirm that.
 
 The docs appear to agree that this is allowed. See:

 http://us.php.net/manual/en/function.sqlite-exec.php

 That's the SQLite interface, though, rather than the SQLite3 one. The
 latter just says: Executes an SQL query 
 
 Not to be a smartass or anything, but what about TIAS ?
 
 What that?
 
 --
 Cheers  --  Tim

Er, Try It And See

A couple of minutes experimentation might have saved you the time of email,
wait for an answer ...


Cheers
-- 
David Robley

Mothers are the necessity of invention -- Calvin
Today is Prickle-Prickle, the 65th day of Bureaucracy in the YOLD 3177. 


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



Re: Re: Re: Re: [PHP] Multiple SQLite statements

2011-10-11 Thread Tim Streater
On 11 Oct 2011 at 11:25, David Robley robl...@aapt.net.au wrote: 

 Tim Streater wrote:

 On 11 Oct 2011 at 10:47, David Robley robl...@aapt.net.au wrote:

 Tim Streater wrote:

 On 11 Oct 2011 at 03:03, Paul M Foster pa...@quillandmouse.com wrote:

 On Mon, Oct 10, 2011 at 04:14:00PM +0100, Tim Streater wrote:

 I would like to use the SQLite3 (not PDO) interface to SQLite, and I
 would like to be able to supply a string containing several SQL
 statements and have them all executed, thus saving the overhead of
 several calls. It *appears* that this may be how it actually works,
 but I wondered if anyone could confirm that.

 The docs appear to agree that this is allowed. See:

 http://us.php.net/manual/en/function.sqlite-exec.php

 That's the SQLite interface, though, rather than the SQLite3 one. The
 latter just says: Executes an SQL query 

 Not to be a smartass or anything, but what about TIAS ?

 What that?

 Er, Try It And See

 A couple of minutes experimentation might have saved you the time of email,
 wait for an answer ...

Well, there is an sqlite3 executable that one can run to do CLI things to a 
database. OS X comes with that and I was also able to download the source of 
that program, and the SQLite C amalgamation, and rebuild it myself. It is 
certainly possible, with that program, to execute a sequence of semi-colon 
separated statements. It *doesn't* work with PHP's PDO interface to sqlite, as 
I found in a test program I put together; I haven't properly tested that with 
the sqlite3 interface. I've tried asking on the sqlite general mailing list and 
(to me at least), the answers are at best unclear. There is a function, part of 
the C interface to sqlite, that talks about a sequence of statements, but I 
guess ultimately it depends on how the writer of the PHP sqlite3 interface 
implemented it.

--
Cheers  --  Tim

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

[PHP] Multiple SQLite statements

2011-10-10 Thread Tim Streater
I would like to use the SQLite3 (not PDO) interface to SQLite, and I would like 
to be able to supply a string containing several SQL statements and have them 
all executed, thus saving the overhead of several calls. It *appears* that this 
may be how it actually works, but I wondered if anyone could confirm that.

--
Cheers  --  Tim

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

Re: [PHP] Multiple SQLite statements

2011-10-10 Thread Paul M Foster
On Mon, Oct 10, 2011 at 04:14:00PM +0100, Tim Streater wrote:

 I would like to use the SQLite3 (not PDO) interface to SQLite, and I
 would like to be able to supply a string containing several SQL
 statements and have them all executed, thus saving the overhead of
 several calls. It *appears* that this may be how it actually works,
 but I wondered if anyone could confirm that.
 
 --
 Cheers  --  Tim
 

The docs appear to agree that this is allowed. See:

http://us.php.net/manual/en/function.sqlite-exec.php

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

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



Re: [PHP] Multiple return statements in a function.

2009-04-24 Thread Richard Heyes
Hi,

 your function could be condensed to this:

 function check($a)
 {
    return is_array($a) ? true : false;
 }

Or even better, this:

function check($a)
{
   return is_array($a);
}

Not that I'd imagine it makes a great deal of difference.

-- 
Richard Heyes
HTML5 graphing: RGraph (www.rgraph.net)
PHP mail: RMail (www.phpguru.org/rmail)
PHP datagrid: RGrid (www.phpguru.org/rgrid)
PHP Template: RTemplate (www.phpguru.org/rtemplate)

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



[PHP] Multiple return statements in a function.

2009-04-23 Thread Peter van der Does
I tend to put my return value in a variable and at the end of the
function I have 1 return statement.
I have seen others doing returns in the middle of the function.

Example how I do it:
function check($a) {
  $return='';
  if ( is_array( $a ) ) {
$return='Array';
  } else {
$return='Not Array';
  }
  return $return;
}

Example of the other method:
function check($a) {

  if ( is_array( $a ) ) {
return ('Array');
  } else {
return ('Not Array');
  }
}

What is your take? And is there any benefit to either method?


-- 
Peter van der Does

GPG key: E77E8E98
Blog: http://blog.avirtualhome.com
Forums: http://forums.avirtualhome.com
Jabber ID: pvanderd...@gmail.com

GetDeb Package Builder
http://www.getdeb.net - Software you want for Ubuntu

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Per Jessen
Peter van der Does wrote:

 I tend to put my return value in a variable and at the end of the
 function I have 1 return statement.
 I have seen others doing returns in the middle of the function.
 
 Example how I do it:
 function check($a) {
   $return='';
   if ( is_array( $a ) ) {
 $return='Array';
   } else {
 $return='Not Array';
   }
   return $return;
 }
 
 Example of the other method:
 function check($a) {
 
   if ( is_array( $a ) ) {
 return ('Array');
   } else {
 return ('Not Array');
   }
 }
 
 What is your take? And is there any benefit to either method?

It's only about style and coding logic.  In essence, all the return does
is pop the previous IP off the stack and adjust the stack pointer.  It
doesn't matter where you do that.


/Per
 

-- 
Per Jessen, Zürich (16.2°C)


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread George Larson
On Thu, Apr 23, 2009 at 8:25 AM, Per Jessen p...@computer.org wrote:
 Peter van der Does wrote:

 I tend to put my return value in a variable and at the end of the
 function I have 1 return statement.
 I have seen others doing returns in the middle of the function.

 Example how I do it:
 function check($a) {
   $return='';
   if ( is_array( $a ) ) {
 $return='Array';
   } else {
 $return='Not Array';
   }
   return $return;
 }

 Example of the other method:
 function check($a) {

   if ( is_array( $a ) ) {
 return ('Array');
   } else {
 return ('Not Array');
   }
 }

 What is your take? And is there any benefit to either method?

 It's only about style and coding logic.  In essence, all the return does
 is pop the previous IP off the stack and adjust the stack pointer.  It
 doesn't matter where you do that.


 /Per


 --
 Per Jessen, Zürich (16.2°C)


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



That's an interesting subject that I've never considered.

I usually return immediately.  For me, it makes the code easier to
read.  I work with a number of other coders here and, if the result
isn't returned then I have to keep reading through the code to make
sure nothing else is done with it.  However, when I see the 'return'
then I know we're done there.

That said, I often see questionable coding practices in use at work.
I follow this list (and try to read books about the technologies I
use) because I intend to develop good practices for myself.  That in
mind, if anybody feels strongly about doing it the other way, I'd be
interested in understanding its benefits.

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Paul M Foster
On Thu, Apr 23, 2009 at 08:12:47AM -0400, Peter van der Does wrote:

 I tend to put my return value in a variable and at the end of the
 function I have 1 return statement.
 I have seen others doing returns in the middle of the function.
 
 Example how I do it:
 function check($a) {
   $return='';
   if ( is_array( $a ) ) {
 $return='Array';
   } else {
 $return='Not Array';
   }
   return $return;
 }
 
 Example of the other method:
 function check($a) {
 
   if ( is_array( $a ) ) {
 return ('Array');
   } else {
 return ('Not Array');
   }
 }
 
 What is your take? And is there any benefit to either method?

As mentioned, this is a matter of style. However, in a lot of cases, in
order to get the return at the very end, it's necessary to put endless
if/else pairs in the function. That makes it hard to read the source.
I'm sure there are purists out there who would insist the single return
go at the end. But expediency and readability may dictate otherwise.

Paul
-- 
Paul M. Foster

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Tony Marston

tedd tedd.sperl...@gmail.com wrote in message 
news:p06240805c6161613a...@[192.168.1.101]...
 At 8:12 AM -0400 4/23/09, Peter van der Does wrote:
I tend to put my return value in a variable and at the end of the
function I have 1 return statement.
I have seen others doing returns in the middle of the function.

-snip-

What is your take? And is there any benefit to either method?

Peter van der Does

 Peter:

 It's called Structured programming -- one way in and one way out of a 
 function.

 There are, of course, exceptions where it might help others reviewing your 
 code to see what's going on, such as returning a null value if the 
 argument(s) provided are not suitable. But normally the rule is, do not 
 provide an exit from a function in more than one place.

There is no such rule, it is a matter of personal preference. As a 
previous poster has already said, if you want to leave a function early and 
ignore all subsequent processing it is easier to understand if you return 
immediately rather than have a mechanism to jump over the remaining code to 
a single return point. In the good old days we used to use the GOTO in COBOL 
to jump to the exit point, but then people found a way to abuse GOTO in very 
imaginatve ways.

 The benefit is easier to read code.

I think that an immediate return is easier to read, but what do I know - 
I've only been programming for 30 years.

-- 
Tony Marston
http://www.tonymarston.net
http://www.radicore.org

 Cheers,

 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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Per Jessen
George Larson wrote:

 That's an interesting subject that I've never considered.
 
 I usually return immediately.  For me, it makes the code easier to
 read.  I work with a number of other coders here and, if the result
 isn't returned then I have to keep reading through the code to make
 sure nothing else is done with it.  However, when I see the 'return'
 then I know we're done there.

I tend to try to have just one return point, but I will occasionally
have more, typically when a function returns more than just true or
false.  
If it's a true or false outcome, but I still have multiple failure
points, I'll sometimes use a construct like this:


rc=0;
while(true){

  if ( cond1 ) { errormsg; rc=1; break; }
  if ( cond2 ) { errormsg; rc=1; break; }
  if ( cond3 ) { errormsg; rc=1; break; }
  if ( cond4 ) { errormsg; rc=1; break; }
  if ( cond5 ) { errormsg; rc=1; break; }
  if ( cond6 ) { errormsg; rc=1; break; }
  if ( cond7 ) { errormsg; rc=1; break; }

  break;
}

return rc;



-- 
Per Jessen, Zürich (16.6°C)


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread tedd

At 8:12 AM -0400 4/23/09, Peter van der Does wrote:

I tend to put my return value in a variable and at the end of the
function I have 1 return statement.
I have seen others doing returns in the middle of the function.

-snip-

What is your take? And is there any benefit to either method?

Peter van der Does


Peter:

It's called Structured programming -- one way in and one way out of 
a function.


There are, of course, exceptions where it might help others reviewing 
your code to see what's going on, such as returning a null value if 
the argument(s) provided are not suitable. But normally the rule is, 
do not provide an exit from a function in more than one place.


The benefit is easier to read code.

Cheers,

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Richard Heyes
Hi,

 while(true){

Yikes.

Personally, I'd put the return value wherever it will make the code
easier to read. If you're checking what has been passed as arguments,
and one of them is wrong, I think there's little point in continuing,
so an immediate return is the order of the day. Though with
exceptions, this could be mitigated (IIRC). BTW there's also something
to be said for code conciseness, which I think is loosely related. Eg
your function could be condensed to this:

function check($a)
{
return is_array($a) ? true : false;
}

But then the question is nullified somewhat.

-- 
Richard Heyes
HTML5 graphing: RGraph (www.rgraph.net)
PHP mail: RMail (www.phpguru.org/rmail)
PHP datagrid: RGrid (www.phpguru.org/rgrid)
PHP Template: RTemplate (www.phpguru.org/rtemplate)

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Richard Quadling
2009/4/23 Tony Marston t...@marston-home.demon.co.uk:

 tedd tedd.sperl...@gmail.com wrote in message
 news:p06240805c6161613a...@[192.168.1.101]...
 At 8:12 AM -0400 4/23/09, Peter van der Does wrote:
I tend to put my return value in a variable and at the end of the
function I have 1 return statement.
I have seen others doing returns in the middle of the function.

-snip-

What is your take? And is there any benefit to either method?

Peter van der Does

 Peter:

 It's called Structured programming -- one way in and one way out of a
 function.

 There are, of course, exceptions where it might help others reviewing your
 code to see what's going on, such as returning a null value if the
 argument(s) provided are not suitable. But normally the rule is, do not
 provide an exit from a function in more than one place.

 There is no such rule, it is a matter of personal preference. As a
 previous poster has already said, if you want to leave a function early and
 ignore all subsequent processing it is easier to understand if you return
 immediately rather than have a mechanism to jump over the remaining code to
 a single return point. In the good old days we used to use the GOTO in COBOL
 to jump to the exit point, but then people found a way to abuse GOTO in very
 imaginatve ways.

 The benefit is easier to read code.

 I think that an immediate return is easier to read, but what do I know -
 I've only been programming for 30 years.

 --
 Tony Marston
 http://www.tonymarston.net
 http://www.radicore.org

 Cheers,

 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



As someone's who been playing this game for only 29 years, I like a
single exit point.

One of the issues I found with multiple exit points is that you have
to work a little harder to guarantee a return.

I always write my code so that if the function did nothing, then null
or false are the return values ...

function foo() {
 $result = False;
// ... code which may or may nor affect $result
 return $result;
}

If your code is heavily nested (maybe an indicator that some
refactoring is worth undertaking) and you introduce yet more code,
keeping track of multiple exit points can be a little problematic.

I suppose if you are the only developer then create a style and stick with it.

Don't be afraid of refactoring your code if you feel that a different
style is easier to work with.

If you work in a team, then once the team has determined its style,
stick with it. Even if it hurts. The hassle you'll get when your code
doesn't fit is far more than the learning curve you'll go through
getting the style.

Richard.


-- 
-
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
Standing on the shoulders of some very clever giants!

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Per Jessen
Richard Heyes wrote:

 Hi,
 
 while(true){
 
 Yikes.
 
 Personally, I'd put the return value wherever it will make the code
 easier to read. If you're checking what has been passed as arguments,
 and one of them is wrong, I think there's little point in continuing,
 so an immediate return is the order of the day.

Well, I usually use that construct where I can have a number of
different error-conditions - e.g. if I have a sequence of socket
create, DNS lookup, bind, connect  etc. 


/Per

-- 
Per Jessen, Zürich (15.6°C)


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread tedd

At 2:19 PM +0100 4/23/09, Tony Marston wrote:

tedd tedd.sperl...@gmail.com wrote in message
  It's called Structured programming -- one way in and one way out of a

 function.

 There are, of course, exceptions where it might help others reviewing your
 code to see what's going on, such as returning a null value if the
 argument(s) provided are not suitable. But normally the rule is, do not
 provide an exit from a function in more than one place.


There is no such rule, it is a matter of personal preference. As a
previous poster has already said, if you want to leave a function early and
ignore all subsequent processing it is easier to understand if you return
immediately rather than have a mechanism to jump over the remaining code to
a single return point. In the good old days we used to use the GOTO in COBOL
to jump to the exit point, but then people found a way to abuse GOTO in very
imaginatve ways.


 The benefit is easier to read code.


I think that an immediate return is easier to read, but what do I know -
I've only been programming for 30 years.

--
Tony Marston


Tony:

Don't get your panties in a knot. :-)

I have 44 years of programming under my belt, so what? However, I 
wish I could remember everything I learned during that time.


But what I do remember is there's a school of thought called 
Structured Programming that has a doctrine in which every function 
should have only one entry and exit point.


Now maybe you want to argue with that concept, that's fine -- but the 
point remains this is a rule under Structured Programming. That's 
history.


Now, I usually follow that rule for I have learned from experience 
that in most cases, it is easier to read what is going on in a 
function if you only have one exit.


As I said in my post, there are of course exceptions. There are times 
that requiring a function to have a single exit point will 
unnecessarily create code that's hard to read -- so doing it 
differently is something to consider in those cases. If you want to 
have an immediate exit point in your functions and that makes your 
code more readable, then that's fine and I'm not arguing that point.


However, I am saying (after years of reading other people's code) it 
is generally much easier to read code that follows Structured 
Programming than it is to read code that doesn't.


Cheers,

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Robert Cummings
On Thu, 2009-04-23 at 10:14 -0400, tedd wrote:
 At 2:19 PM +0100 4/23/09, Tony Marston wrote:
 tedd tedd.sperl...@gmail.com wrote in message
It's called Structured programming -- one way in and one way out of a
   function.
 
   There are, of course, exceptions where it might help others reviewing your
   code to see what's going on, such as returning a null value if the
   argument(s) provided are not suitable. But normally the rule is, do not
   provide an exit from a function in more than one place.
 
 There is no such rule, it is a matter of personal preference. As a
 previous poster has already said, if you want to leave a function early and
 ignore all subsequent processing it is easier to understand if you return
 immediately rather than have a mechanism to jump over the remaining code to
 a single return point. In the good old days we used to use the GOTO in COBOL
 to jump to the exit point, but then people found a way to abuse GOTO in very
 imaginatve ways.
 
   The benefit is easier to read code.
 
 I think that an immediate return is easier to read, but what do I know -
 I've only been programming for 30 years.
 
 --
 Tony Marston
 
 Tony:
 
 Don't get your panties in a knot. :-)
 
 I have 44 years of programming under my belt, so what? However, I 
 wish I could remember everything I learned during that time.
 
 But what I do remember is there's a school of thought called 
 Structured Programming that has a doctrine in which every function 
 should have only one entry and exit point.
 
 Now maybe you want to argue with that concept, that's fine -- but the 
 point remains this is a rule under Structured Programming. That's 
 history.
 
 Now, I usually follow that rule for I have learned from experience 
 that in most cases, it is easier to read what is going on in a 
 function if you only have one exit.
 
 As I said in my post, there are of course exceptions. There are times 
 that requiring a function to have a single exit point will 
 unnecessarily create code that's hard to read -- so doing it 
 differently is something to consider in those cases. If you want to 
 have an immediate exit point in your functions and that makes your 
 code more readable, then that's fine and I'm not arguing that point.
 
 However, I am saying (after years of reading other people's code) it 
 is generally much easier to read code that follows Structured 
 Programming than it is to read code that doesn't.

Actually I use an early return as much as possible also. If there are 5
conditions which disqualify the rest of the function from running then
they'll all be listed one after the other at the top of the function.
This way it's very easy to see exactly what doesn't qualify for
evaluation. Additionally, it saves on the need for that many levels of
indentation or making an overly complex conditional. I find it much more
intuitive than mentally tracking several levels of indentation and
scrolling to the bottom of the function.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread Paul M Foster
On Thu, Apr 23, 2009 at 10:14:28AM -0400, tedd wrote:

 At 2:19 PM +0100 4/23/09, Tony Marston wrote:

snip

 There is no such rule, it is a matter of personal preference. As a
 previous poster has already said, if you want to leave a function early 
 and
 ignore all subsequent processing it is easier to understand if you return
 immediately rather than have a mechanism to jump over the remaining code 
 to
 a single return point. In the good old days we used to use the GOTO in 
 COBOL
 to jump to the exit point, but then people found a way to abuse GOTO in 
 very
 imaginatve ways.

  The benefit is easier to read code.

 I think that an immediate return is easier to read, but what do I know -
 I've only been programming for 30 years.

 --
 Tony Marston

 Tony:

 Don't get your panties in a knot. :-)

I think in Tony's case, it would knickers in a twist. ;-}

Paul

-- 
Paul M. Foster

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



Re: [PHP] Multiple return statements in a function.

2009-04-23 Thread tedd

At 10:25 AM -0400 4/23/09, Robert Cummings wrote:

On Thu, 2009-04-23 at 10:14 -0400, tedd wrote:
  However, I am saying (after years of reading other people's code) it

 is generally much easier to read code that follows Structured
 Programming than it is to read code that doesn't.


Actually I use an early return as much as possible also. If there are 5
conditions which disqualify the rest of the function from running then
they'll all be listed one after the other at the top of the function.
This way it's very easy to see exactly what doesn't qualify for
evaluation. Additionally, it saves on the need for that many levels of
indentation or making an overly complex conditional. I find it much more
intuitive than mentally tracking several levels of indentation and
scrolling to the bottom of the function.

Cheers,
Rob.


Rob:

Again, I'm not arguing the point -- we actually agree.

I have functions where I do that as well. My only criteria is how 
easy my functions are to read and understand.


If by placing function entry-criteria at the beginning of the 
function and then returning nulls (or whatever) makes it easier for 
someone to review your code and understand what it does, then by all 
means do it.


But I am sure that both of you have reviewed functions that have 
multiple returns spread throughout where you thought What the hell 
is this? Just what is this function returning? That is what I am 
addressing.


The OP asked which was the better practice and I replied that it is 
USUALLY good practice to have only one return and have it at the end 
of your function -- I stand by that.


Cheers,

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



Re: [PHP] Multiple if() statements

2006-06-30 Thread tedd
At 11:07 PM -0600 6/29/06, John Meyer wrote:
Larry Garfield wrote:

switch is fine if your elseif comparisons are equality based.  If they're not 
equality based, then they don't map to switch as well.

In other words, if you look at a logical ladder as the roots of the tree, as 
long as each root has the same number of forks (say each fork ends only one 
way), your fine with a switch.  If you have one, however, that has only one 
condition, and another that has  two, then you need an if...elseif logic tree.

Interesting -- can you give me an example?

Perhaps I'm showing my ignorance, but I never ran into an elseif problem that I 
couldn't better solve (for me) with a switch. I've never *had* to use one.

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



Re: [PHP] Multiple if() statements

2006-06-30 Thread John Wells

On 6/30/06, tedd [EMAIL PROTECTED] wrote:

At 11:07 PM -0600 6/29/06, John Meyer wrote:
Larry Garfield wrote:

switch is fine if your elseif comparisons are equality based.  If they're not 
equality based, then they don't map to switch as well.



Not true.  I've come to really appreciate the structure of switches.
What helps to unlock their power is that you don't *have* to compare
your switch() param with your case() params.

Building on Paul's good advice of splitting logic from presentation:


[code]

$display_name = '';

switch('set_display_name')
{
case($row[1] == 'none'):
case($row[1] == $row[2]):
$display_name = $row[0] . ' ' . $row[2];
break;

default:
$display_name = $row[0] . ' (' . $row[1] . ') ' . $row[2];
break;

}

// now print table, using $display_name
[/code]

HTH,
John W

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



RE: [PHP] Multiple if() statements

2006-06-30 Thread Ford, Mike
On 30 June 2006 13:37, tedd wrote:

 At 11:07 PM -0600 6/29/06, John Meyer wrote:
  Larry Garfield wrote:
   
   switch is fine if your elseif comparisons are equality
 based.  If they're not equality based, then they don't map to
 switch as well.
  
  In other words, if you look at a logical ladder as the roots
 of the tree, as long as each root has the same number of
 forks (say each fork ends only one way), your fine with a
 switch.  If you have one, however, that has only one
 condition, and another that has  two, then you need an
 if...elseif logic tree.
 
 Interesting -- can you give me an example?

How about this -- a paraphrase of which occurs in many of my scripts:

   if (isset($_POST['id'])):
  $id = $_POST['id'];
  // etc. -- other initializations based on $id
   elseif ($_POST['action']=='add'):
  $id = generate_id();
  // initialize stuff to empty values
   elseif (potential_other_test_to_detect_other_valid_states()):
  // other stuff
   else:
  KaBlooie();
   endif;

Of course, that *could* still be implemented using the switch(TRUE) technique 
(and I've used that elsewhere), but in a case like this instance I prefer the 
if/elseif construct.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] Multiple if() statements

2006-06-30 Thread Richard Lynch
On Thu, June 29, 2006 11:07 pm, Larry Garfield wrote:
 switch is fine if your elseif comparisons are equality based.  If
 they're not
 equality based, then they don't map to switch as well.

Except in PHP which supports:

switch(TRUE) {
  case _boolean_expression_:
  break;
}

So you can have case statements of whatever complexity is appropriate.

-- 
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] Multiple if() statements

2006-06-30 Thread tedd
At 4:26 PM +0100 6/30/06, Ford, Mike wrote:
On 30 June 2006 13:37, tedd wrote:

 At 11:07 PM -0600 6/29/06, John Meyer wrote:
  Larry Garfield wrote:
  
  [1] switch is fine if your elseif comparisons are equality
  based.  If they're not equality based, then they don't map to
 switch as well.
 
   [2] In other words, if you look at a logical ladder as the roots
  of the tree, as long as each root has the same number of
 forks (say each fork ends only one way), your fine with a
 switch.  If you have one, however, that has only one
 condition, and another that has  two, then you need an
 if...elseif logic tree.

 Interesting -- can you give me an example?

How about this -- a paraphrase of which occurs in many of my scripts:

   if (isset($_POST['id'])):
  $id = $_POST['id'];
  // etc. -- other initializations based on $id
   elseif ($_POST['action']=='add'):
  $id = generate_id();
  // initialize stuff to empty values
   elseif (potential_other_test_to_detect_other_valid_states()):
  // other stuff
   else:
  KaBlooie();
   endif;

Of course, that *could* still be implemented using the switch(TRUE) technique 
(and I've used that elsewhere), but in a case like this instance I prefer the 
if/elseif construct.


I think the point here is that you said [1] and [2], but you still haven't 
provided an example.

The following works just as well as your elseif example.

switch (TRUE)
   {
   case isset($_POST['id']:
  $id = $_POST['id'];
  break;

   case $_POST['action']=='add':
  $id = generate_id();
  break;

   case  (potential_other_test_to_detect_other_valid_states()):
  break;

   default  KaBlooie();
  break;
   }

Additionally, in my timed tests, I find both control structures to be generally 
equal in speed -- so I don't find any support for elsif being better matched 
than switch in any equality based computations.

So, I believe the choice to use one control structure in preference to the 
other is purely a personal one -- and one that has no support stemming from any 
performance differences, because there are none.

Thanks for your reply.

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



RE: [PHP] Multiple if() statements

2006-06-29 Thread Ford, Mike
On 29 June 2006 01:03, David Tulloh wrote:


 I'm also going to throw in an elseif for fun, to get this (hopefully)
 improved version: 
 
 if($row[1] == none) {
print(tr);
print(td$row[0] $row[2]/td);
print(/tr);
 } elseif($row[1] == $row[2]) {
print(tr);
print(td$row[0] $row[2]/td);
print(/tr);
 } else {
print(tr);
print(td$row[0] ($row[1]) $row[2]/td);
print(/tr);
 }

This still seems overly complex to me -- there are 3 identical occurrences of 
some items.

When constructing an if() sequence, I think it's always important to isolate 
the parts that genuinely differ, so my effort would go like this:

echo tr;
echo td$row[0] ;
if ($row[1] != none  $row[1] != $row[2]) {
   echo ($row[1]) ;
}
echo $row[2]/td;
echo /tr;


Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] Multiple if() statements

2006-06-29 Thread tedd
At 8:15 PM -0400 6/28/06, Robert Cummings wrote:
On Wed, 2006-06-28 at 20:02, David Tulloh wrote:
 Grae Wolfe - PHP wrote:
  ...
   want.  Any help would be great!


-snip- if/elseif -snip-


holy war id=opinion

Whenever you need a elseif, then it's time to consider a switch -- like thus:

print( tr );

switch $row[1]
{
case: none;
print( td$row[0] $row[2]/td );
break;

case: $row[2];
print( td$row[0] $row[2]/td );
break;

default:
print( td$row[0] ($row[1]) $row[2]/td );
break;
}

print( /tr );

humor id=mine

 /* Please note the humor tags, smiley and apology beforehand.
No offense meant to anyone -- insert  apology where needed -- and appropriate.
Your mileage may vary, Take only as prescribed by doctor's advice. No hablo 
inglés */

To me, the switch statement is so much simpler -- what's with you guys and 
these long-ass and confusing if/elseif structures?  :-)

/humor id=mine

/holy war id=opinion

tedd limping off to his bear cave awaiting fallout/.

--

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



Re: [PHP] Multiple if() statements

2006-06-29 Thread Larry Garfield
On Thursday 29 June 2006 06:51, tedd wrote:
 At 8:15 PM -0400 6/28/06, Robert Cummings wrote:
 On Wed, 2006-06-28 at 20:02, David Tulloh wrote:
  Grae Wolfe - PHP wrote:
   ...
  
want.  Any help would be great!

 -snip- if/elseif -snip-


 holy war id=opinion

 Whenever you need a elseif, then it's time to consider a switch -- like
 thus:

switch is fine if your elseif comparisons are equality based.  If they're not 
equality based, then they don't map to switch as well.

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

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



Re: [PHP] Multiple if() statements

2006-06-29 Thread John Meyer

Larry Garfield wrote:


switch is fine if your elseif comparisons are equality based.  If they're not 
equality based, then they don't map to switch as well.


In other words, if you look at a logical ladder as the roots of the 
tree, as long as each root has the same number of forks (say each fork 
ends only one way), your fine with a switch.  If you have one, however, 
that has only one condition, and another that has  two, then you need an 
if...elseif logic tree.




--
Online library -- http://pueblonative.110mb.com
138 books and counting.

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



[PHP] Multiple if() statements

2006-06-28 Thread Grae Wolfe - PHP
I have a table with lots of fun information in it.  For one of the pages 
that I am working on, I want to display a list of names based on what is in 
the DB.
  My $SQL statement works great and has pulled in values for first_name, 
hs_last_name, and last_name in that order.  Right now, I am just 
printing to the screen the results of first_name (hs_last_name) last_name 
but there are two problems with this...
  The first problem is men's names and unmarried women's names...  they will 
have the same hs_last_name and last_name so I don't want the duplicate 
displaying on the page.
  The second problem is the entry of the word none by some of the visitors 
in place of a hs_last_name...  obviously I don't want to display this 
either.
  The following is the string of IF() statements that I am using, but I 
have an issue with my syntax somewhere because it isn't working the way I 
want.  Any help would be great!


  if($row[1]=none) {
  print(tr);
  print(td$row[0] $row[2]/td);
  print(/tr);
  } else
  if($row[1]=$row[2]) {
  print(tr);
  print(td$row[0] $row[2]/td);
  print(/tr);
  } else
 print(tr);
  print(td$row[0] ($row[1]) $row[2]/td);
  print(/tr);

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



Re: [PHP] Multiple if() statements

2006-06-28 Thread David Tulloh
Grae Wolfe - PHP wrote:
 ...
 want.  Any help would be great!
 
 
   if($row[1]=none) {
   print(tr);
   print(td$row[0] $row[2]/td);
   print(/tr);
   } else
   if($row[1]=$row[2]) {
   print(tr);
   print(td$row[0] $row[2]/td);
   print(/tr);
   } else
  print(tr);
   print(td$row[0] ($row[1]) $row[2]/td);
   print(/tr);
 

Indenting is your friend, indented version of what you had.

if($row[1]=none) {
   print(tr);
   print(td$row[0] $row[2]/td);
   print(/tr);
} else
   if($row[1]=$row[2]) {
 print(tr);
 print(td$row[0] $row[2]/td);
 print(/tr);
   } else
 print(tr);
print(td$row[0] ($row[1]) $row[2]/td);
print(/tr);

The bigest problem with the above is that both the else becomes unclear
when they finish due to the lack of {}.
The ifs should also be using an == instead of an =, you want to compare
not assign.

I'm also going to throw in an elseif for fun, to get this (hopefully)
improved version:

if($row[1] == none) {
   print(tr);
   print(td$row[0] $row[2]/td);
   print(/tr);
} elseif($row[1] == $row[2]) {
   print(tr);
   print(td$row[0] $row[2]/td);
   print(/tr);
} else {
   print(tr);
   print(td$row[0] ($row[1]) $row[2]/td);
   print(/tr);
}

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



Re: [PHP] Multiple if() statements

2006-06-28 Thread Robert Cummings
On Wed, 2006-06-28 at 20:02, David Tulloh wrote:
 Grae Wolfe - PHP wrote:
  ...
  want.  Any help would be great!
  
  
if($row[1]=none) {
print(tr);
print(td$row[0] $row[2]/td);
print(/tr);
} else
if($row[1]=$row[2]) {
print(tr);
print(td$row[0] $row[2]/td);
print(/tr);
} else
   print(tr);
print(td$row[0] ($row[1]) $row[2]/td);
print(/tr);
  
 
 Indenting is your friend, indented version of what you had.
 
 if($row[1]=none) {
print(tr);
print(td$row[0] $row[2]/td);
print(/tr);
 } else
if($row[1]=$row[2]) {
  print(tr);
  print(td$row[0] $row[2]/td);
  print(/tr);
} else
  print(tr);
 print(td$row[0] ($row[1]) $row[2]/td);
 print(/tr);
 
 The bigest problem with the above is that both the else becomes unclear
 when they finish due to the lack of {}.
 The ifs should also be using an == instead of an =, you want to compare
 not assign.
 
 I'm also going to throw in an elseif for fun, to get this (hopefully)
 improved version:
 
 if($row[1] == none) {
print(tr);
print(td$row[0] $row[2]/td);
print(/tr);
 } elseif($row[1] == $row[2]) {
print(tr);
print(td$row[0] $row[2]/td);
print(/tr);
 } else {
print(tr);
print(td$row[0] ($row[1]) $row[2]/td);
print(/tr);
 }

And for really clear code...

if( $row[1] == none )
{
print( tr );
print( td$row[0] $row[2]/td );
print( /tr );
}
else
if( $row[1] == $row[2] )
{
print( tr );
print( td$row[0] $row[2]/td );
print( /tr);
}
else
{
print( tr );
print( td$row[0] ($row[1]) $row[2]/td );
print( /tr );
}

Now you know when you've forgotten an opening brace and it lines up
beutifully vertically. Now I need to run and hide before a braces holy
war erupts *Whe*.

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] Multiple if() statements

2006-06-28 Thread Paul Novitski

At 04:38 PM 6/28/2006, Grae Wolfe - PHP wrote:

  The first problem is men's names and unmarried women's names...  they will
have the same hs_last_name and last_name so I don't want the duplicate
displaying on the page.
  The second problem is the entry of the word none by some of the visitors
in place of a hs_last_name...  obviously I don't want to display this
either.


  if($row[1]=none) {
  print(tr);
  print(td$row[0] $row[2]/td);
  print(/tr);
  } else
  if($row[1]=$row[2]) {
  print(tr);
  print(td$row[0] $row[2]/td);
  print(/tr);
  } else
 print(tr);
  print(td$row[0] ($row[1]) $row[2]/td);
  print(/tr);



Grae,

For ease of maintenance, and to reduce repetition in the code, I 
would separate the logic of your string concatenation from the HTML 
output, something like this:



$sName = $row[0];
if ($row[1] != none  $row[1] != $row[2]) $sName .= $row[1];
$sName .= $row[2];

$sHTML =  hdHTML
tr
td$sName/td
/tr

hdHTML;

print($sHTML);


Regards,
Paul 


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