<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hot Koehls &#187; standards</title>
	<atom:link href="http://frankkoehl.com/tag/standards/feed/" rel="self" type="application/rss+xml" />
	<link>http://frankkoehl.com</link>
	<description>The more you know, the more you don&#039;t know</description>
	<lastBuildDate>Thu, 10 May 2012 18:34:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Building a complex system? Take easy steps.</title>
		<link>http://frankkoehl.com/2010/03/building-complex-system-take-easy-steps/</link>
		<comments>http://frankkoehl.com/2010/03/building-complex-system-take-easy-steps/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 20:28:08 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[coding theory]]></category>
		<category><![CDATA[crappy coding]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=1192</guid>
		<description><![CDATA[After launching Fwd:Vault last month, it&#8217;s been a race to add the necessary features and functions to take the service broader. First on the list was more subscription tiers. I launched with just two: free and &#8220;unlimited everything.&#8221; I did this because, well, it was easy. Your instinct may be to dismiss my decision as [...]]]></description>
			<content:encoded><![CDATA[<p>After <a href="/2010/02/celebrate-the-little-victories">launching Fwd:Vault last month</a>, it&#8217;s been a race to add the necessary features and functions to take the service broader. First on the list was more subscription tiers. I launched with just two: free and &#8220;unlimited everything.&#8221; I did this because, well, it was easy.</p>
<p>Your instinct may be to dismiss my decision as laziness, but hear me out. I built most of the base site with just 1 state: free (remember that unlimited free beta period last year?). That allowed me to &mdash; rightly &mdash; focus purely on features, functions, bugs, etc. Dealing with subscription tiers at the same time would have clouded everything, slowing everything down and likely leading to more rewriting. Staying focused allowed me to get the cornerstone stuff right before building on top of it. </p>
<p>I applied the same thought process when it came time to offer paid options. The game plan has always been to have three paid options, plus the free account. However instead of initially coding four possible user states, I started with just two: free or paid.</p>
<p>This makes my job as a developer much more focused. There&#8217;s a LOT of logic in a service like Fwd:Vault focused explicitly on subscriptions: access permissions, showing/hiding upgrade options, setting quota restrictions, security checks to prevent hackarounds from unscrupulous users. The functionality of almost every page is affected by the user&#8217;s free/paying status, and don&#8217;t even get me started on the work it takes to process credit cards. You have to be <del>doubly</del> triply careful when dealing with people&#8217;s personal data like that. On and on. Getting the basics in place takes a lot of forethought and coding.</p>
<p>So instead of thinking about all this stuff in four dimensions &mdash; free, option 1, option 2, option 3 &mdash; I can cover most everything in just two &mdash; free or paid &mdash; and then come back later to fill in the holes for the other tiers.</p>
<p>Complexity is your enemy as a developer. Each task must be as tightly focused as possible. The tighter your focus, the less chance you&#8217;ll have to introduce bugs. Adding more later may require rewrites, but they are far far easier than rewriting the big sloppy mess you get when biting off more than you can chew.</p>
<p>With the basic subscription and tier logic in place, it&#8217;s a far simpler matter to expand the options out to infinity (though we&#8217;ll start with four). Expect to see the new pricing options in a few weeks.</p>
<p>Looking for more to read? There&#8217;s a new post on the Fwd:Vault Blog that details the <a href="http://blog.fwdvault.com/2010/03/always-forgetting-to-defrag-use-your-screensaver/">most unobtrusive disk defragmenting process</a> I&#8217;ve seen (that I also use for my own systems).</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2010/03/building-complex-system-take-easy-steps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.4 released</title>
		<link>http://frankkoehl.com/2010/01/jquery-14-released/</link>
		<comments>http://frankkoehl.com/2010/01/jquery-14-released/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 16:33:14 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[handy functions]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=1118</guid>
		<description><![CDATA[The latest and greatest version of jQuery, version 1.4, was released on January 14, the birthday of jQuery&#8217;s original launch. Bugfixes and improvements abound! The jQuery team has put together a site devoted to the new version, called the 14 days of jQuery, covering the major version changes as well as infrastructure updates coinciding with [...]]]></description>
			<content:encoded><![CDATA[<p>The latest and greatest version of jQuery, version 1.4, was released on January 14, the birthday of jQuery&#8217;s original launch. <a href="http://jquery14.com/day-01/jquery-14">Bugfixes and improvements abound!</a></p>
<p>The jQuery team has put together a site devoted to the new version, called the <a href="http://jquery14.com">14 days of jQuery</a>, covering the major version changes as well as infrastructure updates coinciding with the new release. For example, the documentation site has been completely redesigned, and been moved to it&#8217;s own subdomain home, <a href="http://api.jquery.com">api.jquery.com</a>. Links from the primary jquery.com site should be updated within the next week. With video demos of new features, Q&#038;A&#8217;s with the core team (including founder John Resig), it&#8217;s well-worth checking out for every jQuery developer.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2010/01/jquery-14-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run your servers without timezone offsets</title>
		<link>http://frankkoehl.com/2009/11/run-servers-without-timezone-offsets/</link>
		<comments>http://frankkoehl.com/2009/11/run-servers-without-timezone-offsets/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 16:52:13 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[coding theory]]></category>
		<category><![CDATA[fwdvault]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[tech support]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=904</guid>
		<description><![CDATA[I recently made the decision to store times on Fwd:Vault systems in Greenwich Mean Time, or GMT. I decided to do this because I have time-sensitive events happening along several dimensions. Email coming into the system has several timestamps associated with it: the user&#8217;s initial delivery, relay from their mail server, and receipt by the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently made the decision to store times on <a href="http://fwdvault.com">Fwd:Vault</a> systems in Greenwich Mean Time, or GMT. I decided to do this because I have time-sensitive events happening along several dimensions. Email coming into the system has several timestamps associated with it: the user&#8217;s initial delivery, relay from their mail server, and receipt by the Fwd:Vault mail server. Payment receipts come into Fwd:Vault from our billing provider, which gets stored in my system and made available to the user.</p>
<p>Up until now, my server time was set for the US Eastern, where both I and the server physically reside. Then I started building the code to display local time based on a user&#8217;s selected timezone.</p>
<p>Ugh.</p>
<p>Here&#8217;s the problem: displaying local time requires at least one time conversion, from server time to the user&#8217;s timezone. If the time is initially set to anything other than no-offset GMT, you have two calculations to do, from the server timezone to GMT, then GMT to user timezone. You can do it, of course, but who really wants to write even more code?<!--but programmers are inherently lazy (as well they should be)--></p>
<p>Now add to this equation the fact that most data-delivery systems have settled on sending time data in GMT. A very good practice, to be sure, but presents the need to do another timezone conversion when the data come into your systems. Going back to my example, I had to convert payment times from GMT to US Eastern before dropping them into my database.</p>
<p>Finally, add to the mix the potential for time data coming in from more than one source with more than one offset. Again back to my case, payment data is GMT, as is the <a href="http://frankkoehl.com/2009/08/archive-entire-twitter-timeline">Twitter feed I store and display on the site</a>. Meanwhile, email was set to US Eastern. This matched the server and MySQL database where all the data ends up residing, so I was still looking at just one time conversion. But what happens down the road, when my server configuration changes, or I move to another timezone?</p>
<p>Tying this information to me makes as much sense as tying it to any one of my users. It&#8217;s the same rationale that data service providers use when delivering GMT time data, it applies to me, <strong>and it applies to you too</strong>.</p>
<p>I&#8217;m just too lazy to try and keep all that timezone switching straight in my head. </p>
<p>If you find yourself in the same scenario, save your sanity and your future support efforts. If you run a website that (a) displays time-sensitive data, and (b) allows users to create an account, you really owe it to everyone involved to store time in a neutral fashion and adjust time displays according to the user&#8217;s selected timezone.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2009/11/run-servers-without-timezone-offsets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easily calculate dates and times in different timezones</title>
		<link>http://frankkoehl.com/2008/11/easily-calculate-dates-and-times-in-different-timezones/</link>
		<comments>http://frankkoehl.com/2008/11/easily-calculate-dates-and-times-in-different-timezones/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 22:18:47 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[fwdvault]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=157</guid>
		<description><![CDATA[Building off my last post, where I showed you how to easily display any public Twitter feed on your site, I ran into another problem: the dates that are delivered by the Twitter API all reflect Greenwich Mean Time (GMT). Now I thought about going the old route and doing some convoluted math using date(), [...]]]></description>
			<content:encoded><![CDATA[<p>Building off my last post, where I showed you how to <a href="http://frankkoehl.com/2008/11/display-twitter-updates-on-your-website/">easily display any public Twitter feed on your site</a>, I ran into another problem: the dates that are delivered by the Twitter API all reflect Greenwich Mean Time (GMT). Now I thought about going the old route and doing some convoluted math using <code><a href="http://us3.php.net/manual/en/function.date.php">date()</a></code>, <code><a href="http://us3.php.net/manual/en/function.strtotime.php">strtotime()</a></code>, and <code><a href="http://us3.php.net/manual/en/function.mktime.php">mktime()</a></code>, but I thought it was time find a serious solution, one that would allow me to display the correct time for whatever timezone I wished.</p>
<p>So while searching the web, I came across an insightful post that discussed PHP&#8217;s built-in <a href="http://us3.php.net/manual/en/function.date-create.php">DateTime object</a>. I try hard to keep up with all the tech in my job, but this one got through the cracks, and I was instantly intrigued. So after some research and fiddling, I came up with the following block of code, which will allow you to <strong>easily translate dates/times to and from any timezone</strong>.</p>
<pre lang="php">date_default_timezone_set('GMT');
$datetime = new DateTime('2008-11-25 16:48:13');
$tz = new DateTimeZone('America/New_York');
$datetime->setTimezone($tz);
$time_display = $datetime->format('D, M jS g:ia T');</pre>
<p>The process is a little convoluted, so here&#8217;s what&#8217;s going on. First we must set the system-wide timezone to match that of the date/time we would like to translate. In this example, it&#8217;s about 4:50pm on Nov 25, 2008 in the GMT timezone. Next we create a DateTime object with that time (FYI you can use any format accepted by <a href="http://us3.php.net/manual/en/function.strtotime.php">strtotime()</a><code>).</code></p>
<p>Next we create a <code><a href="http://us3.php.net/manual/en/function.timezone-open.php">DateTimeZone</a></code> object. Keep in mind that this step stands completely on its own, and can be performed anytime up to now.</p>
<p>Finally, we pass the <code>DateTimeZone</code> object as the lone argument in a call to the <code><a href="http://us3.php.net/manual/en/function.date-timezone-set.php">setTimezone()</a></code> method. This will change the timezone stored in $datetime from GMT to EST, and automatically update the date/time accordingly, which we output with a call to the <code><a href="http://us3.php.net/manual/en/function.date-format.php">format</a></code> method.</p>
<p>Note that I used object-oriented syntax for this process, but there is a procedural style (i.e. functions) as well. I prefer OOP here because it&#8217;s just easier to read and follow.</p>
<p>Valid arguments for <code>date_default_timezone_set()</code> and <code>DateTimeZone()</code> come from PHP&#8217;s <a href="http://us3.php.net/manual/en/timezones.php">list of supported timezones</a>.</p>
<p>So now that we can translate timezones, let&#8217;s apply it to <a href="http://frankkoehl.com/2008/11/display-twitter-updates-on-your-website/">my Twitter example&#8230;</a></p>
<pre lang="php">
<ul>
<?php
  if ( $twitter_xml = twitter_status('12345678') ) {
    date_default_timezone_set('GMT');
    $tz = new DateTimeZone('America/New_York');
    $i = 0;
    foreach ($twitter_xml->status as $key => $status) {
      $datetime = new DateTime($status->created_at);
      $datetime->setTimezone($tz);
      $time_display = $datetime->format('D, M jS g:ia T');
?>
<li><?php echo $status->text . '' . $time_display; ?></li>

<?php
      ++$i;
      if ($i == 5) break;
    }
?>
<li><a href="http://twitter.com/YOUR_PROFILE_HERE">more...</a></li>

<?php
  } else {
    echo 'Sorry, Twitter seems to be unavailable at the moment...again...';
  }
?>
</ul>
</pre>
<p>Sidenote: While I was working on this I found GMT and UTC used almost interchangeably. For all intents and purposes, they are identical. But there is a technical difference: GMT is based on the rotation of the earth (less precise), while UTC is based on an atomic clock (more precise). Don&#8217;t worry, you and I are not the only ones who got confused.</p>
<p><strong>Build a slick Twitter feed on your site</strong></p>
<ol>
<li><a href="/2008/11/display-twitter-updates-on-your-website/">Display Twitter updates on your website</a></li>
<li class="green bold">Calculate dates and times in different timezones (translate Twitter timestamps)</li>
<li><a href="/2008/12/parse-urls-in-text-create-links">Parse URL’s in text, create links</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2008/11/easily-calculate-dates-and-times-in-different-timezones/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why include_once and require_once may make you a crappy coder</title>
		<link>http://frankkoehl.com/2008/11/why-include_once-and-require_once-may-make-you-a-crappy-coder/</link>
		<comments>http://frankkoehl.com/2008/11/why-include_once-and-require_once-may-make-you-a-crappy-coder/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 20:25:46 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[coding theory]]></category>
		<category><![CDATA[crappy coding]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[zen cart]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=115</guid>
		<description><![CDATA[Over the last few years, I&#8217;ve noticed that the PHP community has, in general, started to favor include_once() and require_once() over the more standard include() and require(). For the uninitiated, the &#8220;_once&#8221; version of each function will check to see if a file has already been loaded. If it has, it will safely bypass loading [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few years, I&#8217;ve noticed that the PHP community has, in general, started to favor <a href="http://us2.php.net/manual/en/function.include-once.php">include_once()</a> and <a href="http://us2.php.net/manual/en/function.require-once.php">require_once()</a> over the more standard include() and require(). For the uninitiated, the &#8220;_once&#8221; version of each function will check to see if a file has already been loaded. If it has, it will safely bypass loading the file again without throwing an error, and continue parsing you code. For the <strong>really</strong> uninitiated, here&#8217;s the difference between require() and include() <a href="http://us2.php.net/manual/en/function.require.php">straight from the manual</a>:</p>
<blockquote><p>require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don&#8217;t hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well. </p></blockquote>
<p>At first glance, this looks great! Since most files are only ever parsed once&mdash;did you know that it is legal to <a href="http://www.phpf1.com/tutorial/php-include-file.html?page=3">load the same file repeatedly?</a>&mdash;these functions will save you from screwing up your code. No more worrying about reloaded files, repeating actions that cause aberrant behavior or flat out fatal errors.</p>
<p>Obviously the dynamic coding newbie uses these functions as training wheels. &#8220;I may have loaded, but I&#8217;m gonna load it again, just in case.&#8221; That&#8217;s understandable, and all well-and-good. If this is you, don&#8217;t make it a habit, learn to structure your code properly so that files that should be loaded once <strong>only ever have one opportunity to do so</strong>.</p>
<p>At the opposite end of spectrum, there are big-time PHP projects that prefer the _once versions exclusively. My own beloved <a href="http://www.zen-cart.com">Zen Cart</a> has slowly been making the switch (new major revisions on the horizon do it more sweepingly). The <a href="http://manual.cakephp.org/view/509/Coding-Standards">CakePHP Coding Standards</a> actually <strong>demand</strong> the use require_once():</p>
<blockquote><p>When including files with classes or libraries, use only and always the require_once function.</p></blockquote>
<p>Here the rationale is completely different, and completely informed. Because these projects are designed to allow extensive 3rd part manipulation, the chances for a file collision are fairly high. Think about situations where two different mods require the presence of a third &#8220;standard&#8221; mod library. They <a href="http://www.avnetlabs.com/php/php-framework-comparison-benchmarks">trade off a fair amount of performance</a> to offer this flexibility, but at least they know what they&#8217;re doing.</p>
<p>If you&#8217;ve read this far, chances are you are not a newbie, but you sure as hell aren&#8217;t Zen Cart or CakePHP either. So, what business do you have using include_once() or require_once()? None, truth be told. And if you use them extensively, then the title of this article was written for you. Congrats!</p>
<p>The problem that these functions introduce for most developers is two-fold. First, there&#8217;s a <strong>performance penalty</strong> for using these functions, because the function must first check to see if a file has been loaded. Standard include() and require() statements don&#8217;t perform such checks, they simply look for the file and load it. Any type of dynamic system setup is going to use these functions quite a bit, and the penalty is incurred each time the function is used. It only takes about a dozen or so calls to see a difference.</p>
<p>Second and more importantly, it <strong>feeds laziness</strong>. If loading order and structure don&#8217;t cause code to fail, then you&#8217;re naturally tempted not to worry about them. This leads to unnecessary calls&mdash;&#8221;Did I load file yet? Ah screw it, I&#8217;ll do it again to make sure&#8230;&#8221;&mdash;which feels eerily similar to the rationale of the newbie coder I described above.</p>
<p>I&#8217;ve learned that code naturally snowballs in one direction or the other. If you <a href="/2008/04/writing-modular-code-make-it-legible/">write good clean code</a>, optimize where possible (without going overboard), and completely smash bugs, the code you write and your ability to code will only improve. If instead you opt to ignore formatting, write just until &#8220;it works&#8221; and move on, and/or create logic that fixes a bug symptom rather than the bug itself, you will not improve and your code will suck. </p>
<p>That&#8217;s really the great tragedy; you&#8217;ve bypassed an opportunity to potentially improve your code, to potentially improve your own ability. Any shlub can half-ass it; take the high road and do the work. That will put you ahead of said shlubs when it comes time to look for a new job or get a promotion.</p>
<p>I&#8217;m not saying any of that is true for you (is it?), but include_once and require_once definitely fall squarely in the lazy coder category as far as I&#8217;m concerned.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2008/11/why-include_once-and-require_once-may-make-you-a-crappy-coder/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Format a RFC2822 date for mysql datetime fields</title>
		<link>http://frankkoehl.com/2008/09/format-a-rfc2822-date-for-mysql-datetime-fields/</link>
		<comments>http://frankkoehl.com/2008/09/format-a-rfc2822-date-for-mysql-datetime-fields/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 14:50:42 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=96</guid>
		<description><![CDATA[I was crash-coursing myself on PHP&#8217;s IMAP functionality recently, one of the first questions I came across was how I might store the date from an e-mail header in a MySQL DATETIME field. I was afraid I was going to have to parse out the string using a bunch of calls to substr(), but then [...]]]></description>
			<content:encoded><![CDATA[<p>I was crash-coursing myself on <a href="http://www.php.net/imap">PHP&#8217;s IMAP functionality</a> recently, one of the first questions I came across was how I might store the date from an e-mail header in a MySQL DATETIME field. I was afraid I was going to have to parse out the string using a bunch of calls to substr(), but then I remembered that the strtotime() function recognizes various RFC formats.</p>
<p>So, if you need to get the timestamp of an RFC2822 e-mail header&mdash;or most any RFC-based timestamp&mdash;into MySQL DATETIME format, it&#8217;s easy:</p>
<pre class="brush: php; title: ; notranslate">$timestamp = strtotime('Tue, 30 Sep 2008 10:30:00 EDT');
echo date('Y-m-d g:i:s a', $timestamp);</pre>
<p>Keep in mind that if there are differences between the timezones of your server and the timestamp, the date() function could screw with your desired output. You should probably take a look at <a href="http://www.php.net/manual/en/function.date-default-timezone-set.php">date_default_timezone_set()</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2008/09/format-a-rfc2822-date-for-mysql-datetime-fields/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing Modular Code &#8211; Make it Legible!</title>
		<link>http://frankkoehl.com/2008/04/writing-modular-code-make-it-legible/</link>
		<comments>http://frankkoehl.com/2008/04/writing-modular-code-make-it-legible/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 02:28:38 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=11</guid>
		<description><![CDATA[In the first post of my modular code series, we talked about overrides and how they are an important, and often overlooked, feature to consider in any code project. Today we will discuss an equally even more important concept: Ensure that others can quickly and easily read and understand your code. What? Seriously? Yes. Writing [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://frankkoehl.com/2008/04/05/writing-modular-code-overrides/">first post</a> of my modular code series, we talked about overrides and how they are an important, and often overlooked, feature to consider in any code project. Today we will discuss an <span style="text-decoration: line-through;">equally</span> even more important concept: Ensure that others can quickly and easily read and understand your code.</p>
<p><em>What?  Seriously?</em></p>
<p>Yes. Writing clean code is much like any other type of writing exercise, but with slightly different rules. Periods go at the end of a sentence, while semicolons go at the end of a line of code. You get the idea.</p>
<p>We don&#8217;t have to browse far in order to find that a lot of projects place a lot of importance on good syntax: <a href="http://source.mambo-foundation.org/content/category/2/23/69/">Mambo</a>, <a href="http://drupal.org/coding-standards">Drupal</a>, <a href="http://pear.php.net/manual/en/standards.php">PEAR</a>, <a href="http://area51.phpbb.com/docs/coding-guidelines.html">phpBB</a> (a lot of projects, including <a href="http://www.zen-cart.com">Zen Cart</a>, are based on this one), <a href="http://www.possibility.com/Cpp/CppCodingStandard.html">C++</a> (the big daddy), plus <a href="http://www.google.com/search?q=coding+standards+conventions">countless others</a>.</p>
<p>The commonality between all writing formats is that you need to maintain <strong>discipline and consistency</strong> in your vocabulary. The establishment of consistent writing behavior allows your audience to focus on comprehending your <strong>message</strong>, and not waste time and brain power on first deciphering the <strong>words</strong> of that message. Again, the same rules apply to all readers, whether they be blog readers (hi there!) or fellow developers.</p>
<p>Even if you&#8217;re one of those coders who does his work from a dark basement, code by its very nature is <strong>supposed to be a shared</strong>. Unless you code only for yourself, your code <strong>will</strong> be viewed by other people at some point. Your work takes on an instant professional look when it&#8217;s well-formatted. Professional look means professional jobs, means professional-level fees, means 2009 Mustang GT (I&#8217;m working on that last one).</p>
<p>The only way you gain that ease of reading in your code is to practice and stay militant about sticking to your accepted conventions. The exact conventions you adopt are not as important as acting to own them in your coding.</p>
<p>The idea is much like the chores that Mr. Miyagi gave to Daniel LaRusso in <em>Karate Kid</em> (&#8220;wax on, wax off,&#8221; remember?). Miyagi made Daniel do the chores because they helped him develop specific <a href="http://en.wikipedia.org/wiki/Muscle_memory">muscle memories</a>, which he applied directly to his technique training. Stick to your guns, and even when you adjust conventions, your code will still read nicely.</p>
<p>Want some good starting points? Listen up, Daniel-san.</p>
<p><strong>Indent using spaces, not tabs.<br />
</strong>I see tab indentation a lot in both young projects, and older, established ones, and there are several problems that come with them. Tabs display differently at different screen resolutions, on different displays, on different systems, in different programs. It may look alright on your screen, and horrendous on another.</p>
<p>In general, a tab pushes a ton of whitespace in front of the start of your code; in the worst cases there&#8217;s more whitespace on the line than actual code! Your uber 1900&#215;1200 screen running Eclipse with tab stylizing enabled has no problem with 10 levels nested for loops and switches, but take pity on the 1024&#215;768 guy, especially since he&#8217;s still holding the <a href="http://www.thecounter.com/stats/2008/March/res.php">lion&#8217;s share of screen res</a> on the web.</p>
<p>I use <strong>two spaces per level</strong> in my code. This is plenty using a fixed width font for display, though some projects do prefer four.</p>
<p><strong>Keep nested logic in line with code at the same level.<br />
</strong>All that talk about spaces over tabs means nothing if you don&#8217;t actually do it. Any code editor worth its salt will have <strong>auto-indent</strong>, which pushes the cursor to match the indentation of the previous line when a newline is created. Start there, and make sure you keep track of your nesting levels (e.g. for loop, inside an if statement, inside a switch).</p>
<p><strong>Place a header comment block at the very top of every code file.<br />
</strong>I&#8217;m talking about those blocks of code you find at the top of most files in an established project. Mambo has a <a href="http://source.mambo-foundation.org/content/view/115/77/">very good layout description</a>. These are important for one reason: they let people know that <strong>you made this</strong>.</p>
<p>You should put them in every file because never know which file a person will open first. A newbie will start high, naturally, somewhere like the index. But if they&#8217;re debugging, chances are they&#8217;ll go right to the class or function that&#8217;s throwing the error.</p>
<p>Plus, you never where your code may end up. If someone borrows your code, most are nice enough to give you the credit. But even if they grab the whole file, someone else may eventually wander into it. And you always hope the person who does see your stuff appreciates work, and is willing/able to <strong>pay</strong> for something custom. I want that person to know how to reach me.</p>
<p>Bottom line: there&#8217;s nothing wrong doing everything you can to get credit, however small, for your efforts.</p>
<p><strong>Avoid inline comments ( // or # ) in favor of comment blocks ( /*  */ ).<br />
</strong>It just looks better, reads easier, and is way easier to use when you have more than one line of comments at a time. I find it useful in isolated cases to make short inline comments, however most of the time I use full-on blocks, with the opening /* on one line, the closing */ on another, and the comments in between.</p>
<p>Starting out go for comment blocks. It sets a good precedent and will avoid the bad habit of  using single line syntax for multi-line commenting. You also must use this syntax if you intend to use a documentor with your project, such as <a href="http://phpdoc.org/">phpDocumentor</a>.<strong></strong></p>
<p><strong>Use camel case or underscores, but not both.<br />
</strong>This one deals with how you assign multi-word variables and elements.  With camel case you capitalize the first letter of a new word, usually with the first word lowercase (e.g. &#8220;$myVariable&#8221;). This is the standard adopted by JavaScript. Otherwise you can break apart words with an underscore character (e.g. &#8220;$my_variable&#8221;). The choice is completely personal, neither is better than the other. Camel case saves an extra character between each word, making your total variable shorter, while underscores are typically easier to read.<strong><br />
</strong></p>
<p>Again the case can be made for both in different situations; some developers use underscores in the database tables / fields, and camel case in variables. However establishing standards is hard enough without using different ones in different circumstances. Pick one and go.<strong></strong></p>
<p><strong>Stay standard with spacing and newlines around parentheses ( ), brackets [ ], and braces { }.<br />
</strong>This is the biggie, which is why I saved it for last. What you decide to do with the wrapper symbols will have the most impact on your code&#8217;s overall legibility. Parentheses go around evaluation statements (e.g. &#8220;( (1 + 2) * (9 / 3) )&#8221;), function calls (e.g. &#8220;myFunction($argument)&#8221;), and control structures (e.g. &#8220;if ((condition1) || (condition2)) &#8220;). Brackets get used most often in arrays (e.g. &#8220;$myArray[$key1][$key2] = $value;&#8221;). Braces are the walls of your logic nesting, wrapping classes, functions, and nearly every control structure.</p>
<p>Personally, I use spaces around parentheses and braces, because they often contain complex logic. Brackets often hold nothing more than a key, so they&#8217;re not as necessary. The opening brace of a class/function/whatever stays on the same line as the starting code (e.g. &#8220;myFunction() {&#8220;), while the closing one will get its own line and match indentation with up with the opening brace (see tabs and nested logic above).</p>
<p>The goal here is to use whitespace to adequately pad your code for easy reading, while not making it overly elongated. I avoid placing the opening brace on its own line because the code just gets crazy long, making for lots of scrolling. <a href="http://area51.phpbb.com/docs/coding-guidelines.html#codelayout">phpBB elected this style</a>, but as their own standards state, &#8220;This one is a bit of a holy war.&#8221; The basics of this rule can be expanded to include all spacing around everything, such as equal signs, commas, and mathematical symbols.</p>
<p>That should be more than enough to get you started. For more ideas, refer to the project links I provided at the start, and just poke around other people&#8217;s code.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2008/04/writing-modular-code-make-it-legible/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

