By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,449 Members | 972 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,449 IT Pros & Developers. It's quick & easy.

Optimization in coding style

P: n/a
Hi there,

I was wondering is there a point in optimization in code style... I
have used to put parenthesis around some php keywords, but they are not
needed.

For example, I always use:
echo( "String..." );
but I could use
echo "String...";

the same thing is with return:
return( $tempHTML );
as opposed to
return $tempHTML;

Also is there a point to variate between string delimiters " and ' ?
ie. is it better to use single quotes instead of double quotes if I
don't won't any variable in a string? For example:
'This is a string...'
as opposed to
"This is a string...".

Double quotes are needed, of course in other cases:
"Frankly my dear, your name is ${nameOfTheUser}!"

I know it sounds stupid, even hard to read, but what are your opinions?

- Evanescent Lurker -

Jul 17 '05 #1
Share this Question
Share on Google+
25 Replies


P: n/a
ev***************@gmail.com says...
I was wondering is there a point in optimization in code style... I
have used to put parenthesis around some php keywords, but they are not
needed.

For example, I always use:
echo( "String..." );
but I could use
echo "String...";
Not needed, so don't use them.
the same thing is with return:
return( $tempHTML );
as opposed to
return $tempHTML;
Ditto.
Also is there a point to variate between string delimiters " and ' ?
ie. is it better to use single quotes instead of double quotes if I
don't won't any variable in a string? For example:
'This is a string...'
as opposed to
"This is a string...".
My preference is to use single quotes unless you are certain that the
string will surely have an apostrophe, and then decide whether it will be
better to escape the apostrophe or switch to double quotes.
Double quotes are needed, of course in other cases:
"Frankly my dear, your name is ${nameOfTheUser}!"
No they're not, and my strongly preferred style is to clearly string
escape and concatenate variables. For starters in my editor it is far
superior for syntax highlighting and debugging, even if it is more typing:

$string = 'My "nickname" is '.$myNickName.', what\'s yours?';
I know it sounds stupid, even hard to read, but what are your opinions?


IIRC there is also a (very slight) additional overhead is parsing double
quoted as opposed to single quoted strings.

Geoff M
Jul 17 '05 #2

P: n/a
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable Geoff Muldoon's handwriting:
My preference is to use single quotes unless you are certain that the
string will surely have an apostrophe, and then decide whether it will
be better to escape the apostrophe or switch to double quotes.


Agreed.
Double quotes are needed, of course in other cases:
"Frankly my dear, your name is ${nameOfTheUser}!"


No they're not, and my strongly preferred style is to clearly string
escape and concatenate variables. For starters in my editor it is far
superior for syntax highlighting and debugging, even if it is more
typing:

$string = 'My "nickname" is '.$myNickName.', what\'s yours?';

Hmmm... I'd say that it's a definetely less readable than:
$string="My \"nickname\" is $myNickName, what's yours?";

and if you can use single quotes to surround "nickname":
$string="My 'nickname' is $myNickName, what's yours?";

I really like to write readable code. Saves time and hassle.

Besides, don't know about yours, but my editor highlights the variables
surrounded by double-quotes as viariables.

Cheers
Mike
Jul 17 '05 #3

P: n/a
Michal Wozniak wrote:
<snip>
$string = 'My "nickname" is '.$myNickName.', what\'s yours?';

Hmmm... I'd say that it's a definetely less readable than:
$string="My \"nickname\" is $myNickName, what's yours?";

and if you can use single quotes to surround "nickname":
$string="My 'nickname' is $myNickName, what's yours?";

I really like to write readable code. Saves time and hassle.


His version is much better than yours. Nowadays, I'm annoyed when
someone uses double quotes:(

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #4

P: n/a
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable R. Rajesh Jeba Anbiah's handwriting:
His version is much better than yours. Nowadays, I'm annoyed when
someone uses double quotes:(


Why? Maybe I need to re-think my position?

Cheers
Mike

Jul 17 '05 #5

P: n/a
<ev***************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi there,

I was wondering is there a point in optimization in code style... I
have used to put parenthesis around some php keywords, but they are not
needed.
Not really. You don't get much saving from stripping them out. And when you
think about it, having the paratheses there makes it easier to replace
"echo" with a function.
Also is there a point to variate between string delimiters " and ' ?
ie. is it better to use single quotes instead of double quotes if I
don't won't any variable in a string? For example:
'This is a string...'
as opposed to
"This is a string...".


Again, not really. The saving is practically nil. Personally I use double
quotes for long sentences and single quotes from single letters or words.
Looks a little neater, I think.
Jul 17 '05 #6

P: n/a
"Geoff Muldoon" <gm************@scu.edu.au> wrote in message
news:MP************************@newsgate.x-privat.org...
No they're not, and my strongly preferred style is to clearly string
escape and concatenate variables. For starters in my editor it is far
superior for syntax highlighting and debugging, even if it is more typing:

$string = 'My "nickname" is '.$myNickName.', what\'s yours?';


The dot notation is confusing for people who jump between PHP and
Javascript.
Jul 17 '05 #7

P: n/a
Chung Leong wrote:
<snip>
Also is there a point to variate between string delimiters " and ' ? ie. is it better to use single quotes instead of double quotes if I
don't won't any variable in a string? For example:
'This is a string...'
as opposed to
"This is a string...".
Again, not really. The saving is practically nil. Personally I use

double quotes for long sentences and single quotes from single letters or words. Looks a little neater, I think.


It would be much better if you could benchmark the difference
before advocating the wrong practice *purely* because you use it.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #8

P: n/a
I noticed that Message-ID:
<11**********************@l41g2000cwc.googlegroups .com> from R. Rajesh
Jeba Anbiah contained the following:
Hmmm... I'd say that it's a definetely less readable than:
$string="My \"nickname\" is $myNickName, what's yours?";

and if you can use single quotes to surround "nickname":
$string="My 'nickname' is $myNickName, what's yours?";

I really like to write readable code. Saves time and hassle.


His version is much better than yours. Nowadays, I'm annoyed when
someone uses double quotes:(


better not look at any of my code then...

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9

P: n/a
For all those questions : http://www.php.lt/benchmark/phpbench.php

Jul 17 '05 #10

P: n/a
R. Rajesh Jeba Anbiah wrote:
Michal Wozniak wrote:
<snip>
$string = 'My "nickname" is '.$myNickName.', what\'s yours?';


Hmmm... I'd say that it's a definetely less readable than:
$string="My \"nickname\" is $myNickName, what's yours?";

and if you can use single quotes to surround "nickname":
$string="My 'nickname' is $myNickName, what's yours?";

I really like to write readable code. Saves time and hassle.

His version is much better than yours. Nowadays, I'm annoyed when
someone uses double quotes:(


Annoyed? Why?
Jul 17 '05 #11

P: n/a
That you need a benchmark to tell the difference means it doesn't
matter.

Jul 17 '05 #12

P: n/a
nice logic
do you strive for mediocracy in everything you do?

Jul 17 '05 #13

P: n/a
BKDotCom wrote:
nice logic
do you strive for mediocracy in everything you do?


And more importantly do you ever managed to reach it! ;-)
--
Half the people you know are below average.

Jul 17 '05 #14

P: n/a
On Fri, 15 Apr 2005 07:35:03 +0100, Geoff Berrow <bl******@ckdog.co.uk> wrote:
I noticed that Message-ID:
<11**********************@l41g2000cwc.googlegroup s.com> from R. Rajesh
Jeba Anbiah contained the following:
Hmmm... I'd say that it's a definetely less readable than:
$string="My \"nickname\" is $myNickName, what's yours?";

and if you can use single quotes to surround "nickname":
$string="My 'nickname' is $myNickName, what's yours?";

I really like to write readable code. Saves time and hassle.


His version is much better than yours. Nowadays, I'm annoyed when
someone uses double quotes:(


better not look at any of my code then...


Or mine. I like to use sprintf.

$string = sprintf('My "nickname" is %s, what's yours?', $myNickName);

OK, so I maybe wouldn't use it if there's just one argument. But you have a
few arguments and you can do a nice spaced out call; sprintf's the original
templating system ;-)

If you have to HTML encode everything then it saves on temporaries or mangling
the original variables.

$string = sprintf(
'<td>something something %s</td><td>something else %s</td>',
htmlspecialchars($something1),
htmlspecialchars($something2)
);

You could have multiple concatenations. It's a style issue - unless your
script is trivial, worrying about the performance of this is micro-optimisation
- only worth doing when you've optimised the more important bits first.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #15

P: n/a
On Wed, 13 Apr 2005 16:09:08 -0700, ev***************@gmail.com wrote:
Hi there,

I was wondering is there a point in optimization in code style... I
have used to put parenthesis around some php keywords, but they are not
needed.
No, coding style is for clarity, remember the parser only reads through
your code once everytime your code is loaded ... or not at all if using
the Zend optimising thingymebob.

Loading unrequired code, repeated loading of libaries, inefficient
program design and logic all have a far greater impact on system
performance:-

$sql = "SELECT [id], [name] FROM [foo] ORDER BY id";
$result = mysql_query($sql, $conn);
while($row = mysql_fetch_array($result))
{
if($row['id'] == 9)
{
echo "Row 9 exists";
}
}
It doesn't matter whether you use

echo "Row 9 exists";
echo ( "Row 9 exists" );
echo 'Row 9 exists';
printf ( "Row 9 exists" );

the program code still processes too many database records just to find
one row.
For example, I always use:
echo( "String..." );
but I could use
echo "String...";

the same thing is with return:
return( $tempHTML );
as opposed to
return $tempHTML;

Also is there a point to variate between string delimiters " and ' ?
ie. is it better to use single quotes instead of double quotes if I
don't won't any variable in a string? For example:
'This is a string...'
as opposed to
"This is a string...".

Double quotes are needed, of course in other cases:
"Frankly my dear, your name is ${nameOfTheUser}!"
Double quotes are used for indicating text which may have a variable or
code embedded into it, and that the php interpreter should scan the text
as it outputs it for tokens representing variables and then automatically
replace them with the value of those variables.

If you don't need to parse variables use single quotes.
I know it sounds stupid, even hard to read, but what are your opinions?


Putting literals into the body of your program is evil!

define('TXT_YOUR_NAME_IS', 'Frankly my dear, your name is %s!');

..
..
..
..

echo sprintf(TXT_YOUR_NAME_IS , $nameOfTheUser);

This code could be very quickly maintained in French, German, Spanish as
I don't need to change the logic of the program, just the values I declare
at the start of the program.

switch($_COOKIE['lang'])
{
case 'fr':
include('lang/french.php');
break;
case 'gr':
include('lang/german.php');
break;
case 'sp':
include('lang/spanish.php');
break;
default:
include('lang/english.php');
break;
}
Each language has it's own declaration file and only the relivent one will
be loaded. Compare that to

switch($_COOKIE['lang'])
{
case 'fr':
echo "Tu et ${nameOfTheUser}";
break;
case 'gr':
echo "Das namen est ${nameOfTheUser}";
break;
case 'sp':
echo "No speaka da language ${nameOfTheUser}";
break;
default:
echo "Frankly my dear, your name is ${nameOfTheUser}!";
break;
}
Where all four different languages have to be included even when only one
is used. (Appologies to German/French/Spanish readers!)
Jul 17 '05 #16

P: n/a
On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj**********@gmail.com>
wrote:
echo sprintf(
Yay, it's not just me that likes sprintf then :-)
This code could be very quickly maintained in French, German, Spanish as


Although sprintf for localisation has the main problem that word order within
sentences isn't always the same; sprintf doesn't do ordered parameters.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #17

P: n/a
On Fri, 15 Apr 2005 22:52:02 +0100, Andy Hassall wrote:
On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj**********@gmail.com>
wrote:
echo sprintf(


Yay, it's not just me that likes sprintf then :-)


Yup. :) Although htmlspecialchars processing is just one of it's many
talents, the same techniques can easily be applied to SQL statement
building.
This code could be very quickly maintained in French, German, Spanish as


Although sprintf for localisation has the main problem that word order within
sentences isn't always the same; sprintf doesn't do ordered parameters.


It may have been a bad example to give, I was trying to place emphasis on
code/data seperation and efficiency of code maintenance.
Jul 17 '05 #18

P: n/a
On Fri, 15 Apr 2005 22:12:01 +0100, CJ Llewellyn <cj**********@gmail.com>
wrote:
On Fri, 15 Apr 2005 22:52:02 +0100, Andy Hassall wrote:
On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj**********@gmail.com>
wrote:
echo sprintf(


Yay, it's not just me that likes sprintf then :-)


Yup. :) Although htmlspecialchars processing is just one of it's many
talents, the same techniques can easily be applied to SQL statement
building.


Indeed - I am a rabid advocate of the use of placeholders/bind variables in
SQL, to separate SQL from variable values - since I use Oracle at work, it's
got native support (and using them is mandatory unless you want to kill the
database) so sprintf() is the Wrong Way To Do it there - but both when I use
Oracle, and when I use MySQL for playing at home, I use ADOdb to give me a
consistent interface which supports native placeholders on Oracle, and emulated
ones on MySQL.

If you're using the naked MySQL functions then sprintf()+mysql_escape_string()
works very nicely, provided you always remember to use it...
This code could be very quickly maintained in French, German, Spanish as


Although sprintf for localisation has the main problem that word order within
sentences isn't always the same; sprintf doesn't do ordered parameters.


It may have been a bad example to give, I was trying to place emphasis on
code/data seperation and efficiency of code maintenance.


OK :-)

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #19

P: n/a
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:ro********************************@4ax.com...
On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj**********@gmail.com>
wrote:
echo sprintf(
Yay, it's not just me that likes sprintf then :-)
This code could be very quickly maintained in French, German, Spanish as


Although sprintf for localisation has the main problem that word order

within sentences isn't always the same; sprintf doesn't do ordered parameters.


I thought you can reference the variables supplied to sprintf() by number in
the format string. Not something I have tried tough. Of course it doesn't
help much when you have languages with declensions.
Jul 17 '05 #20

P: n/a
James Pittman wrote:
R. Rajesh Jeba Anbiah wrote:
Michal Wozniak wrote:
$string = 'My "nickname" is '.$myNickName.', what\'s yours?';

Hmmm... I'd say that it's a definetely less readable than:
$string="My \"nickname\" is $myNickName, what's yours?";

and if you can use single quotes to surround "nickname":
$string="My 'nickname' is $myNickName, what's yours?";

I really like to write readable code. Saves time and hassle.

His version is much better than yours. Nowadays, I'm annoyed when someone uses double quotes:(


Annoyed? Why?


'coz nowadays, I'm trying to grow up by caring performance and
optimization.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #21

P: n/a
Geoff Berrow wrote:
I noticed that Message-ID:
<11**********************@l41g2000cwc.googlegroups .com> from R. Rajesh Jeba Anbiah contained the following:

<snip>
His version is much better than yours. Nowadays, I'm annoyed whensomeone uses double quotes:(


better not look at any of my code then...


Unfortunately, I've already seen your notes;) LOL! BTW, welcome back
to the php square.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #22

P: n/a
One quick glance of an experienced eye allowed to understand the blurred
and almost unreadable R. Rajesh Jeba Anbiah's handwriting:
James Pittman wrote:
R. Rajesh Jeba Anbiah wrote:
> His version is much better than yours. Nowadays, I'm annoyed when > someone uses double quotes:(


Annoyed? Why?


'coz nowadays, I'm trying to grow up by caring performance and
optimization.


I'd say "d'oh" (note the double quotes). Have you seen the php
benchmarks? Do you ALWAYS use the fastest solution? I don't think so.
And it that case, the "quotes optimization" is just completely
impossible to notice.

Cheers
Mike
Jul 17 '05 #23

P: n/a
Michal Wozniak wrote:
<snip>
'coz nowadays, I'm trying to grow up by caring performance and
optimization.


I'd say "d'oh" (note the double quotes). Have you seen the php
benchmarks? Do you ALWAYS use the fastest solution? I don't think so.
And it that case, the "quotes optimization" is just completely
impossible to notice.


Oh, yes. You're right.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #24

P: n/a
On Fri, 15 Apr 2005 22:25:51 -0400, "Chung Leong" <ch***********@hotmail.com>
wrote:
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:ro********************************@4ax.com.. .
On Fri, 15 Apr 2005 21:40:44 +0100, CJ Llewellyn <cj**********@gmail.com>
wrote:
>echo sprintf(


Yay, it's not just me that likes sprintf then :-)
>This code could be very quickly maintained in French, German, Spanish as


Although sprintf for localisation has the main problem that word order

within
sentences isn't always the same; sprintf doesn't do ordered parameters.


I thought you can reference the variables supplied to sprintf() by number in
the format string. Not something I have tried tough. Of course it doesn't
help much when you have languages with declensions.


Ooh, I didn't know that "As of PHP 4.0.6 the format string supports argument
numbering/swapping.". http://uk2.php.net/sprintf

<?php
$format = "The %2\$s contains %1\$d monkeys.
That's a nice %2\$s full of %1\$d monkeys.";
printf($format, $num, $location);
?>

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #25

P: n/a
I noticed that Message-ID:
<11**********************@l41g2000cwc.googlegroups .com> from R. Rajesh
Jeba Anbiah contained the following:
better not look at any of my code then...
Unfortunately, I've already seen your notes;) LOL!


Uh..thanks :-} My students think I'm wonderful
BTW, welcome back
to the php square.


Haven't been away, just busy coding :-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #26

This discussion thread is closed

Replies have been disabled for this discussion.