Optimization in coding style

This is a discussion on Optimization in coding style within the PHP Language forums, part of the PHP Programming Forums category; Hi there, I was wondering is there a point in optimization in code style... I have used to put parenthesis ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-14-2005
evanescent.lurker@gmail.com
 
Posts: n/a
Default Optimization in coding style

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 -

Reply With Quote
  #2 (permalink)  
Old 04-14-2005
Geoff Muldoon
 
Posts: n/a
Default Re: Optimization in coding style

evanescent.lurker@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
Reply With Quote
  #3 (permalink)  
Old 04-14-2005
Micha³ Wo¼niak
 
Posts: n/a
Default Re: Optimization in coding style

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
Reply With Quote
  #4 (permalink)  
Old 04-14-2005
R. Rajesh Jeba Anbiah
 
Posts: n/a
Default Re: Optimization in coding style

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/

Reply With Quote
  #5 (permalink)  
Old 04-14-2005
Micha³ Wo¼niak
 
Posts: n/a
Default Re: Optimization in coding style

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

Reply With Quote
  #6 (permalink)  
Old 04-15-2005
Chung Leong
 
Posts: n/a
Default Re: Optimization in coding style

<evanescent.lurker@gmail.com> wrote in message
news:1113433748.805122.306810@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.


Reply With Quote
  #7 (permalink)  
Old 04-15-2005
Chung Leong
 
Posts: n/a
Default Re: Optimization in coding style

"Geoff Muldoon" <gmuldoonnospam@scu.edu.au> wrote in message
news:MPG.1cc85e50c86e830a98969f@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.


Reply With Quote
  #8 (permalink)  
Old 04-15-2005
R. Rajesh Jeba Anbiah
 
Posts: n/a
Default Re: Optimization in coding style

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/

Reply With Quote
  #9 (permalink)  
Old 04-15-2005
Geoff Berrow
 
Posts: n/a
Default Re: Optimization in coding style

I noticed that Message-ID:
<1113477617.109936.233590@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/
Reply With Quote
  #10 (permalink)  
Old 04-15-2005
zimba
 
Posts: n/a
Default Re: Optimization in coding style

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

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:02 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0