<?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>alexander kirk &#187; code</title>
	<atom:link href="http://alexander.kirk.at/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://alexander.kirk.at</link>
	<description></description>
	<lastBuildDate>Thu, 22 Jul 2010 15:11:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Debugging PHP on Mac OS X</title>
		<link>http://alexander.kirk.at/2008/12/26/debugging-php-on-mac-os-x/</link>
		<comments>http://alexander.kirk.at/2008/12/26/debugging-php-on-mac-os-x/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 18:18:15 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/?p=104</guid>
		<description><![CDATA[I have been using Mac OS X as my primary operating system for a few years now, and only today I have found a very neat way to debug PHP code, like it is common for application code (i.e. stepping through code for debugging purposes). The solution is a combination of Xdebug and MacGDBp. I [...]]]></description>
			<content:encoded><![CDATA[<div class="factolex-glossary"><div class="factolex-glossary-header"><a href="http://en.factolex.com/">Glossary powered by Factolex.com</a></div><div class="factolex-glossary-content"><h2><a href="http://en.factolex.com/Operating_system:system">Operating system</a></h2><ul><li>the software component of a computer system that is responsible for the management and coordination of activities and the sharing of the resources of the computer</li></ul><h2><a href="http://en.factolex.com/OS_X">OS X</a></h2><ul><li>a line of computer operating systems developed, marketed, and sold by Apple Inc, the latest of which is pre-loaded on all currently shipping Macintosh computers</li></ul><h2><a href="http://en.factolex.com/PHP:computer">PHP</a></h2><ul><li>an open source programming language</li></ul></div></div>
<p>I have been using Mac OS X as my primary operating system for a few years now, and only today I have found a very neat way to debug PHP code, like it is common for application code (i.e. stepping through code for debugging purposes).</p>
<p>The solution is a combination of <a href="http://www.xdebug.org/">Xdebug</a> and <a href="http://www.bluestatic.org/software/macgdbp/">MacGDBp</a>.</p>
<p><img src="http://alexander.kirk.at/wp-content/uploads/2008/12/macgdbp-debugger-300x227.png" alt="macgdbp-debugger" title="macgdbp-debugger" width="300" height="227" class="aligncenter size-medium wp-image-105" /></p>
<p>I am using the PHP package by <a href="http://www.entropy.ch/software/macosx/php/">Marc Liyanage</a> almost ever since I have been working on OS X, because it&#039;s far more flexible than the PHP shipped with OS X.</p>
<p>Unfortunately, installing Xdebug the usual <tt>pecl install xdebug</tt> doesn&#039;t work. But on the internetz you can find a <a href="http://www.designified.com/blog/article/60/compiling-installing-xdebug-for-php-525-entropych-build-on-os-x-105">solution to this problem</a>.</p>
<p>Basically you need to download the source tarball and use the magic command <tt>CFLAGS='-arch x86_64' ./configure --enable-xdebug</tt> for configuring it. (The same works for installing APC by the way)</p>
<p><code><br />
/usr/local/php5/php.d $ cat 50-extension-xdebug.ini<br />
[xdebug]<br />
zend_extension=/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so</p>
<p>xdebug.remote_autostart=on<br />
xdebug.remote_enable=on<br />
xdebug.remote_handler=dbgp<br />
xdebug.remote_mode=req<br />
xdebug.remote_host=localhost<br />
xdebug.remote_port=9000<br />
</code></p>
<p>Now you can use MacGDBp. There is an <a href="http://particletree.com/notebook/silence-the-echo-with-macgdbp/">article on Particletree</a> that describes the interface in a little more detail.</p>
<p>I really enjoy using this method to only fire up this external program, when I want to debug some PHP code, and can continue to use my small editor, so that I don&#039;t have to switch to a huge IDE to accomplish the same.</p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2008/12/26/debugging-php-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Website Optimization, a book by Andrew B. King</title>
		<link>http://alexander.kirk.at/2008/08/21/website-optimization-a-book-by-andrew-b-king/</link>
		<comments>http://alexander.kirk.at/2008/08/21/website-optimization-a-book-by-andrew-b-king/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 11:39:18 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[misc]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/?p=90</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<div style="float: right; padding: 5px"<a href=""><img src="http://alexander.kirk.at/img/9780596515089_cat.gif" alt="Website Optimization" border="0"/></a></div>
<p>This time I&#039;m reviewing a book by Andy King. Unlike <a href="http://alexander.kirk.at/2007/09/26/high-performance-web-sites/">High Performance website</a> by Steve Souders, it doesn&#039;t solely focus on the speed side of optimization, but it adds the art of Search Engine Optimization to form a compelling mix in a single book.</p>
<p>If you have a website that underperforms your expectations, this single book can be your one-stop shop to get all the knowledge you need.</p>
<p>Andy uses interesting examples of how he succeeded in improving his clients&#039; pages that illustrate well what he describes in theory before. He not only focuses on how to make your website show up at high ranks in search engines (what he calls &#034;natural SEO&#034;), but also discusses in detail how to use pay per click (PPC) ads to drive even more people to one&#039;s site. I especially liked how Andy describes how to find the best keywords to pick and also describes how to monitor success of PPC.</p>
<p>The part about the optimization for speed feels a little too separated in the book. It is a good read and provides similar content as Steve Souders book, though the level of detail feels a little awkward considering how different the audience for the SEO part of the book is. Still, programmers can easily get deep knowledge about how to get that page load fast. </p>
<p>Unfortunately Andy missed out a little on bringing this all into the grand picture. Why would I want to follow not only SEO but also optimize the speed of the page? There is a chapter meant to &#034;bridge&#034; the topics, but it turns out to be about how to properly do statistics and use the correct metrics. Important, but not enough to really connect the topics (and actually I would have expected this bridging beforehand).</p>
<p>Altogether I would have structured things a little different. For example: It&#039;s the content that makes search engines find the page and makes people return to a page, yet Andy explains how to pick the right keywords for the content first whereas he tells the reader how to create it only afterwards.<br />
Everything is there, I had just hoped for a different organization of things.</p>
<p>All in all, the book really deserves the broad title &#034;Website Optimization.&#034; Other books leave out SEO which usually is the thing that people mean when they want to optimize their websites (or have them optimized).</p>
<p>I really liked that the topics are combined a book and I highly recommend the book for everyone who wants to get his or her website in shape.</p>
<p>The book has been published by O&#039;Reilly in July 2008, ISBN <a href="http://www.oreilly.com/catalog/9780596515089/">9780596515089</a>. Also take a look at the <a href="http://www.websiteoptimization.com/secrets/">Website Optimization Secrets</a> companion site.</p>
<p>Thanks to Andy for providing me a review copy of this book.</p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2008/08/21/website-optimization-a-book-by-andrew-b-king/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Upgrade WordPress Script</title>
		<link>http://alexander.kirk.at/2008/07/15/upgrade-wordpress-script/</link>
		<comments>http://alexander.kirk.at/2008/07/15/upgrade-wordpress-script/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 08:39:05 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/?p=85</guid>
		<description><![CDATA[Whenever a new version of WordPress comes out (as just WordPress 2.6 did), it is somewhat of a pain to upgrade it. But not for me anymore, because I have created a small (and simple) script some versions ago which I would like to share with you. $ cat upgrade_wordpress.sh wget http://www.wordpress.org/latest.tar.gz mv www wordpress [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever a new version of WordPress comes out (as just <a href="http://wordpress.org/development/2008/07/wordpress-26-tyner/">WordPress 2.6</a> did), it is somewhat of a pain to upgrade it.</p>
<p>But not for me anymore, because I have created a small (and simple) script some versions ago which I would like to share with you.</p>
<p><code><br />
$ cat upgrade_wordpress.sh<br />
wget http://www.wordpress.org/latest.tar.gz<br />
mv www wordpress<br />
tar --overwrite -xzf latest.tar.gz<br />
rm latest.tar.gz<br />
mv wordpress www<br />
</code></p>
<p><tt>www</tt> is my document root and the script sits outside of it. It downloads the most recent version, extracts it while overwriting the already existing files. The script doesn&#039;t contain anything extra-ordinary, but makes upgrading real easy.</p>
<p>Of course this script is only useful if you have ssh access to your web server, but if you do that script might ease the (almost too frequent) pain of upgrading WordPress.</p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2008/07/15/upgrade-wordpress-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>bash completion for the pear command</title>
		<link>http://alexander.kirk.at/2008/05/21/bash-completion-for-the-pear-command/</link>
		<comments>http://alexander.kirk.at/2008/05/21/bash-completion-for-the-pear-command/#comments</comments>
		<pubDate>Wed, 21 May 2008 11:59:33 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/?p=84</guid>
		<description><![CDATA[I am only scratching my own itch here, but maybe someone can use it or expand from it. I just always found annoying that pear run-tests tab gives all files instead of just *.phpt. This is what this snippet actually does. Paste this into the file /opt/local/etc/bash_completion on OSX (for me it is just before [...]]]></description>
			<content:encoded><![CDATA[<p>I am only scratching my own itch here, but maybe someone can use it or expand from it.</p>
<p>I just always found annoying that <tt>pear run-tests <i>tab</i></tt> gives all files instead of just <tt>*.phpt</tt>. This is what this snippet actually does.</p>
<p>Paste this into the file <tt>/opt/local/etc/bash_completion</tt> on OSX (for me it is just before <tt>_filedir_xspec()</tt>) or into a new file <tt>/etc/bash_completion.d/pear</tt> on Debian.</p>
<p><code><br />
# pear completion<br />
#<br />
have pear &#038;&#038;<br />
{<br />
_pear()<br />
{<br />
	local cur prev commands options command</p>
<p>	COMPREPLY=()<br />
	cur=${COMP_WORDS[COMP_CWORD]}</p>
<p>	commands='build bundle channel-add channel-alias channel-delete channel-discover channel-info channel-update clear-cache config-create config-get config-help config-set config-show convert cvsdiff cvstag download download-all info install list list-all list-channels list-files list-upgrades login logout makerpm package package-dependencies package-validate pickle remote-info remote-list run-scripts run-tests search shell-test sign uninstall update-channels upgrade upgrade-all'</p>
<p>	if [[ $COMP_CWORD -eq 1 ]] ; then<br />
		if [[ "$cur" == -* ]]; then<br />
			COMPREPLY=( $( compgen -W '-V' -- $cur ) )<br />
		else<br />
			COMPREPLY=( $( compgen -W "$commands" -- $cur ) )<br />
		fi<br />
	else</p>
<p>		command=${COMP_WORDS[1]}</p>
<p>		case $command in<br />
			run-tests)<br />
				_filedir 'phpt'<br />
				;;<br />
		esac<br />
	fi</p>
<p>	return 0<br />
}<br />
complete -F _pear $default pear<br />
}<br />
</code></p>
<p>Then re-source your <tt>bashrc</tt> or logout and re-login.</p>
<p>I am far from being an expert in bash_completion programming, so I hope someone can go on from here (or maybe has something more complete lying around?). </p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2008/05/21/bash-completion-for-the-pear-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>High Performance Web Sites, a book by Steve Souders</title>
		<link>http://alexander.kirk.at/2007/09/26/high-performance-web-sites/</link>
		<comments>http://alexander.kirk.at/2007/09/26/high-performance-web-sites/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 09:28:30 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/2007/09/26/high-performance-web-sites/</guid>
		<description><![CDATA[I&#039;d like to introduce you to this great book by Steve Souders. There already have been several reports on the Internet about it, for example on the Yahoo Developers Blog. There is also a video of Steve Souders talking about the book. The book is structured into 14 rules, which, when applied properly, can vastly [...]]]></description>
			<content:encoded><![CDATA[<div style="float: right; padding: 5px"><a href="http://www.oreilly.com/catalog/9780596529307/"><img src="http://alexander.kirk.at/img/hpws.gif" border="0" /></a></div>
<p>I&#039;d like to introduce you to this great book by <a href="http://stevesouders.com/">Steve Souders</a>. There already have been several reports on the Internet about it, for example on the <a href="http://developer.yahoo.net/blog/archives/2007/03/high_performanc.html">Yahoo Developers Blog</a>. There is also a <a href="http://video.yahoo.com/video/play?vid=1040890">video of Steve Souders talking about the book</a>.</p>
<p>The book is structured into <a href="http://developer.yahoo.com/performance/rules.html">14 rules</a>, which, when applied properly, can vastly improve the speed of a web site or web application.</p>
<p>Alongside with the book he also introduced <a href="http://developer.yahoo.net/yslow/">YSlow</a>, an extension for the Firefox extension <a href="http://www.getfirebug.com/">FireBug</a>. YSlow helps the developer to see how good his site complies with the rules Steve has set up.</p>
<p>I had the honour to do the technical review on this book, and I love it. Apart from some standard techniques (for example employing HTTP headers like Expires or Last-Modified/Etag), Steve certainly has some tricks up his sleave:</p>
<p>For instance he shows how it is possible to reduce the number of HTTP requests (by inlining the script sources) for first time visitors, while still filling up their cache for their next page load (see page 59ff).</p>
<p>The small down side of this book is that some rules need to be taken with care when applied to smaller environments; for example, it does not make sense (from a cost-benefit perspective) for everyone to employ a CDN. A book just can&#039;t be perfect for all readers.</p>
<p>If you are interested in web site performance and have a developer background, then buy this book (or <a href="http://safari.oreilly.com/9780596529307">read it online</a>). It is certainly something for you. </p>
<p>The book has been published by <a href="http://www.oreilly.com/catalog/9780596529307/">O&#039;Reilly</a> in September 2007, ISBN <a href="http://www.oreilly.com/catalog/9780596529307/">9780596529307</a>.</p>
<p>Some more links on the topic:
<ul>
<li><a href="http://www.oreillynet.com/fyi/blog/2007/09/yahoos_chief_performance_guru.html">Yahoo!&#039;s Chief Performance Guru Talks about Writing his New Book</a></li>
<li><a href="http://www.web2expo.com/presentations/webex2007/souders_steve.ppt">Slides from his presentation at WebEx 2007</a></li>
<li><a href="http://developer.yahoo.net/blog/archives/2007/08/yslow-podcast-screencast.html">Podcast on YSlow</a></li>
</ul>
<p><a href="http://technorati.com/tag/high+performance+web+apps" rel="tag">high performance web apps</a>, <a href="http://technorati.com/tag/steve+souders" rel="tag"> steve souders</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2007/09/26/high-performance-web-sites/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Subversion: The Magic of Merging</title>
		<link>http://alexander.kirk.at/2006/10/12/subversion-the-magic-of-merging-2/</link>
		<comments>http://alexander.kirk.at/2006/10/12/subversion-the-magic-of-merging-2/#comments</comments>
		<pubDate>Thu, 12 Oct 2006 14:22:18 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/2006/10/12/subversion-the-magic-of-merging-2/</guid>
		<description><![CDATA[Use svn merge -rhead:{date} to go back in time with a file]]></description>
			<content:encoded><![CDATA[<p>When programming professionally, <a href="http://subversion.tigris.org/">Subversion</a> is a must-have. Same for system administration: it&#039;s quite a good idea to keep your configuration files (e.g in Linux the whole <tt>/etc/</tt> directory) as a Subversion checkout.</p>
<p>So the goal of Subversion (or any other <a href="http://en.wikipedia.org/wiki/Source_control">Source Control system</a>) is to allow you to do something Apple will introduce with it&#039;s new Leopard operating system: <a href="http://www.apple.com/macosx/leopard/timemachine.html">Time Machine</a>. Go back in time (and restore a version of a file as it was on day x).</p>
<p>Using Subversion on a daily basis is quite easy. Just check in (<tt>svn ci</tt>) your changes after you have completed a certain task. When you work collaboratively, and someone else has committed some changes, you do a <tt>svn up</tt> and the changes of the others are applied to your codebase.</p>
<p>That&#039;s all you basically need. But how can you go back in time now?</p>
<p>So you poke around a bit and find that <tt>svn up</tt> has a parameter <tt>-r</tt> which let&#039;s you put your checkout to the state in which it was at a certain revision.</p>
<p>Let&#039;s suppose we know that something was ok on monday and is not today. So let&#039;s use the command from above to see what it looks like.<br />
<code><br />
~/project/trunk$ svn up -r {2006-10-09} app.php<br />
U app.php<br />
</code><br />
Voila, there it is. Now we choose to use that code now and throw away all changes that have been committed since. We modify the file a bit and do a check in:<br />
<code><br />
~/project/trunk$ svn ci -m "revert to monday" app.php<br />
Sending&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app.php<br />
svn: Commit failed (details follow):<br />
svn: Your file or directory 'app.php' is probably out-of-date<br />
svn: The version resource does not correspond to the resource within the transaction.&nbsp;&nbsp;Either the requested version resource is out of date (needs to be updated), or the requested version resource is newer than the transaction root (restart the commit).<br />
</code><br />
Uh.. ok. So you probably you know that error message already. It is also returned when you want to check something in on a file that has been changed by someone else since your last <tt>svn up</tt>.</p>
<p>When you check something into a subversion repository, one of the basic rules is that the file you want to commit is &#034;up to date&#034;, i.e. the revision number of your local file (updated by <tt>svn up</tt>) equals the number in the repository (on the server).</p>
<p>Ok, so, let&#039;s update our checkout so we can re-run the check in.<br />
<code><br />
~/project/trunk$ svn up<br />
G app.php<br />
</code><br />
So you discover the changes that happened since have been re-inserted to that file again. Maybe Subversion has alerted you of a conflict, because you changed some lines that have been modified since monday also.</p>
<p>Great! Basically we are back to where we started.</p>
<p>Let&#039;s not resign here, but rather use the appropriate command: <tt>svn merge</tt>. That command is mostly known for merging changes from one branch of development to another. But it can also help you to go back in time.</p>
<p>The parameters of <tt>svn merge</tt> are to specify a revision range, which changes to be merged, and a source &#045;&#045;&#045; what part of the subversion repository should be searched for the changes.</p>
<p>Usually one would find this command used in a way like:<br />
<code><br />
~/project/trunk$ svn merge -r 15:26 ../branches/first_release/<br />
G app.php<br />
</code><br />
So with two revisions specified you define a range of changes which should be merged into the current checkout. Ok so how would us help this here? </p>
<p>You can also specify revisions <i>backwards</i>, to go back in time. So to undo the command form before you can write:<br />
<code><br />
~/project/trunk$ svn merge -r 26:15 ../branches/first_release/<br />
G app.php<br />
</code></p>
<p>To put it simple, Subversion generates a <tt>diff</tt> file behind the scenes that incorporates the changes between the given revisions. Then the changes are merged with the files in the same way the <tt>patch</tt> command (Linux, Unix, OS X, &#8230;) does it. When going back in time, the parameter <tt>-R</tt> is used which applies the patch in the reverse direction. Voila.</p>
<p>So as a final solution this leaves us with:<br />
<code><br />
~/project/trunk$ svn merge -r head:{2006-10-09} .<br />
U app.php<br />
~/project/trunk$ svn ci -m "revert to monday" app.php<br />
Sending&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app.php<br />
Transmitting file data .<br />
Committed revision 27.<br />
</code></p>
<p>For further questions, the <a href="http://subversion.tigris.org/faq.html">Subversion FAQ</a> is a good starting point when you know exactly what you want (i.e. the correct terminology). (For example <i>reverting</i> does not mean to go back to a previous version of the file, but rather to remove the changes you did locally).</p>
<p>There is the <a href="http://svnbook.red-bean.com/">subversion book</a> (also published by <a href="http://www.oreilly.com/catalog/0596004486/">O&#039;Reilly</a>), of which the <a href="http://svnbook.red-bean.com/nightly/en/svn.tour.html">Guided Tour</a> is a good starting point.</p>
<p>The process I described above as a trial and error is also described in that book at <a href="http://svnbook.red-bean.com/nightly/en/svn.branchmerge.commonuses.html#svn.branchmerge.commonuses.undo">Undoing changes</a>.</p>
<p>Also <a href="http://bradchoate.com/weblog/2006/07/27/oscon-subversion-best-practices">OSCON: Subversion Best Practices</a>, a transcript of a talk given by the subversion creators (Ben Collins-Sussman and Brian W. Fitzpatrick) by Brad Choate has some good tips.</p>
<p>Have fun :)</p>
<p><a href="http://technorati.com/tag/subversion" rel="tag">subversion</a>, <a href="http://technorati.com/tag/merge" rel="tag"> merge</a>, <a href="http://technorati.com/tag/revert" rel="tag"> revert</a>, <a href="http://technorati.com/tag/time+machine" rel="tag"> time machine</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2006/10/12/subversion-the-magic-of-merging-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Tricks And Good Programming Style</title>
		<link>http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/</link>
		<comments>http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/#comments</comments>
		<pubDate>Wed, 09 Aug 2006 09:39:34 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/?p=65</guid>
		<description><![CDATA[Note that this is an updated version. Original version can be found here. Thanks to the commenters I have updated this post with some better tricks. In a loose series I&#039;d like to point out a few of them. As I am currently mostly programming in JavaScript, I will write most of my samples in [...]]]></description>
			<content:encoded><![CDATA[<p>Note that this is an updated version. <a href="http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style-original-version/">Original version can be found here</a>.</p>
<p>Thanks to the commenters I have updated this post with some better tricks.</p>
<p>In a loose series I&#039;d like to point out a few of them. As I am currently mostly programming in JavaScript, I will write most of my samples in that language; also some of the tricks I mention only apply to JavaScript. But most of them apply to most programming languages around.</p>
<p><b id="optional_parameter_and_default_value">Optional parameter and default value</b> <a href="http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/#optional_parameter_and_default_value">#</a><br />
When defining a function in PHP you can declare optional parameters by giving them a default value (something like <tt>function myfunc($optional = "default value") {}</tt>).</p>
<p>In JavaScript it works a bit differently:<br />
<code><br />
var myfunc = function(optional) {<br />
&nbsp;&nbsp;if (typeof optional == "undefined") {<br />
&nbsp;&nbsp;&nbsp;&nbsp;optional = "default value";<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;alert(optional);<br />
}<br />
</code></p>
<p>This is a clean method to do it. Basically I pretty much recommend the use of <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Special_Operators:typeof_Operator">typeof</a> operator.</p>
<p><b>update</b><br />
<a href="http://mg.to/">Michael Geary</a> (<a href="http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/#comment-4367">his comment</a>) pointed out this solution that I like.</p>
<p><code><br />
var myfunc = function(optional) {<br />
&nbsp;&nbsp;if (optional === undefined) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;optional = "default value";<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;alert(optional);<br />
}<br />
</code></p>
<p>The solutions mentioned (<tt>if (!optional)</tt>, <tt>optional = optional || "default value"</tt>, and the like) have problems when you pass <tt>0</tt> (zero) or <tt>null</tt> as an argument.</p>
<p>Commenters said that the <tt>0/null</tt> problem is not one as this would not be the situation to use it. I would not say so. In an AJAX world where you do serialization back to a server/database often a 0/1 to false/true mapping has to be established. For default values it is important.</p>
<p>In case you just need to make sure that an object is not <tt>null</tt> I do prefer the mentioned<br />
<code><br />
myobject = myobject || { animal: "dog" };<br />
</code><br />
<b>end update</b></p>
<p><b id="parameter_hints">Parameters Hints</b> <a href="http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/#parameter_hints">#</a><br />
The larger your app gets, the more functions you get which you would use throughout the app. It also creates a problem with maintenance. As each function can contain multiple arguments it is not unlikely that you forget what those parameters were for (especially for boolean variables) or mix up their sequence (I am especially gifted for that).</p>
<p>So what I do is this: <b>update</b> substitute variables with comments <b>end update</b><br />
<code><br />
var myfunc2 = function(title, enable_notify) {<br />
&nbsp;&nbsp;// [...]<br />
}<br />
myfunc2(/* title */ "test", /* enable_notify */ true);<br />
</code></p>
<p>This piece of code relies on the functionality of programming languages that the return value of an assignment is the assigned value. (This is something that you should also maintain in your app, for example with database storage calls, give the assignment value as a return value. It&#039;s minimal effort and you might be glad at some point that you did it). </p>
<p>If you do this you can see at any point in the code, what parameters the function takes. Of course this is not always useful, but especially for functions with many parameters it gets very useful.</p>
<p><b id="search_javascript_documentation">Search JavaScript documentation</b> <a href="http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/#search_javascript_documentation">#</a><br />
When I need some documentation for JavaScript I use the mozilla development center (mdc). To quickly search for <tt>toLocaleString</tt>, I use Google: <a href="http://google.com/search?q=toLocaleString+mdc">http://google.com/search?q=toLocaleString+mdc</a></p>
<p>As I am a German speaker I also use the excellent (though a bit out-dated) JavaScript section <a href="http://www.selfhtml.org/">SelfHTML</a>. I use the downloaded version on my own computer for even faster access.</p>
<p><b id="self_variable">The <tt>self</tt> variable</b> <a href="http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/#self_variable">#</a></p>
<p><b>update</b><br />
&#8230; should be avoided. Even if someone like <a href="http://www.crockford.com/">Douglas Crockford</a> (creator of JSON) <a href="http://www.crockford.com/javascript/private.html">uses it and calls it <tt>that</tt></a>.</p>
<p>Let me quote Jack Slocum who put it best:<br />
<code><br />
// used to fix "this" prob with Function.apply to give call proper scope<br />
// nice method to put in your lib<br />
function delegate(instance, method) {<br />
&nbsp;&nbsp;return function() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;return method.apply(instance, arguments);<br />
&nbsp;&nbsp;}<br />
}</p>
<p>function Animal(name) {<br />
&nbsp;&nbsp;this.name = name;<br />
&nbsp;&nbsp;this.hello = function() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;alert("hello " + this.name);<br />
&nbsp;&nbsp;}<br />
}</p>
<p>var dog = new Animal("Jake");<br />
var button = {<br />
&nbsp;&nbsp;onclick : delegate(dog, dog.hello)<br />
};<br />
button.onclick();<br />
</code></p>
<p>I removed my code as it can be considered obsolete by this.<br />
<b>end update</b></p>
<p><b id="reduce_indentation_amount ">Reduce indentation amount</b> <a href="http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/#reduce_indentation_amount">#</a></p>
<p><b>update</b><br />
I have removed the code because it leads people into believing something different than I meant. So let me put it differently:</p>
<p>What I am opposing is white space deserts. If you have many levels of indentation then probably something is wrong.</p>
<p>If a <tt>for</tt> loop only applies to a handful of cases, don&#039;t indent the whole loop in an <tt>if</tt> clause but rather catch the other cases at the top.<br />
Often it is advisable to move longer functionality to a function (there is a good reason for that name) that you call throughout a loop.<br />
<b>end update</b></p>
<p>That&#039;s all for now, to be continued. Further readings on this blog:</p>
<ul>
<li><a href="http://alexander.kirk.at/2006/03/02/a-better-understanding-of-javascript/">A better understanding of JavaScript</a></li>
<li><a href="http://alexander.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/">Misuse of the Array Object in JavaScript</a>
</li>
</ul>
<p><b>update</b><br />
Eventhough some commenters disagreed with what I said, I think posts like this are very much needed in the bloggersphere. Even if they are not free of errors on the first take, great people can help improve them. I would appreciate if more people took that risk.<br />
<b>end update</b></p>
<p><a href="http://technorati.com/tag/javascript" rel="tag">javascript</a>, <a href="http://technorati.com/tag/tricks" rel="tag"> tricks</a>, <a href="http://technorati.com/tag/coding+practices" rel="tag"> coding practices</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>JavaScript Tricks And Good Programming Style &#045;&#045; Original Version</title>
		<link>http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style-original-version/</link>
		<comments>http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style-original-version/#comments</comments>
		<pubDate>Tue, 08 Aug 2006 09:39:17 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style-original-version/</guid>
		<description><![CDATA[Note that there is an updated version I have been programming for about 10 years now, and I am always longing for improving my code. Throughout time I added a few habbits that I consider to be good practices and increase the quality of my code. In a loose series I&#039;d like to point out [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://alexander.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/">Note that there is an updated version</a></p>
<p>I have been programming for about 10 years now, and I am always longing for improving my code. Throughout time I added a few habbits that I consider to be good practices and increase the quality of my code.</p>
<p>In a loose series I&#039;d like to point out a few of them. As I am currently mostly programming in JavaScript, I will write most of my samples in that language; also some of the tricks I mention only apply to JavaScript. But most of them apply to most programming languages around.</p>
<p><b id="original_optional_parameter_and_default_value">Optional parameter and default value</b> <a href="http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style/#optional_parameter_and_default_value">#</a><br />
When defining a function in PHP you can declare optional parameters by giving them a default value (something like <tt>function myfunc($optional = "default value") {}</tt>).</p>
<p>In JavaScript it works a bit differently:<br />
<code><br />
var myfunc = function(optional) {<br />
&nbsp;&nbsp;if (typeof optional == "undefined") {<br />
&nbsp;&nbsp;&nbsp;&nbsp;optional = "default value";<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;alert(optional);<br />
}<br />
</code></p>
<p>This is a clean method to do it. Basically I pretty much recommend the use of <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Special_Operators:typeof_Operator">typeof</a> operator. Some people would do the above with a <tt>if (!optional)</tt>, but my version works cross browser (e.g. Safari will throw an error when you try to negate <tt>null</tt>).</p>
<p><b id="original_parameter_hints">Parameters Hints</b> <a href="http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style/#parameter_hints">#</a><br />
The larger your app gets, the more functions you get which you would use throughout the app. It also creates a problem with maintenance. As each function can contain multiple arguments it is not unlikely that you forget what those parameters were for (especially for boolean variables) or mix up their sequence (I am especially gifted for that).</p>
<p>So what I do is this:<br />
<code><br />
var myfunc2 = function(title, enable_notify) {<br />
&nbsp;&nbsp;// [...]<br />
}<br />
myfunc2(title = "test", enable_notify = true);<br />
</code></p>
<p>This piece of code relies on the functionality of programming languages that the return value of an assignment is the assigned value. (This is something that you should also maintain in your app, for example with database storage calls, give the assignment value as a return value. It&#039;s minimal effort and you might be glad at some point that you did it). </p>
<p>If you do this you can see at any point in the code, what parameters the function takes. Of course this is not always useful, but especially for functions with many parameters it gets very useful.</p>
<p>Also be careful that you would override the variable names in the scope of which you are calling the function. You might mini-namespace the variables, e.g. with letter+underscore (<tt>p_title, p_enable_notify</tt>).</p>
<p><b id="original_search_javascript_documentation">Search JavaScript documentation</b> <a href="http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style/#search_javascript_documentation">#</a><br />
When I need some documentation for JavaScript I use the mozilla development center (mdc). To quickly search for <tt>toLocaleString</tt>, I use Google: <a href="http://google.com/search?q=toLocaleString+mdc">http://google.com/search?q=toLocaleString+mdc</a></p>
<p>As I am a German speaker I also use the excellent (though a bit out-dated) JavaScript section <a href="http://www.selfhtml.org/">SelfHTML</a>. I use the downloaded version on my own computer for even faster access.</p>
<p><b id="original_self_variable">The <tt>self</tt> variable</b> <a href="http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style/#self_variable">#</a><br />
This technique comes from <a href="http://www.crockford.com/javascript/private.html">Private Members in JavaScript by Douglas Crockford</a>. By assigning a value in a function to <tt>this.value</tt> it will be publically accessible afterwards.<br />
<code><br />
function Animal(name) {<br />
&nbsp;&nbsp;this.name = name;<br />
&nbsp;&nbsp;var self = this;<br />
&nbsp;&nbsp;this.hello = function() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;alert("hello " + self.name);<br />
&nbsp;&nbsp;&nbsp;&nbsp;//alert("hello " + this.name); // would fail<br />
&nbsp;&nbsp;}<br />
}<br />
var dog = new Animal("Jake");<br />
button = {<br />
&nbsp;&nbsp; onclick = dog.hello;<br />
}<br />
button.onclick();<br />
</code></p>
<p>The cause of this problem is that the <tt>this</tt> keyword receives different values in different contexts. See <a href="http://www.youngpup.net/2005/0525024806/comments#c1444">here for a closer explanation</a>.</p>
<p>Problem with this solution is that I am not absolutely sure if this creates a <a href="http://simon.incutio.com/slides/2006/etech/javascript/js-reintroduction-notes.html#memoryleaks">memory leak in internet explorer</a></p>
<p><b id="original_reduce_indentation_amount ">Reduce indentation amount</b> <a href="http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style/#reduce_indentation_amount">#</a><br />
One of the most annoying things I find in other people&#039;s code is this: (multiple) nested if clauses. Something like this:<br />
<code><br />
var arr = ["dog", "cat"];<br />
var action = 'greet';<br />
for(i = 0, ln = arr.length; i < ln; i++) {<br />
&nbsp;&nbsp;animal = arr[i];<br />
&nbsp;&nbsp;if (animal == "cat") {<br />
&nbsp;&nbsp;&nbsp;&nbsp;alert("hello " + animal);<br />
&nbsp;&nbsp;}<br />
}<br />
</code></p>
<p>This is only a short example, but I often saw this going deep into 10 levels of nested clauses. I suggest using the <tt>break</tt> and <tt>continue</tt> (and <tt>next</tt> in Perl):<br />
</code><code><br />
var arr = ["dog", "cat"];<br />
for(i = 0, ln = arr.length; i < ln; i++) {<br />
	animal = arr[i];<br />
	if (animal != "cat") continue;<br />
	alert("hello " + animal);<br />
}<br />
</code></p>
<p>This accomplishes the same with only one level of indentation. One more example for a function:<br />
</code><code><br />
function greet_animal(animal) {<br />
&nbsp;&nbsp;if (typeof animal == "undefined") return;<br />
&nbsp;&nbsp;if (animal != "cat") return;<br />
&nbsp;&nbsp;alert("hello " + animal);<br />
}<br />
</code></p>
<p>Javascript is one of the few languages where you can leave the return value empty (i.e. <tt>typeof greet_animal() == "undefined"</tt>). You might want to rather use <tt>return false</tt> so that you can easily determine if the function failed for some reason.</p>
<p><a href="http://technorati.com/tag/javascript" rel="tag">javascript</a>, <a href="http://technorati.com/tag/tricks" rel="tag"> tricks</a>, <a href="http://technorati.com/tag/coding+practices" rel="tag"> coding practices</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2006/08/08/javascript-tricks-and-good-programming-style-original-version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Firefox 1.5, XmlHttpRequest, req.responseXML and document.domain</title>
		<link>http://alexander.kirk.at/2006/07/27/firefox-15-xmlhttprequest-reqresponsexml-and-documentdomain/</link>
		<comments>http://alexander.kirk.at/2006/07/27/firefox-15-xmlhttprequest-reqresponsexml-and-documentdomain/#comments</comments>
		<pubDate>Thu, 27 Jul 2006 13:07:30 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/?p=63</guid>
		<description><![CDATA[Recently I have been working on a web application, extending it with an iframe on another subdomain. When you set up communication with an iframe on another subdomain, it works by setting document.domain in both pages. Pretty nice and straight forward. But it can mess up the rest of your page. As soon as you [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been working on a web application, extending it with an iframe on another subdomain.</p>
<p>When you set up communication with an iframe on another subdomain, it works by setting <tt>document.domain</tt> in both pages. Pretty nice and straight forward.<br />
But it can mess up the rest of your page.</p>
<p>As soon as you have set <tt>document.domain</tt> you should be able to do an XHR to your original domain according to the same domain policy.</p>
<p>This will work in IE, Safari, and Opera.<br />
This will <b>not</b> work in Firefox 1.0. This is very awkward but at least it has been <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=290100">fixed in 1.5</a>.<br />
So it will work in Firefox 1.5. <b>But:</b></p>
<p>The responseXML object is useless. You can&#039;t access it, you receive a Permission Denied when trying to access it&#039;s content (e.g. documentElement). Very annoying.<br />
Even stranger that responseText is still readable. What&#039;s the reason for this? Is there some security risk i am unaware of or is it a plain bug?</p>
<p>As the responseText is available there is a pretty simple fix: re-parse the XML, which is kinda stupid and cpu intense if you have a lot of them. (something like: <tt>var doc =<br />
(new DOMParser()).parseFromString(req.responseText, "text/xml");</tt>)</p>
<p>I have some <a href="/cross_subdomain_iframe/">sample code available here</a>.</p>
<p>Apparently a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=326337">bug report has been filed at 1.5.0.1</a>. No response from developers. Great.<br />
Unfortunately it has only been filed for OSX, but it also afffects Windows Firefox.</p>
<p>Mozilla guys, fix this ASAP.</p>
<p><b>Update 2007-06-21:</b> Things seem to start moving, we will likely <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=326337">have a fix</a> for Firefox 3.</p>
<p><a href="http://technorati.com/tag/firefox+bug" rel="tag">firefox bug</a>, <a href="http://technorati.com/tag/document.domain" rel="tag"> document.domain</a>, <a href="http://technorati.com/tag/XmlHttpRequest" rel="tag"> XmlHttpRequest</a>, <a href="http://technorati.com/tag/responseXML" rel="tag"> responseXML</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2006/07/27/firefox-15-xmlhttprequest-reqresponsexml-and-documentdomain/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Misuse of the Array Object in JavaScript</title>
		<link>http://alexander.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/</link>
		<comments>http://alexander.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/#comments</comments>
		<pubDate>Thu, 18 May 2006 18:06:54 +0000</pubDate>
		<dc:creator>Alexander Kirk</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://alexander.kirk.at/?p=62</guid>
		<description><![CDATA[There is a very good post about Associative Arrays considered harmful by Andrew Dupont. The title is a bit misleading but correct. When coming accross a piece of JavaScript like this foo["test"] = 1; there is nothing wrong about it. It&#039;s the basic usage scheme of assoziative arrays. Or should i rather say objects? While [...]]]></description>
			<content:encoded><![CDATA[<p>There is a very good post about <a href="http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/">Associative Arrays considered harmful</a> by <a href="http://www.andrewdupont.net/about/">Andrew Dupont</a>.</p>
<p>The title is a bit misleading but correct. When coming accross a piece of JavaScript like this<br />
<code>foo["test"] = 1;</code><br />
there is nothing wrong about it. It&#039;s the basic usage scheme of assoziative arrays. Or should i rather say objects?</p>
<p>While in languages such as PHP arrays used like this <code>$foo = array("test" => 1);</code> is perfectly correct.</p>
<p>In JavaScript<br />
<code>var foo = new Array();<br />
foo["test"] = 1;</code><br />
works but does not do what you want.</p>
<p>I don&#039;t need to repeat Andrew&#039;s really great post, but basically you should use <code>Object</code> instead of <code>Array</code>.</p>
<p><code>var foo = new Object(); // same as var foo = {};<br />
foo["test"] = 1; // same as foo.test = 1;</code></p>
<p>Now go and read <a href="http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/">Andrew&#039;s post</a>.</p>
<p>via <a href="http://erik.eae.net/archives/2006/05/18/10.31.01/">Erik Arvidsson</a>.</p>
<p>btw: that post lead me to <a href="http://erik.eae.net/archives/2005/06/06/22.13.54/">Object.prototype is verboten</a> which explains for me why my <code>for (i in myvar) {}</code> loops never worked correctly. I was using <a href="http://prototype.conio.net/">prototype.js</a> version &lt; 1.4 (which messed with Object.prototype).</p>
<p><a href="http://technorati.com/tag/javascript" rel="tag">javascript</a>, <a href="http://technorati.com/tag/array" rel="tag"> array</a>, <a href="http://technorati.com/tag/object" rel="tag"> object</a>, <a href="http://technorati.com/tag/prototype.js" rel="tag"> prototype.js</a>, <a href="http://technorati.com/tag/Object.prototype" rel="tag"> Object.prototype</a></p>
]]></content:encoded>
			<wfw:commentRss>http://alexander.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
