<![CDATA[ MODx 0.9.7 Coding Standards - My Forums]]> https://forums.modx.com/thread/?thread=32122 <![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174014 rethrash Feb 01, 2008, 08:15 AM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174014 <![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174013 Quote from: OpenGeek at Feb 01, 2008, 03:39 AM

Too many formal rules limits creativity, and IMHO, how one builds a string with variable replacements is an issue that really should be decided by based on the problem being solved.

I think this is a fantastic point. And with the coming changes wrt patch contribution and peer review, we can hope for an ecosystem where the community can more easily hash out performance and readability issues after the initial contribution is made, the important part being the developer inspired by an idea and freely creating and sharing it.
]]>
netProphET Feb 01, 2008, 07:58 AM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174013
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174012 Quote from: BobRay at Feb 01, 2008, 05:58 AM

I certainly can’t justify it logically, but it makes me uneasy to have variable names inside of quotation marks. I imagine it’s my background in other languages that makes me want to use quotation marks strictly for "string literals." smiley
I concur...

Don’t know how much people have looked these "guidelines", but the single quotes for literals and single quote concatenation has been there all along.. but of course those guidelines in wiki have not been very official..]]>
doze Feb 01, 2008, 06:58 AM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174012
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174011 Quote from: sirlancelot at Feb 01, 2008, 06:58 AM

So, if we were to get really granular with the guidelines (oxymoron?), I’d see something like this:
Exactly, it is an oxymoron to get this granular with "Coding Standards". We’re now talking about implementation and optimization details. But some great stuff is being revealed here, regardless of what you call it. Just don’t call it coding standards. wink]]>
opengeek Feb 01, 2008, 01:49 AM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174011
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174010 oxymoron?), I’d see something like this:

  • Pure Strings must use single quotes ([tt]$lang[’static_string’] = ’This string has no variables’;[/tt])
  • Concatenation inside any loop must not use the bracket syntax ([tt]$lang[’another’] = "This string is: {$lang[’static_string’]}";[/tt])
  • Avoid using double quotes when possible
As Ryan, pointed out, speed is a big factor on high traffic sites. I think speed should be a very high priority for MODx and optimizations made anywhere possible. Of course caching kind of relieves much of the processing required, but it only goes so far; also consider scenarios where a website might use many un-cached elements on a single page. Comments should explain code that might be hard to read (but let’s not get into argument about performance of comments wink).]]>
sirlancelot Feb 01, 2008, 12:58 AM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards?page=2#dis-post-174010
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174009 Quote from: OpenGeek at Feb 01, 2008, 03:39 AM


I personally think "like {$this}" is much more readable in most situations . . .

Funny, I was just thinking exactly the opposite. Whenever I see {$that}, my first thought is that it’s a chunk. wink Mentally, I have to do an extra level of processing to "unquote" the variable.

I certainly can’t justify it logically, but it makes me uneasy to have variable names inside of quotation marks. I imagine it’s my background in other languages that makes me want to use quotation marks strictly for "string literals." smiley

Bob
]]>
BobRay Jan 31, 2008, 11:58 PM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174009
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174008
double quotes: 0.000492811203003
single quotes: 0.000663995742798
difference...: -0.000171184539795

<?php

function getmicrotime($t) {  
list($usec, $sec) = explode(" ",$t);  
    return ((float)$usec + (float)$sec);  
}

$var = 'Bob';

# 
$start = microtime(); 
$a = array(); 
for($i=0;$i<100;$i++) { 
    array_push($a, "{$var} rules!"); 
} 
$end = microtime(); 
$t1 = (getmicrotime($end) - getmicrotime($start)); 
echo "<pre>double quotes: $t1<br>"; 
# 
unset($a); 
# 
$start = microtime(); 
$b = array(); 
for($i=0;$i<100;$i++) { 
    array_push($b, $var . ' rules!'); 
} 
$end = microtime(); 
$t2 = (getmicrotime($end) - getmicrotime($start)); 
$t3 = $t1-$t2; 
echo "single quotes: $t2<br>difference...: $t3</pre>";

?>


But all fun aside, my point was that you should test it yourself, in context, and choose the best path based on all the factors, including how many times it’s going to be executed.]]>
opengeek Jan 31, 2008, 11:42 PM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174008
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174007 Point:
If you have tens of thousands of instances on a page, I’d say it might make a difference, but even your benchmark proves that a single instance is only 0.00000723 faster. I’m thinking that just might not be all that noticeable when it’d take almost 14,000 repetitions to reach a whole tenth of a second. I’m guessing there’s much bigger fish to fry when it comes to areas of performance to worry about! Then again, if you’re talking about the core like in the parser, every iota could make a difference on busy sites with millions or more requests daily.

Counter-point:
2 million daily monthly page views * average of 25 occurrences per page (? completely grabbed out of the air ?) = 50,000,000 occurrences/daily or about 6 minutes of processing overhead in a given day month ...

6 min/day * 365 days / 60 min/hour / 24 hours/day ... it really adds up

You could save over 1.5 days per year just from using single quote concatenation on a busy site.


update: then when you realize it’s more conceivable to think about 2 million page views a month (instead of days) that’s 6-minutes a month ... you get back about 1.5 hours, not days, in a year.]]>
rethrash Jan 31, 2008, 10:28 PM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174007
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174006 Quote from: OpenGeek at Feb 01, 2008, 03:39 AM

Saying that all array indexes should be defined with single-quotes (one of the standards currently included in this list) is another example of this.
Actually, I’ll disagree here....performance tests show that concatenation with single-quotes run twice as fast benchmarked than interpolation with double-quotes. And if we’re going to do so with strings with vars inside, might as well keep it standard and do so with those without.

data

double quotes: 0.001505970954895 
single quotes: 0.00078308582305908 
difference...: 0.00072288513183594

benchmark

function getmicrotime($t) {  
 list($usec, $sec) = explode(" ",$t);  
 return ((float)$usec + (float)$sec);  
}  
# 
$start = microtime(); 
$a = array(); 
for($i=0;$i<100;$i++) { 
 array_push($a, "Aaron rules!"); 
} 
$end = microtime(); 
$t1 = (getmicrotime($end) - getmicrotime($start)); 
echo "<pre>double quotes: $t1<br>"; 
# 
unset($a); 
# 
$start = microtime(); 
$b = array(); 
for($i=0;$i<100;$i++) { 
 array_push($b, 'Aaron rules!'); 
} 
$end = microtime(); 
$t2 = (getmicrotime($end) - getmicrotime($start)); 
$t3 = $t1-$t2; 
echo "single quotes: $t2<br>difference...: $t3</pre>";
]]>
splittingred Jan 31, 2008, 09:49 PM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174006
<![CDATA[Re: MODx 0.9.7 Coding Standards]]> https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174005 Quote from: netProphET at Feb 01, 2008, 12:45 AM

Nice write-up.
I’ve read some comments on string concatenation in the forums - should any thoughts about that be made official?
ie. "like {$this}" or ’like ’ . $this
.. or leave it up to the discretion of the coder?
I say discretion of the coder, or ultimately, consensus of those involved in contributing, on this one.

I don’t mind a few simple guidelines and principles under the guise of "Coding Standards", but I’d like to shy away from any inclusion of general PHP coding issues. Too many formal rules limits creativity, and IMHO, how one builds a string with variable replacements is an issue that really should be decided by based on the problem being solved.

Saying that all array indexes should be defined with single-quotes (one of the standards currently included in this list) is another example of this. There are simply times it’s more important to make code readable vs. optimize it for speed, times when it’s appropriate to use "like {$this} or ’like ’ . $that, etc. It’s a matter of context and I believe we should treat such matters on a case-by-case basis based on measurable evidence. I’m not necessarily arguing against or for this specific issue mind you (though I personally think "like {$this}" is much more readable in most situations), just don’t think we should worry ourselves with defining and maintaining standards for the numerous issues like this one, or the string concatenation one, that we could include.

But discussing these kinds of PHP coding tips, as well as tips specific to working with xPDO/MODx code, here in the forums will be a wonderful way to start hashing out content that can fill core committer and add-on developer guides based on the appropriate consensus in each culture. Working on making it much easier to contribute so this knowledge doesn’t get lost here either...]]>
opengeek Jan 31, 2008, 09:39 PM https://forums.modx.com/thread/32122/modx-0-9-7-coding-standards#dis-post-174005