<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Dave Dash</title>
 <link href="http://davedash.com/tag/best-practices/atom.xml" rel="self"/>
 <link href="http://davedash.com/tag/best-practices"/>
 <updated>2010-08-29T14:12:50-07:00</updated>
 <id>http://davedash.com/</id>
 <author>
   <name>Dave Dash</name>
   <email>dd+atom1@davedash.com</email>
 </author>

 
 <entry>
   <title>PHP double versus single quotes</title>
   <link href="http://davedash.com/2007/03/03/php-double-versus-single-quotes/"/>
   <updated>2007-03-03T00:00:00-08:00</updated>
   <id>http://davedash.com/2007/03/03/php-double-versus-single-quotes</id>
   <content type="html">&lt;p&gt;[tags]best practices, php[/tags]&lt;/p&gt;

&lt;p&gt;If you're familiar with PHP, the difference between double and single quotes is obvious.  Double quotes evaluate variables and control characters (e.g. &lt;code&gt;\n&lt;/code&gt; or &lt;code&gt;\r&lt;/code&gt;), whereas single quotes do not.&lt;/p&gt;

&lt;p&gt;I've been indoctrinated with the &quot;use single quotes whenever possible&quot; methodology, but I never really put it to the test.  Is it really worth it for me to go back and look at old code that uses double quotes and change them?  Like all best practices, the answer is &quot;maybe.&quot;&lt;/p&gt;

&lt;p&gt;So I wrote a simple test harness (there's a lot of room for error with these, but I tried my best).&lt;/p&gt;

&lt;div&gt;&lt;textarea name=&quot;code&quot; class=&quot;php&quot;&gt;
&lt;?php

define ('MAX',2000000);

function f1()
{
    for($i=0;$i&lt;MAX; $i++)
    {
        $c = &quot;test &quot; . $i;
    }
}


function f2()
{
    for($i=0;$i&lt;MAX; $i++)
    {
        $c = &quot;test $i&quot;;
    }
}


function f3()
{
    for($i=0;$i&lt;MAX; $i++)
    {
        $c = 'test ' . $i;
    }
}



$t1 = microtime(true);
f1();
echo 'Time 1: ' , (microtime(true) - $t1) , &quot;\n&quot;;

$t2 = microtime(true);
f2();
echo 'Time 2: ' , (microtime(true) - $t2) , &quot;\n&quot;;

$t3 = microtime(true);
f3();
echo 'Time 3: ' , (microtime(true) - $t3) , &quot;\n&quot;;



&lt;/textarea&gt;&lt;/div&gt;


&lt;p&gt;I tried different permutations of which order to run &lt;code&gt;f1()&lt;/code&gt;, &lt;code&gt;f2()&lt;/code&gt; and &lt;code&gt;f3()&lt;/code&gt; they seemed to give similar results no matter which order they were run.&lt;/p&gt;

&lt;p&gt;My results were:&lt;/p&gt;

&lt;pre&gt;
Time 1: 2.898087978363
Time 2: 3.5480048656464
Time 3: 2.8503499031067
&lt;/pre&gt;


&lt;p&gt;My interpretation is that quotes versus double quotes isn't as big of a deal as concatenation versus putting variables within double quotes.  My guess is that PHP 5.2.0 (which is what I used for the tests) is smarter with strings than older versions.&lt;/p&gt;

&lt;p&gt;So really if you look at the test harness there isn't any discernible differences until you hit 2 million iterations and even then nobody gets fired over 0.6 seconds of performance.  Chances are it doesn't matter too much, but over time you can write enough code in the right spots or shared in the right open source projects and that few seconds will snowball.  After all, I'm not so much concerned about how optimized a script is that I write, but rather how optimized is a script that everyone ends up using.  But rather than do mental calculations about whether or not to optimize something, let's just assume that everything should be as optimal as we can stand to write it.&lt;/p&gt;

&lt;p&gt;Update: 6 March 2006, I updated the test harness to reflect my intended tests.
Update: 7 March 2006, I updated the results to be more clear.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Parsing a list of Key:Value pairs</title>
   <link href="http://davedash.com/2007/02/24/parsing-a-list-of-kv-pairs/"/>
   <updated>2007-02-24T00:00:00-08:00</updated>
   <id>http://davedash.com/2007/02/24/parsing-a-list-of-kv-pairs</id>
   <content type="html">&lt;p&gt;[tags]best practices,php,openID[/tags]
I'm working on implementing &lt;a href=&quot;http://openid.net/&quot;&gt;openID&lt;/a&gt; for &lt;a href=&quot;http://reviewsby.us/&quot;&gt;reviewsby.us&lt;/a&gt; and for use in &lt;a href=&quot;http://www.symfony-project.com/&quot;&gt;symfony&lt;/a&gt; apps.  One thing I was having trouble with was parsing key value pairs, which is one of the requirements to reading responses.  It's a fairly easy task, but &lt;a href=&quot;http://php.net/&quot;&gt;PHP&lt;/a&gt; offers so many ways to do this.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://openid.net/&quot;&gt;openID&lt;/a&gt; calls for the following &lt;a href=&quot;http://openid.net/specs/openid-authentication-1_1.html#anchor32&quot;&gt;Key-Value format&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Lines of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; some_key:some value&lt;/li&gt;
&lt;li&gt; There MUST NOT be a space before or after the colon.&lt;/li&gt;
&lt;li&gt; Newline characters MUST be Unix-style, just ASCII character 10 (&quot;\n&quot;).&lt;/li&gt;
&lt;li&gt; Newlines MUST BE at end of each line as well as between lines.&lt;/li&gt;
&lt;li&gt; MIME type is unspecified, but text/plain is RECOMMENDED.&lt;/li&gt;
&lt;li&gt; Character encoding MUST BE UTF-8.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;So here is my attempt at parsing something like this as efficiently and error free as possible:
&lt;!--more--&gt;&lt;/p&gt;

&lt;div&gt;&lt;textarea name=&quot;code&quot; class=&quot;php&quot;&gt;
function splitKV($response) 
{
    $r = array();
    preg_match_all('|^\s*([^:]+):([^:\n]+)[ ]*$|m', $kvs, $matches);
    for($i = 0; $i &lt; count($matches[0]); $i++) {
        $r[$matches[1][$i]] = $matches[2][$i];
    }
    return $r;
}
&lt;/textarea&gt;&lt;/div&gt;


&lt;p&gt;I wrote this function as I was writing the post... and it came out way faster than my previous implementations using &lt;code&gt;strtok&lt;/code&gt; or a combination of &lt;code&gt;explode&lt;/code&gt; and &lt;code&gt;trim&lt;/code&gt;.  Which is good to hear since I do use &lt;code&gt;preg_&lt;/code&gt; functions quite a bit in PHP and they definitely have their place.&lt;/p&gt;

&lt;p&gt;I'm curious if people have found a faster way of parsing through a string of Key-Value pairs.  I'll run it in a test harness and stand corrected if someone comes through with something faster ;).&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Making anchor links smarter... and sexier</title>
   <link href="http://davedash.com/2007/02/14/making-anchor-links-smarter-and-sexier/"/>
   <updated>2007-02-14T00:00:00-08:00</updated>
   <id>http://davedash.com/2007/02/14/making-anchor-links-smarter-and-sexier</id>
   <content type="html">&lt;p&gt;So I have a small bone to pick with Jacob Nielsen and his &lt;a href=&quot;http://www.useit.com/alertbox/within_page_links.html&quot;&gt;opinion on within-page links&lt;/a&gt; or anchor links&lt;sup id=&quot;fnr1&quot;&gt;.&lt;a href=&quot;#fn1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;  There clearly is a benefit to not just linking to a specific page, but linking to a specific part of a page.&lt;/p&gt;

&lt;p&gt;With a little help from &lt;a href=&quot;http://script.aculo.us/&quot;&gt;script.aculo.us&lt;/a&gt; we can spice up our anchor links by highlighting them as well as linking to them.&lt;/p&gt;

&lt;p&gt;For this article we'll limit our scope to internal anchors only.&lt;sup&gt;&lt;a href=&quot;#fn2&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;  We'll write the code using the symfony framework and in straight up &lt;acronym title=&quot;eXtended HypterText Markup Language&quot;&gt;XHTML&lt;/acronym&gt;.  This is really dirt simple and is more of a design pattern with an example than a tutorial.&lt;/p&gt;

&lt;p&gt;Let's do the &lt;acronym title=&quot;eXtended HypterText Markup Language&quot;&gt;XHTML&lt;/acronym&gt; first:&lt;/p&gt;

&lt;div&gt;
    
    &lt;textarea name=&quot;code&quot; class=&quot;xhtml&quot; rows=&quot;10&quot; cols=&quot;50&quot;&gt;
    &amp;lt;a href=&amp;quot;#test&amp;quot; onclick=&amp;quot;new Effect.Highlight(&amp;apos;test&amp;apos;)&amp;quot;&amp;gt;this is a test&amp;lt;/a&amp;gt;
&lt;/textarea&gt;

&lt;/div&gt;


&lt;p&gt;Yup, that's it... I told you it was dirt simple.  You just need to include the proper &lt;a href=&quot;http://prototypejs.org/&quot;&gt;prototype&lt;/a&gt; and &lt;a href=&quot;http://script.aculo.us/&quot;&gt;script.aculo.us&lt;/a&gt; libraries.&lt;/p&gt;

&lt;p&gt;In &lt;a href=&quot;http://symfony-project.com/&quot;&gt;symfony&lt;/a&gt; we avoid repeating ourselves with a helper function:&lt;/p&gt;

&lt;div&gt;
    
&lt;textarea name=&quot;code&quot; class=&quot;php&quot; rows=&quot;10&quot; cols=&quot;50&quot;&gt;
    function link_to_anchor($text, $target)
    {
        return link_to($text, '#'.$target, 'onclick='.visual_effect('highlight',$target);
    }
&lt;/textarea&gt;

&lt;/div&gt;


&lt;p&gt;and call it by doing:&lt;/p&gt;

&lt;div&gt;
    
    &lt;textarea name=&quot;code&quot; class=&quot;php&quot; rows=&quot;10&quot; cols=&quot;50&quot;&gt;
    echo link_to_anchor('this is a test', 'test');
    &lt;/textarea&gt;

&lt;/div&gt;


&lt;p&gt;That's it.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
    &lt;hr/&gt;
    &lt;ol&gt;
        &lt;li id=&quot;fn1&quot;&gt;Jacob Nielsen is an easy target. &lt;a href=&quot;#fnr1&quot; class=&quot;footnoteBackLink&quot;  title=&quot;Jump back to footnote 1 in the text.&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/li&gt;
        
        &lt;li id=&quot;fn2&quot;&gt;Anchors on other pages are equally useful.  To implement that, you need to have an event listener to examine the URL for an anchor and appropriately highlight the correct element. &lt;a href=&quot;#fnr2&quot; class=&quot;footnoteBackLink&quot;  title=&quot;Jump back to footnote 2 in the text.&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>Not taking frameworks for granted</title>
   <link href="http://davedash.com/2006/11/21/not-taking-frameworks-for-granted/"/>
   <updated>2006-11-21T00:00:00-08:00</updated>
   <id>http://davedash.com/2006/11/21/not-taking-frameworks-for-granted</id>
   <content type="html">&lt;p&gt;One of my clients approached me with a relatively easy project.  She gave me a log file of PHP errors and I was supposed to fix her scripts.  I fixed about 100+ different errors in a few hours.  It was fairly straightforward.&lt;/p&gt;

&lt;p&gt;Throughout the site I could understand the previous developer and the choices he or she made for better or worse.  It did look like a struggle however.&lt;/p&gt;

&lt;p&gt;&lt;!--more--&gt;&lt;/p&gt;

&lt;p&gt;Ultimately it felt that there were some fairly simple things that each script needed to do, but each task was a challenge.  Validating forms, storing data across pages, decorating the site, interacting with the database.  Everything seemed very kludged together.&lt;/p&gt;

&lt;p&gt;I realized that this is exactly how I used to write code, sure... my way of course was better and more logical, yada, yada, yada... but ultimately I was there before.&lt;/p&gt;

&lt;p&gt;My client noted that these scripts were made from another contract programmer, and then a light-bulb went on... frameworks (whether it be &lt;a href=&quot;http://symfony-project.com/&quot;&gt;symfony&lt;/a&gt;, &lt;acronym title=&quot;Ruby On Rails&quot;&gt;ROR&lt;/acronym&gt;, Django, CakePHP, etc) help iron out and standardize these tasks.&lt;/p&gt;

&lt;p&gt;Since I know &lt;a href=&quot;http://symfony-project.com/&quot;&gt;symfony&lt;/a&gt; best, I'll cover what I think could have helped in this last project.  I'm sure other major frameworks have their equivalents.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Form validation&lt;/strong&gt;: &lt;a href=&quot;http://symfony-project.com/&quot;&gt;symfony&lt;/a&gt; lets you define form validation in a very simple manner.  The validation logic is also separate from the rest of the code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Storing Data&lt;/strong&gt;: Without a framework, you generally have to rely on the &lt;code&gt;$_SESSION&lt;/code&gt; array in PHP.  While very useful and easy to use, storing parameters and attributes to a user object is done a lot more cleanly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;decorating the site&lt;/strong&gt;: My biggest problem was with each page I had,, I used to have to call headers, sidebars, etc, etc.  &lt;a href=&quot;http://symfony-project.com/&quot;&gt;symfony&lt;/a&gt;'s layout system was a boon.  I had a common layout for all pages (maybe a few alternates) and hooks inside them if they needed to be adjusted.  Then the various actions had their own seperate templates that were injected into the common layout.  It made adding new pages easy, since I didn't need to remember &lt;code&gt;header()&lt;/code&gt; and &lt;code&gt;footer()&lt;/code&gt; functions for each and every page.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interacting with the database&lt;/strong&gt;: &lt;a href=&quot;http://spindrop.us/2006/08/07/how-object-relational-mapping-saves-time-and-makes-your-code-sexy/&quot;&gt;I've covered before the benefits of ORM&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Not only do the bulk of these problems disappear with a framework, a lot of the difficulties of switching developers melt away.  If you tell me, a developer, that I'm walking into a project made with a framework, I can learn about the framework and be able to understand its ins and outs.&lt;/p&gt;

&lt;p&gt;If you just tell me it's written in PHP, chances are I'm going to want to do things my own way.  It's hard to understand the logic that another programmer was using so we fall back to standards whether they are your own or borrowed.&lt;/p&gt;

&lt;p&gt;When we use a framework, we can find some mutually agreed upon standards and usually people who specialize in that framework and are willing to help.  So my advice: stick to frameworks.  The coding style will be no worse than the whims of a programmer, but at best it'll be something that anyone can pick up.  The general case is that even a bad coder can only do so much damage within a framework.  The bullet points I covered above will cut down on development time tremendously.&lt;/p&gt;
</content>
 </entry>
 

</feed>
