<?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>select * &#187; coding</title>
	<atom:link href="http://aptoma.com/select.star/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://aptoma.com/select.star</link>
	<description>web-development, and other issues we really, really care about</description>
	<lastBuildDate>Thu, 22 Apr 2010 19:53:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript: Tools, Coding Standard and Guidelines</title>
		<link>http://aptoma.com/select.star/2010/02/08/javascript-tools-coding-standard-and-guidelines/</link>
		<comments>http://aptoma.com/select.star/2010/02/08/javascript-tools-coding-standard-and-guidelines/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 13:54:33 +0000</pubDate>
		<dc:creator>Geir Berset</dc:creator>
				<category><![CDATA[Monday School]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[checklist]]></category>
		<category><![CDATA[guideline]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[standard]]></category>

		<guid isPermaLink="false">http://aptoma.com/select.star/?p=1123</guid>
		<description><![CDATA[We&#8217;re currently devoting an hour each Monday to collectively work our way through JavaScript. Our aim is to point to the key areas of interest and improvement, and to dig into each area one by one.
We are maintaining a TOC for our JS-discussions here.
Development Tools

Firebug is the most important development tool for us, without comparison. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re currently devoting an hour each Monday to collectively work our way through JavaScript. Our aim is to point to the key areas of interest and improvement, and to dig into each area one by one.</p>
<p>We are maintaining a <a href="http://aptoma.com/select.star/2010/02/01/enterprise-javascript-coding/">TOC for our JS-discussions here</a>.</p>
<h3>Development Tools</h3>
<p><a href="http://getfirebug.com"><img class="alignnone size-full wp-image-1124" title="Picture 1" src="http://aptoma.com/select.star/wp-content/uploads/2010/02/Picture-1.png" alt="" width="411" height="111" /></a><a href="http://getfirebug.com/"></a></p>
<p><strong><a href="http://getfirebug.com/">Firebug</a> is the most important development tool for us</strong>, without comparison. Everyone is familiar with this time-tested tool.</p>
<p>You will rarely need more than Firebug, although there are some valid alternatives now, in 2010. Chrome, Safari and Opera (Dragonfly) have all internal tools for debugging (and more). None of us have used those tools extensively, so we don&#8217;t know more than what we have heard from third parties, which is mostly positive. If you need to spot errors in IE, <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=2F465BE0-94FD-4569-B3C4-DFFDF19CCD99&amp;displaylang=en">Microsoft Script Debugger</a> is your best hope. IE8 ships with this integrated as default. No-one in our team reported ever making use of the Firebug Light for IE.</p>
<p><strong>When it comes to JavaScript editors</strong>, we find that no text editor seems to differ significantly from the rest. Most editors provide color-formatting, some provide API-reference with autocompletion and some editors like vim and TextMate can be configured for auto-validation through JSLint.</p>
<h3>Coding Standard</h3>
<p>Our coding standard for JavaScript builds on our own internal PHP Coding standard as far as it fits, which in turn builds on the publicly available <a href="http://framework.zend.com/manual/en/coding-standard.coding-style.html">Zend Framework Coding standard</a>.</p>
<p><strong>We have boiled our standard down to this shortlist.</strong></p>
<ul>
<li>Follow <a href="http://framework.zend.com/manual/en/coding-standard.coding-style.html">the Zend  Framework Coding standard</a></li>
<li>Use an underscore in front of private objects. (i.e. var _private = 0;) Private objects and methods can be defined as «trusted» with everything else foreign and error-prone. This can make your core more stable and rigid without having to overdo it with data-testing and integrity validation.</li>
</ul>
<p>It is possible to achieve a private scope in JavaScript, but it comes at a cost in complexity, and we have decided against it. Our decision is based on experience and advice given in &#8220;<a href="http://www.dustindiaz.com/pro-javascript-design-patterns/">Pro Javascript Design Patterns</a>&#8220;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * Rationale and short description of ClassName.
 */</span>
<span style="color: #003366; font-weight: bold;">var</span> objectName <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    myFunction<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>whatToDo<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> whatToDo <span style="color: #339933;">===</span> <span style="color: #3366CC;">'string'</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> whatTodo.<span style="color: #660066;">length</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            objectName._doWhatYouGottaDo<span style="color: #009900;">&#40;</span>whatToDo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    _doWhatYouGottaDo<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>whatToDo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// As this is a private function, I trust the type and data validity of the argument</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>whatToDo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above is a minimal example, which in no way covers all the rules accurately.</p>
<h3>Guidelines<strong> </strong></h3>
<p><strong>Global scope. </strong>Put as little garbage as possible in the global scope. Use well-named objects as namespaces and if needed sub-namespaces. Know the results of using or skipping &#8220;var&#8221; when creating objects.</p>
<p><strong>Use closures,</strong> with care! Closures are necessary in JavaScript, but as not all languages have them, they will to an inexperienced JS-programmer look too much like Magic(TM), which isn&#8217;t too good. Use good judgement when to use them, and  remember good inline commenting to make the magic more transparent.</p>
<p><strong>Don&#8217;t use the &#8220;new&#8221; operator</strong>. When setting up prototype inheritance it has to be used, but there are no other cases where it should be used. In most cases it&#8217;s not only slow, but the alternatives are often easier to understand, require less code and are more powerfull. Because of this, we have decided to conform on code without <em>new</em>. As examples, new Array and new Object should be written as [] and {}.<strong> </strong></p>
<p><strong>Prototype inheritance</strong> contains power for object oriented code. We will return with more  guidelines when our experience grow. We must admit not to have used this elaborately. A serious liability of PrototypeJS (the framework we use<strong>d</strong>) is that it overrides prototype (the language feature) from JavaScript.</p>
<p><strong>Few anonymous functions.</strong> As a rule of thumb, we say: <em>name functions</em>. This has to do with testability and readability of your code. The exception is if you need a  &#8220;one-liner&#8221; to use just once, in which case anonymous functions are OK. You would normally not test these kind of functions.</p>
<p><strong>Lint your code! </strong>Lint is a code quality tool, and it will hurt your feelings. But  that pain is just Douglas Crockford stabbing the parts of you responsible for writing bad JavaScript. You end up stronger in the end.</p>
<h3>Recommended research material</h3>
<p>JavaScript requires effort on your behalf. You need to do the homework if you want to be a powerful and successful JavaScript developer. For starters we recommend these resources:</p>
<ul>
<li>Closures : <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Closures">Working with closures</a> and <a href="http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/">Explaining JavaScript scope and closures.</a></li>
<li><a href="http://video.yahoo.com/watch/630959/2974197">Douglas Crockford: &#8220;JavaScript &#8211; The Good Parts&#8221; (video)</a></li>
<li><a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742">Douglas Crockford: &#8220;JavaScript &#8211; The Good Parts&#8221; (amazon)</a><a href="http://ajaxian.com/archives/javascript-the-good-parts"><br />
</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2010/02/08/javascript-tools-coding-standard-and-guidelines/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Enterprise JavaScript Coding</title>
		<link>http://aptoma.com/select.star/2010/02/01/enterprise-javascript-coding/</link>
		<comments>http://aptoma.com/select.star/2010/02/01/enterprise-javascript-coding/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 14:55:17 +0000</pubDate>
		<dc:creator>Geir Berset</dc:creator>
				<category><![CDATA[Monday School]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://aptoma.com/select.star/?p=1097</guid>
		<description><![CDATA[Not many years ago, people would giggle and think of enterprise and JavaScript as an oxymoron. Not so much anymore. If you want to be a serious actor even in (or maybe especially in) the enterprise software market, you have to take JavaScript seriously. Large parts of your product&#8217;s business logic might find its way [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://aptoma.com/select.star/wp-content/uploads/2010/02/18okt04_ajax_logo_150_rgb.jpg"><img class="alignright size-medium wp-image-1109" style="border: 0px;" title="18okt04_ajax_logo_150_rgb" src="http://aptoma.com/select.star/wp-content/uploads/2010/02/18okt04_ajax_logo_150_rgb-284x300.jpg" alt="" width="284" height="300" /></a>Not many years ago, people would giggle and think of enterprise and JavaScript as an oxymoron. Not so much anymore. If you want to be a serious actor even in (or maybe especially in) the enterprise software market, you have to take JavaScript seriously. Large parts of your product&#8217;s business logic might find its way into your JavaScript scope, not to mention your GUI elements, and you must be prepared when it does.</p>
<p>We have decided to address JavaScript with more care. We&#8217;d like to give it the time and focus it deserves. Not to be mistaken, we are probably in the top 5% of businesses going deep into JavaScript on our enterprise software, but we still feel we can go further &#8212; especially in the fields of quality assurance and scaling.</p>
<p>Lo and behold, we give you;</p>
<h3><strong>Monday School Notes from JavaScript session #1</strong></h3>
<h4>Table of Content</h4>
<p>We have decided to divide the subject of JavaScript into the following sections:</p>
<ul>
<li><a href="http://aptoma.com/select.star/2010/02/08/javascript-tools-coding-standard-and-guidelines/">Development Tools</a></li>
<li><a href="http://aptoma.com/select.star/2010/02/08/javascript-tools-coding-standard-and-guidelines/">Coding standard</a></li>
<li><a href="http://aptoma.com/select.star/2010/02/08/javascript-tools-coding-standard-and-guidelines/">Guidelines</a></li>
<li><a href="http://aptoma.com/select.star/2010/02/22/a-first-look-at-javascript-frameworks/">Frameworks, or the lack thereof</a> (when to use what)</li>
<li>Testing</li>
<li>Debugging</li>
<li>Patterns</li>
<li>Document Object Model (DOM)</li>
<li>Productivity tips</li>
<li>Compatibility issues</li>
<li>Recommended reading/watching</li>
</ul>
<p>We&#8217;ll be covering these topics in the weeks to come, and we well be returning with links on the TOC. If you will join us in this, please follow <a href="http://feeds.feedburner.com/aptoma/selectstar">this blog on RSS</a> or follow <a href="http://twitter.com/aptoma">the company</a> or <a href="http://twitter.com/geirber">the author</a> on twitter. If you digg  it, then <a href="http://digg.com/programming/Notes_on_Continuous_Deployment">digg  it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2010/02/01/enterprise-javascript-coding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL Query Cache</title>
		<link>http://aptoma.com/select.star/2009/10/22/mysql-query-cache/</link>
		<comments>http://aptoma.com/select.star/2009/10/22/mysql-query-cache/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 14:00:09 +0000</pubDate>
		<dc:creator>Lars Hetland</dc:creator>
				<category><![CDATA[Monday School]]></category>
		<category><![CDATA[coding]]></category>

		<guid isPermaLink="false">http://aptoma.com/select.star/?p=809</guid>
		<description><![CDATA[The following article is an in-depth look at Query Cache, mentioned briefly in our post about Caching Strategies.
With Query Cache, the result set from SELECT queries are cached with the query command itself as key. Meaning that if a SELECT query initially executes in ten seconds, consecutive times the _exact_ (including whitespace, case and more. [...]]]></description>
			<content:encoded><![CDATA[<p>The following article is an in-depth look at Query Cache, mentioned briefly in <a href="http://aptoma.com/select.star/2009/10/21/caching-strategiescaching-strategies/">our post about Caching Strategies</a>.</p>
<p>With Query Cache, the result set from SELECT queries are cached with the query command itself as key. Meaning that if a SELECT query initially executes in ten seconds, consecutive times the _exact_ (including whitespace, case and more. The query must be an identical string.) same query is executed, the server gets an hit in Query Cache and returns the cached data in milliseconds, normally giving performance boosts between a few and many orders of magnitude. QC is a global server setting and has three modes; ON, OFF and DEMAND. Setting QC to OFF disables it, with it to ON makes the server look for a cached result for all SELECT queries while setting it to DEMAND will have the server only do a QC lookup if the query has SQL_CACHE after the SELECT command. It&#8217;s recommended to put this MySQL-only extension in a comment to keep with compatibility with SQL standards. (SELECT /*SQL_CACHE*/ * FROM brille) A QC miss will add up to about 20% overhead compared to the same query executed directly without QC lookup. QC is guaranteed to deliver fresh data as all QC on a table is invalidated when a INSERT, DELETE or UPDATE command is executed on it. This means that for a table with frequent writes, QC on reads will probably decrease performance. On tables with a high number of both reads and writes, setting QC to DEMAND and not use it on queries hitting that table might be a good idea. When actively using query cache on tables with dates it’s also important to write reusable and cacheable queries. Any use of NOW() or equivalent non-deterministic functions will render query cache unused even within the same second, so date and time should be calculated outside of MySQL. When doing so, first analyze the need for an exact query. If you can get away with &lt; 1 minute accuracy, don’t add date( ‘Y-m-d H:i:s’ ) but date( ‘Y-m-d H:i:00′ ) so any identical queries within the same minute will get the cached result. Subselects will not use QC. On high performance applications, QC should be kept in mind when designing the database and queries. Below are two examples where performance characteristics could be radically different:</p>
<p>First table structure:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`username`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> collate utf8_danish_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`password`</span> text collate utf8_danish_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`salt`</span> text collate utf8_danish_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`last_time_visited`</span> datetime <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0000-00-00 00:00:00'</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`username`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`username`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8 COLLATE<span style="color: #66cc66;">=</span>utf8_danish_ci;</pre></div></div>

<p>Second table structure:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`username`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> collate utf8_danish_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`password`</span> text collate utf8_danish_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`salt`</span> text collate utf8_danish_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`username`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`username`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8 COLLATE<span style="color: #66cc66;">=</span>utf8_danish_ci;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`user_visited`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`last_time_visited`</span> datetime <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'0000-00-00 00:00:00'</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8 COLLATE<span style="color: #66cc66;">=</span>utf8_danish_ci;</pre></div></div>

<p>If the first table was used on a high traffic forum, every time a user loads a page the QC would be invalidated as last_time_visited had to be updated. By denormalization and having two tables where one is often read and the other is often written to, QC on the first table would be kept until a user changed password/username or a new user is registered which is probably less frequent than pageloads.</p>
<p>But the way the SELECT query is created is also important. Lets say you normaly just need the basic information about a user, but every time you visit the users profile, last_time_visited is needed:</p>
<p>First set of queries:<br />
Normal display of user:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #808080; font-style: italic;">/*SQL_CACHE*/</span> id<span style="color: #66cc66;">,</span> username <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>

<p>Display of users profile:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #808080; font-style: italic;">/*SQL_CACHE*/</span> u<span style="color: #66cc66;">.</span>id<span style="color: #66cc66;">,</span> u<span style="color: #66cc66;">.</span>username<span style="color: #66cc66;">,</span> uv<span style="color: #66cc66;">.</span>last_time_visited <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">AS</span> u <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> user_visited <span style="color: #993333; font-weight: bold;">AS</span> uv <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#40;</span> u<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> uv<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> u<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>

<p>Second set of queries:<br />
Normal display of user:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #808080; font-style: italic;">/*SQL_CACHE*/</span> id<span style="color: #66cc66;">,</span> username <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>

<p>Display of users profile:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #808080; font-style: italic;">/*SQL_CACHE*/</span> id<span style="color: #66cc66;">,</span> username <span style="color: #993333; font-weight: bold;">FROM</span> users <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #993333; font-weight: bold;">SELECT</span> last_time_visited <span style="color: #993333; font-weight: bold;">FROM</span> user_visited <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;</pre></div></div>

<p>In the first set, two different queries are used to fetch the same information, the username. This means the second query won&#8217;t be able to use the QC of the first and more frequent query. Also, the often-read table is joined with the often-written which means INSERT, DELETE and UPDATE queries on _both_ tables will clear cache for this query. In the second set of queries, the same query is used two times, increasing the chance of QC hit. And instead of joining the two tables in one query, a second and separate query is executed on the second table, here without the SQL_CACHE keyword. Two queries increases overhead, but the gain in higher cache hit (The first query can even get a hit in a Memcache-lookup) can easily outweigh this.</p>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2009/10/22/mysql-query-cache/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Why Pair Programming?</title>
		<link>http://aptoma.com/select.star/2009/05/27/why-pair-programming/</link>
		<comments>http://aptoma.com/select.star/2009/05/27/why-pair-programming/#comments</comments>
		<pubDate>Wed, 27 May 2009 04:00:10 +0000</pubDate>
		<dc:creator>Geir Berset</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[pair programming]]></category>
		<category><![CDATA[practice]]></category>

		<guid isPermaLink="false">http://aptoma.com/select.star/?p=742</guid>
		<description><![CDATA[I am a sucker for rationale. I&#8217;ve been struggling with rationale on the Extreme Programming (XP)-practice of pair programming for quite some time. What at first looks like one person writing code, and the other one watching, has admittedly been very counter intuitive to me. Let me share my current thoughts of benefits and disadvantages [...]]]></description>
			<content:encoded><![CDATA[<p>I am a sucker for rationale. I&#8217;ve been struggling with rationale on the Extreme Programming (XP)-practice of pair programming for quite some time. What at first looks like one person writing code, and the other one watching, has admittedly been very counter intuitive to me. Let me share my current thoughts of benefits and disadvantages of so called pairing.</p>
<p><a href="http://aptoma.com/select.star/wp-content/uploads/2009/05/pair-of-feet.jpg"><img class="alignnone size-full wp-image-743" style="border:0" title="pair-of-feet" src="http://aptoma.com/select.star/wp-content/uploads/2009/05/pair-of-feet.jpg" alt="pair-of-feet" width="560" height="318" /></a></p>
<h3>Disadvantages of Pair Programming</h3>
<p><strong>Don&#8217;t four hands produce more code than two?<br />
</strong></p>
<p>If you are a carpenter, you can hit more nails with four hands than with two. If you are a brick-layer you can lay more bricks with four hands than with two. If you are a developer you can write more production code with four hands than with two. Or wait. This is based on the assumption that the bottleneck in software development is a mechanical one ; the speed of which you can type on the keyboard. My 10 years as a developer and 5 years as a program manager tells me otherwise. The bottlenecks in somewhat innovative projects (non-repetitive) rarely or never include typing speed. Bottlenecks <em>do </em>include hesitation (doubting your own solution), introducing bugs and having to go back to fix it, suboptimal design that deserves a refactoring, lack of needed knowledge for the task, loss of focus and so on.</p>
<p>A small disclaimer before I go on : We have not been experimenting with Pair Programming for more than a few months, and my above observations are based on observing teams develop from a managerial point of view and from reflecting upon my own coding habits as a team member.</p>
<h3>Benefits of Pair Programming</h3>
<p><strong>Amplify learning</strong></p>
<p>The speed of learning in pair programming is overwhelming. Once you enter a topic one party has more experience in, you are in for a ride. I have never experienced learning at such a pace as when this occurs.</p>
<p><strong>Prevent bugs</strong></p>
<p>Bugs are cheapest when they are prevented  from being introduced with measures that does not put restraints on your risk-taking and innovation. Pairing keeps you innovating, and will still help you spot errors more frequently. You will be more critical of your own code, and you will have someone constantly looking for possible improvements in your code, such as stopping a bug in the making, or having an idea for an even better test.</p>
<p><strong>Continuous improvement</strong></p>
<p>Pair programming is a dialogue between two people trying to simultaneously program, and understand how to program better.</p>
<p><strong>Articulating ideas</strong></p>
<p>Pair programming forces you to put words to your thoughts. This will through constant practice improve your skills in articulating your ideas.</p>
<p><strong>Share frustrations</strong></p>
<p>The responsibility on one single programmer can be huge at times. It is relieving to carry the load together. A teams strength and robustness is greater than the sum of strengths.</p>
<p><strong>Avoid hesitation</strong></p>
<p>When exhausted for ideas or new angles, it can lead to hesitation to implement what you suspect is a sub-optimal solution. The union of ideas and angles between the two developers makes this a rarer incident, and the resulting discussions will usually help remove the knots causing hesitation.</p>
<p><strong>Distributed resources<br />
</strong></p>
<p>From a managers point of view, a migration of people between project modules, and even between projects, becomes vastly easier. This is a dream come true. Nobody is the sole &#8220;owner&#8221; of code anymore, and developers can move more freely between code sections and projects, simplifying the complex task of resource scheduling. Starting out on unfamiliar code means that you should be pair programming with someone with a lot of domain knowledge in the start.</p>
<h3>How to do pair programming</h3>
<p>Pair programming is two people using one keyboard to write code. Here are some important best-practices to go along with that description.</p>
<ol>
<li>Choose a suitable partner for pair programming the given task during <a href="http://aptoma.com/select.star/2008/08/15/the-sprint-part-22-the-daily-scrum/">the daily scrum.</a></li>
<li>Sit side by side with only one keyboard and one mouse. Both should have a good view of the monitor.</li>
<li>It must be easy to slide the keyboard from person to person. Switch roles from typer to observer every so often.</li>
<li>Discuss and have a good time.</li>
</ol>
<p>So, these are my reflections on the subject so far. <a href="http://feeds.feedburner.com/aptoma/selectstar">I will be sure to update you (RSS)</a> on any changes of opiniton as we continue to improve our development practices.</p>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2009/05/27/why-pair-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mother Of All UTF-8 Checklists</title>
		<link>http://aptoma.com/select.star/2008/12/19/mother-of-all-utf-8-checklists/</link>
		<comments>http://aptoma.com/select.star/2008/12/19/mother-of-all-utf-8-checklists/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 11:29:29 +0000</pubDate>
		<dc:creator>Geir Berset</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[checklist]]></category>
		<category><![CDATA[encoding]]></category>

		<guid isPermaLink="false">http://aptoma.com/select.star/?p=478</guid>
		<description><![CDATA[I believe we have overcome all future encoding challenges. And that&#8217;s the cue-word here; believe. Encoding has a way of sneaking up on you, kicking you in the butt when you least expect it.
We&#8217;d like to share with you what we believe have proven to bring us from encoding hell and into UTF-8-heaven. (Writing this [...]]]></description>
			<content:encoded><![CDATA[<p>I believe we have overcome all future encoding challenges. And that&#8217;s the cue-word here; believe. Encoding has a way of sneaking up on you, kicking you in the butt when you least expect it.</p>
<p>We&#8217;d like to share with you what we believe have proven to bring us from encoding hell and into UTF-8-heaven. (Writing this makes me feel like the first day of an AA-meeting; &#8220;Yes, I have had problems with encoding&#8221;. Please be welcomed to Encoders Anonymous).</p>
<p><img class="size-full" title="signpost-blank" src="http://aptoma.com/select.star/wp-content/uploads/2008/12/utf-8.jpg" alt="" /></p>
<h3>Can all your applications send, receive, process and display this?</h3>
<p>بما في ذلك الكلمات المستخدمة في صفحات التيكت والويكي.</p>
<p>Or this?<br />
繁體中文, 許功蓋會育</p>
<p>Or even this?<br />
Българският език работи ли</p>
<p>Yes? Well, then you don&#8217;t have much of a problem with Iñtërnâtiônàlizætiøn, do you? Having such ingenious programming skills you can now move on to other endeavours. I bid you adieu. The rest of us will pick at this very one in the meantime.</p>
<h3>The Mother Of All UTF-8 Checklists.</h3>
<p>We have come to realize that we should maintain such a list, and you are now being presented with the current state of it. I&#8217;ll present a section for each and every layer of our application technologies (LAMP).</p>
<h3>Operating system</h3>
<p>Use a proper operating system for server purposes. *nix, except-OS X (1) that is.</p>
<h3>Mysql</h3>
<p>Sadly enough, mysql ships with a &#8220;latin1&#8243; default encoding set up. This is a no-go in the &#8220;Defaults Matter&#8221;-department, and more a sad realization to the state of encoding, than a visionary attempt to try to change things for the better. Sharpen up, mysql, don&#8217;t go all Microsoft on us, here.</p>
<p>Check your mysql status and configuration with :</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">mysql&amp;gt<span style="color: #666666; font-style: italic;">; status;</span>
 Server characterset:	latin1 # se my.cnf changes below to fix
 Db     characterset:	utf8   # all well and good
 Client characterset:	latin1 # to fix : SET NAMES utf8
 Conn.  characterset:	latin1 # same as above
&nbsp;
mysql&amp;gt<span style="color: #666666; font-style: italic;">; show variables;</span>
 character_set_client            | utf8
 character_set_connection        | utf8
 character_set_database          | utf8
 character_set_filesystem        | binary
 character_set_results           | utf8
 character_set_server            | utf8
 character_set_system            | utf8</pre></div></div>

<p>There are four levels on which you can determine character set type in mysql, and if able, you should start at the top, the server-level.</p>
<p>Add these options to my.cnf :</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>mysqld<span style="">&#93;</span></span>
<span style="color: #000099;">character-set-server</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> utf8</span>
<span style="color: #000099;">collation-server</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> utf8_generic_ci</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>client<span style="">&#93;</span></span>
<span style="color: #000099;">default-character-set</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> utf8</span></pre></div></div>

<p>If you have no access to change the server config, you can have a go at the lower levels, the next one being the database. If you were unable to change the server config, use these types of statements for creating databases.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> $db CHARACTER <span style="color: #993333; font-weight: bold;">SET</span> utf8 COLLATE utf8_generic_ci;</pre></div></div>

<p>If you already have a database up and running, you have to drop it and recreate it to change to UTF-8. If this is not an option, all is still not lost. You can have a go at lower levels as well.</p>
<p>Table and column-level (use in this order) :</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> $table COLLATE utf8_generic_ci;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> $table <span style="color: #993333; font-weight: bold;">CHANGE</span> <span style="color: #ff0000;">`$field_name`</span> <span style="color: #ff0000;">`$field_name`</span> $field_type
  CHARACTER <span style="color: #993333; font-weight: bold;">SET</span> utf8 COLLATE utf8_generic_ci;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#123;</span>$table<span style="color: #66cc66;">&#125;</span> CONVERT <span style="color: #993333; font-weight: bold;">TO</span> CHARACTER <span style="color: #993333; font-weight: bold;">SET</span> utf8 COLLATE utf8_generic_ci</pre></div></div>

<p>The collation attribute is used for mysql to know how to sort the characters in relation to one another, and not for encoding specifically. A small warning should be issued here, as well. You never know if convert statements, as the one above, will translate everything correctly. Use with caution on production sensitive data.</p>
<p>This is how you&#8217;ll find out what your system is currently running on table level</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> table_name <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLES</span>
   <span style="color: #993333; font-weight: bold;">WHERE</span> TABLE_COLLATION <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AND</span> table_schema <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'$db'</span>
   <span style="color: #993333; font-weight: bold;">AND</span> TABLE_COLLATION <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'utf8%'</span></pre></div></div>

<p>And this is how you&#8217;ll find out on column level</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> COLUMN_NAME<span style="color: #66cc66;">,</span> COLUMN_TYPE <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span>
   <span style="color: #993333; font-weight: bold;">WHERE</span> CHARACTER_SET_NAME <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
   <span style="color: #993333; font-weight: bold;">AND</span> TABLE_SCHEMA <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'{$db}'</span> <span style="color: #993333; font-weight: bold;">AND</span> TABLE_NAME <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'$table_name'</span>
   <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">&#40;</span> CHARACTER_SET_NAME !<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'utf8'</span> <span style="color: #993333; font-weight: bold;">OR</span> COLLATION_NAME <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'utf8%'</span> <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This might seem like a lot of detail just for encoding issues (2), and only on the level of the database. Well, it is. However, being suckers for automation, we have automated these procedures in setup-tests for our in-house framework.</p>
<h3>PHP</h3>
<p>For starters, let&#8217;s not have all our efforts recently made in mysql be in vain. Let&#8217;s speak to mysql in the same international way.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SET NAMES utf8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// When in Rome ..</span></pre></div></div>

<p>And we should be sure it&#8217;s transported the right way from there on out to the browser.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: text/html;charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You&#8217;ll even have to modify php-defaults to get things running smoothly (<a href="http://aptoma.com/select.star/2008/08/13/encoding-getting-those-strange-characters-to-behave/">as we&#8217;ve already discussed</a>). This is done by setting the <a title="PHP.net: Function overloading for mbstring" href="http://www.php.net/manual/en/mbstring.overload.php"><code>mbstring.func_overload</code></a> value to 7.</p>
<p><strong>NB:</strong> You should <strong>not</strong> have to use utf8_decode() and utf8_encode(), if the data is properly encoded as UTF-8 everywhere else. If you find yourself using those functions, you are in hell. Run through every step of this checklist to avoid future complexity. (3)</p>
<h3><strong>HTML</strong></h3>
<p>Insert this in your head-sections to ensure that it is rendered correctly in the user&#8217;s web-browser.</p>
<h3>Apache</h3>
<p>In your .htaccess or apache-config, set these values to ensure that all Javascript and CSS files are served as UTF-8.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">AddCharset UTF-<span style="">8</span> .js
AddCharset UTF-<span style="">8</span> .css</pre></div></div>

<h3>Editor</h3>
<p>You are presumably writing code, and this should also be UTF-8. Ensure that your editor is all set up for saving files that way.</p>
<h3>Conclusion</h3>
<p>Go through the checklist as provided, and you should be all set to conquer the world with your applications. Now, all you do is sit tight and hope that all this adds up to something. God speed.</p>
<p>Please provide feedback if any of our advice does not bring you to where you want to be, or if by any chance your application implodes when doing these measures.</p>
<p>We have a policy of automating and making defaults of all such issues as those mentioned here, and we&#8217;ll surely keep adding more defaults to our framework as challenges emerge.</p>
<p>Have you got any sad or perhaps even inspiring stories on the path to UTF-8-excellence?</p>
<h3>Notes</h3>
<p>For background purposes, it can be nice to know that most of our encoding challenges has existed only in legacy applications. Some of our systems has been developed for Norwegian purposes, and then upgraded for international use. If you start a new project today, and follow the steps above, you should not run into any problems.</p>
<ol>
<li>OS X is excluded because even if you might think that UTF-8 is UTF-8, there are different versions being used. You got UTF-8 NFD form used by OS X and UTF-8 NFC form used by almost everyone else. A file created on OS X named <em>pål</em><em>.txt</em> and transfered over e.g. FTP to a GNU/Linux system won&#8217;t have its name listed the same way there before a run through <em>convmv</em>. <a href="http://unicode.org/reports/tr15/">http://unicode.org/reports/tr15/</a></li>
<li>This is another whacky way of converting your database:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqldump <span style="color: #660033;">-u</span>  <span style="color: #660033;">-p</span>  <span style="color: #000000; font-weight: bold;">&amp;</span>gt; dump.sql
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'s/latin1/utf8/g'</span> dump.sql <span style="color: #000000; font-weight: bold;">&amp;</span>gt; utf8.sql
iconv <span style="color: #660033;">-f</span> iso-<span style="color: #000000;">8859</span>-<span style="color: #000000;">1</span> <span style="color: #660033;">-t</span> UTF-<span style="color: #000000;">8</span><span style="color: #000000; font-weight: bold;">//</span>TRANSLIT utf8.sql <span style="color: #000000; font-weight: bold;">&amp;</span>gt; dump.utf8.sql
create database  default character <span style="color: #000000; font-weight: bold;">set</span> utf8 collate utf8_general_ci;
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">rm</span> utf8.sql dump.sql<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

</li>
<li>If you are receiving non-UTF-8 data from external data-providers which encodes in other encodings than UTF-8, you might find that you&#8217;ll benefit from utf8_encode(), but I&#8217;d recommend that you use it only once, and at once you touch those data.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2008/12/19/mother-of-all-utf-8-checklists/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Encoding &#8211; Getting Those Strange Characters to Behave</title>
		<link>http://aptoma.com/select.star/2008/08/13/encoding-getting-those-strange-characters-to-behave/</link>
		<comments>http://aptoma.com/select.star/2008/08/13/encoding-getting-those-strange-characters-to-behave/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 07:35:39 +0000</pubDate>
		<dc:creator>Michael Plikk</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://aptoma.no/select.star/?p=155</guid>
		<description><![CDATA[The problems surrounding encoding is slowly starting to get the attention of web developers around the world. Previously content was mainly written in English, so we only needed support for the characters used in the English language. As pages in other languages started to appear they needed a way to display their special characters. The [...]]]></description>
			<content:encoded><![CDATA[<p>The problems surrounding encoding is slowly starting to get the attention of web developers around the world. Previously content was mainly written in English, so we only needed support for the characters used in the English language. As pages in other languages started to appear they needed a way to display their special characters. The solutions they found rarely added support for special characters from other languages. This approached worked for a while, but now we&#8217;re seeing content written in one language with comments in every language imaginable. So a method to display all of these different characters at the same time is needed.</p>
<p>Figuring out how to solve this problem in a standardised way posed some problems, so various software vendors went with their own solutions to begin with.</p>
<h3>Laying the Foundations</h3>
<p>Before dwelling deeper into the solutions created it is important to define the terminology that I will use (as it differs between various sources). Some basic knowledge about how encoding works will also be supplied to create the basis for the rest of this blog entry.</p>
<p>A character set, or more specifically, a <strong>coded character set</strong> is a set of character symbols, each of which has a unique numerical ID, which is called the character’s <strong>code point</strong>. The computer representation of the code points is decided by the <strong>character encoding</strong>.</p>
<p>An example of a <em>character set</em> is the 128-character ASCII character set which mainly consist of the letters, numbers and basic punctuation used in the English language. Another common character set is the ISO-8859-1, or Latin 1, character set which extended the ASCII set to also contain extra characters used in various European languages (e.g the accented characters used in French). The most comprehensive character set in use today is the Universal Character Set (UCS) with over 1.1 million <em>code points</em>, as such it contains all characters our current languages need.</p>
<p>Every HTML document uses the UCS &#8211; or more accurately the ISO 10646 character set, which is a less involved standard describing the same set of characters. Older browsers and less powerful devices might not support the complete character set, but that doesn&#8217;t change the fact a HTML<br />
document can contain any character found in the UCS.</p>
<p>However, between each document the <em>character encoding</em> might change. It is the variable character encodings which is the root of all the problems web developers experience in regards to erroneous character display. The problems are often caused by a missing definition of the encoding used on the page (which causes the browser to guess), or usage of different encodings for various parts of the page.</p>
<h3>The Problem</h3>
<p>As the web evolved the standard usage of the ISO-8859-1 encoding started to show some problems. People wanted to use characters not supported.</p>
<p>The first solution to this was to use character entities, or character numerical references. Instead of typing the literal character you would type something like &amp;mdash; (&mdash;) or &amp;#x05d0;(&#x05d0;). The problem with this approach was when submitting data through a form. If the user typed an &#x05d0; the browser would convert it to the numerical reference (&amp;#x05d0;) before submitting the data, since the encoding didn&#8217;t have proper support for the character. But then the application had no way of differentiating between a user who typed the &#x05d0;-character and one who typed the entity code.</p>
<h3>The Solution</h3>
<p>The solution found for this problem was to start using the UTF-8 character set. UTF-8 is an extension of the old ASCII encoding and it has support for all kinds of characters, so the encoding never needs to use entities to display information. The application has full control over the kind of information it receives, and the users has access to all characters they need.</p>
<p>UTF-8 manages to support this wide range of characters by using more than one byte of storage for those characters that require it. Most characters used on an English page will still only use one byte (as ISO does), but when using characters that doesn&#8217;t fit into that small amount it expands by using up to four bytes in total for a single character. There are other UTF encodings available as well. They have a base usage of two (UTF-16) or four bytes(UTF-32), but are not compatible with the ASCII standard so they should not be used unless strictly necessary.</p>
<h3>In Code We Trust</h3>
<p>Creating a UTF-8 application in PHP isn&#8217;t without problems though. Converting an old non-UTF application is even more error prone as PHP has no good and error-free tool for conversion between the various encodings, as I cover at the end of this entry.</p>
<p>The first step in making a web application use UTF-8 is to tell the browser which encoding to expect and thereby how it should encode data submitted through forms. This is done by either using a <code>&lt;meta&gt;</code> tag in the page&#8217;s <code>&lt;head&gt;</code>-section:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;</pre></div></div>

<p>Or by defining the Content-Type in the header:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: text/html;charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The header sent will always override the meta tag, so to be on the safe side you should always send an appropriate header. If you forget it a default header might be sent from Apache which can be different from your desired header. To be on the safe side doing both is advisable, though the <code>&lt;meta&gt;</code> tag will only be used when the content is viewed offline. When the browser knows that content is being sent as UTF-8 it will automatically send content back using the same encoding, so that part doesn&#8217;t require any extra work.</p>
<p>Once that is done the biggest problem when creating a UTF-8 application remains. Most of the string related functions in PHP (e.g <a title="PHP.net: strlen function" href="http://www.php.net/strlen"><code>strlen</code></a>) doesn&#8217;t work any more. Those functions assumes that each character in a string is encoded using ISO-8859 and takes exactly one byte of storage. When characters in a UTF-8 string uses more than one byte the calculations return the wrong result. An example is <code>strlen("Iñtërnâtiônàlizætiøn")</code> which would return 27. From manually counting we see that the result is off by 7. The difference comes from the 7 characters that need two bytes to be stored using UTF-8.</p>
<p>If you&#8217;re aware of this problem to begin with you can avoid many of the problems by coding around the use of these functions. It is possible to do so, but requires extra work and attention from the programmer. Luckily PHP supplies us with a more elegant solution &#8211; <a title="PHP.net: mbstring extension" href="http://www.php.net/mbstring"><em>the mbstring extension</em></a>.</p>
<p>The mbstring extension supplies PHP with multi byte versions of the standard string functions, e.g <a title="PHP.net: mb_strlen function" href="http://www.php.net/mb_strlen"><code>mb_strlen</code></a> which is an extension of the standard <code>strlen</code> function. So prefixing the previously erroneous string functions with mb_ should solve most of those problems encountered.</p>
<p>A more elegant solution exists though, you can override the standard function names to invoke the multi byte versions. This is done by setting the <a title="PHP.net: Function overloading for mbstring" href="http://www.php.net/manual/en/mbstring.overload.php"><code>mbstring.func_overload</code></a> value to 7 in php.ini. The <code>mbstring.func_overload</code> can be set to other values as well if you only want to overload a subset of the string functions.</p>
<p>The encoding must also be set in the text editor and in any databases used. Most modern code editors have this option, so as long as one is aware of the problem it should not be any problem to fix it. Setting the encoding on the database level is usually no problem, but the approach varies between the different systems. </p>
<h3>Legacy Code</h3>
<p>All is well and nice when we&#8217;re creating a new application. We can start storing and handling text as UTF-8 from the very beginning. But what if we have and old application that now needs to support foreign characters?</p>
<p>Unfortunately there is no easy solution for this. If all your previous content is in English it <em>should</em> be safe to simply add the appropriate headers, since the basic characters are the same in all common encodings.</p>
<p>Conversion of characters not commonly used in English is trickier. PHP&#8217;s <a title="PHP.net: iconv library" href="http://www.php.net/iconv">iconv library</a> can be used for most conversions, but there is no guarantee that the converted content is completely correct. On forums and mailing lists you can also find a lot of hand crafted functions for conversion between various encodings &#8211; which might, or might not, work. In the end the only fool proof way to convert content is to rewrite the required sections by hand, or at least double check the changes done by an automated process like iconv.</p>
<p>Further reading, and sources used from this entry:</p>
<ul>
<li><a title="A tutorial on character code issues" href="http://www.cs.tut.fi/~jkorpela/chars.html">A tutorial on character code issues</a></li>
<li><a title="Internationalization (I18N)" href="http://www.phpwact.org/php/i18n">Internationalization (I18N)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2008/08/13/encoding-getting-those-strange-characters-to-behave/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP Sessions &#8211; Mysterious Timeouts Explained</title>
		<link>http://aptoma.com/select.star/2008/08/11/php-sessions-mysterious-timeouts-explained/</link>
		<comments>http://aptoma.com/select.star/2008/08/11/php-sessions-mysterious-timeouts-explained/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 14:07:07 +0000</pubDate>
		<dc:creator>Patrik Henningsson</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://aptoma.no/select.star/?p=212</guid>
		<description><![CDATA[I recently found out something I think worth writing a couple of lines about. Have you ever tried to make a HTTP request from PHP to a second script and preserve the session and ending up with the request hanging? Let me give you an example.
home.php:

session_start&#40;&#41;;
...
$url = &#34;http://url/admin.php?PHPSESSID=&#34;.session_id&#40;&#41;;
$fp = fopen&#40;$url, 'r'&#41;;
$content = fread&#40;$fp&#41;; // will [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found out something I think worth writing a couple of lines about. Have you ever tried to make a HTTP request from PHP to a second script and preserve the session and ending up with the request hanging? Let me give you an example.</p>
<p>home.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://url/admin.php?PHPSESSID=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will hang here!</span>
<span style="color: #339933;">...</span></pre></div></div>

<p>The problem lies in the fact that PHP opens the session file in a exclusive mode, which denies other threads to open the same file. In another words, admin.php will wait for home.php to release the lock on the session file and this will eventually lead to a timeout for the request. But fortunately there is a solution. To solve this you must close the session file before making the request.</p>
<p>home.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span>
<span style="color: #990000;">session_write_close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// close the session file so admin.php can access it</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://url/admin.php?PHPSESSID=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// start the session again</span>
<span style="color: #339933;">...</span></pre></div></div>

<p>I hope this will save you some headache!</p>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2008/08/11/php-sessions-mysterious-timeouts-explained/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Responsive Code in Javascript</title>
		<link>http://aptoma.com/select.star/2008/06/05/writing-responsive-code-in-javascript/</link>
		<comments>http://aptoma.com/select.star/2008/06/05/writing-responsive-code-in-javascript/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 12:59:12 +0000</pubDate>
		<dc:creator>Patrik Henningsson</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://aptoma.no/select.star/?p=4</guid>
		<description><![CDATA[So, in the beginning all the things you&#8217;ve done in Javascript runs quick as the rabbit and your application feels very snappy. But after a while as your code is getting more complex and growing in size, you&#8217;ll probably experience that the application is not as responsive as you wish. Here&#8217;s a technique to help [...]]]></description>
			<content:encoded><![CDATA[<p>So, in the beginning all the things you&#8217;ve done in Javascript runs quick as the rabbit and your application feels very snappy. But after a while as your code is getting more complex and growing in size, you&#8217;ll probably experience that the application is not as responsive as you wish. Here&#8217;s a technique to help you out, which was a bit of an eye opener for me.</p>
<p><strong>Consider the following example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a id=&quot;mylink&quot; href=&quot;#&quot;&gt;Click me, please!&lt;/a&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	updateStatusMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Working..'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	someCpuIntensiveCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	updateStatusMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Done!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mylink'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> myFunction<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The function updateStatusMessage() is suppose to update an element on the page as soon as the user clicks on the link, which will indicate that something is happening. But since your javascript code compete with the UI-thread, the browser probably doesn&#8217;t do the update until the function has finished. The solution is to delay the code that prevents the browser to update using the function setTimeout() and give the browser a chance to refresh the UI before the cpu intensive code executes.</p>
<p><strong>Here&#8217;s the trick:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	updateStatusMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Working..'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		someCpuIntensiveCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		updateStatusMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Done!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You may have to tweak the timeout in order to see any effect.</p>
<p><strong>Now wrap it up so we can reuse the code for other operations too:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> heavyFunction<span style="color: #009900;">&#40;</span>func<span style="color: #339933;">,</span> timeout<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	timeout <span style="color: #339933;">=</span> timeout <span style="color: #339933;">||</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	updateStatusMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Working..'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		func<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		updateStatusMessage<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Done!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> timeout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> myFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	heavyFunction<span style="color: #009900;">&#40;</span>someCpuIntensiveCode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> myOtherFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	heavyFunction<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		moreCPUIntensiveCode<span style="color: #009900;">&#40;</span>arg1<span style="color: #339933;">,</span> arg2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong><a href="http://aptoma.no/select.star/demos/writing-responsive-code-in-javascript.html">Click here for a working example!</a></strong></p>
<p>For more information about this topic, take a look at the excellent screencast <a href="http://video.yahoo.com/watch/1041101">High-performance JavaScript: Why Everything You&#8217;ve Been Taught Is Wrong</a> which gave me some light in the dark.</p>
]]></content:encoded>
			<wfw:commentRss>http://aptoma.com/select.star/2008/06/05/writing-responsive-code-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
