<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.1" -->
<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/"
	>

<channel>
	<title>CodeUtopia - The blog of Jani Hartikainen</title>
	<link>http://codeutopia.net/blog</link>
	<description>Software development with a focus on web-related technologies</description>
	<pubDate>Thu, 11 Mar 2010 15:58:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
	<language>en</language>
			<item>
		<title>Should a failed function return a value or throw an exception?</title>
		<link>http://codeutopia.net/blog/2010/03/11/should-a-failed-function-return-a-value-or-throw-an-exception/</link>
		<comments>http://codeutopia.net/blog/2010/03/11/should-a-failed-function-return-a-value-or-throw-an-exception/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 15:58:54 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2010/03/11/should-a-failed-function-return-a-value-or-throw-an-exception/</guid>
		<description><![CDATA[You have created a nice, well written function, but you realize you forgot something: The failure case. 
What should a function do when it fails? There are two schools for this - the &#8220;throw an exception&#8221; school and the &#8220;return an error value&#8221; school.
But which of these is the correct approach?

Some background
Back in ye olden [...]]]></description>
			<content:encoded><![CDATA[
			<p>You have created a nice, well written function, but you realize you forgot something: The failure case. </p>
<p>What should a function do when it fails? There are two schools for this - the &#8220;throw an exception&#8221; school and the &#8220;return an error value&#8221; school.</p>
<p>But which of these is <em>the</em> correct approach?</p>
<p><span id="more-282"></span></p>
<h3>Some background</h3>
<p>Back in ye olden times there were no exceptions. Languages like C are still being used today. In such languages the choice is quite easy to make.</p>
<p>However, what about languages that do support exceptions? Should you strive to use the language&#8217;s features to their fullest, and always throw an exception? Are error returns just relics?</p>
<p>If you listen to people talk about this, it often turns into a debate about what they like. If you grew up with languages that had no exceptions, chances are you still prefer return codes. Similarly, if you used a lot of Java or other exceptional language, you might end up flaming return codes.</p>
<p>However, I think both of them deserve merit and have their uses.</p>
<h3>Basics</h3>
<p>As you may have heard, <em>exceptions are for exceptional conditions</em>. </p>
<p>But what is an exceptional condition? What isn&#8217;t?</p>
<p>Let&#8217;s take an example: Your application attempts to load a resource file - for example the configuration settings for your database - and it&#8217;s missing.</p>
<p>Is this an exceptional condition? <em>Maybe</em>. It depends on one thing: Is it normal for it to be missing, or should the file always be there?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> loadConfiguration<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> file = <span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'configuration.ini'</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>exists<span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    read<span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #3366CC;">&quot;Configuration file is missing&quot;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>So this pseudo-code could be a simplified configuration loading function. In this case the configuration file has to exist - let&#8217;s say it&#8217;s some internal file.</p>
<p>But what if the file is something that contains settings the user has selected? In that case it might be perfectly fine for the file to not exist. The user may not have added any custom settings and as such, it&#8217;s normal for the file to be missing. In this case our function could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> tryLoadConfiguration<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> file = <span style="color: #000066;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'configuration.ini'</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>exists<span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    read<span style="color: #66cc66;">&#40;</span>file<span style="color: #66cc66;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Note that I also changed the name of the function a bit. It may be a good idea to try indicating that the function will be returning a value - It tries to do it, so the name is more expressive of the permissive nature and that it&#8217;ll return whether it succeeded or not.</p>
<h3>Other examples</h3>
<p>Other good examples are various string related functions. In many languages, there exists a function to determine whether a string contains another string, and this function usually return <code>-1</code> on failure.</p>
<p>It&#8217;s normal for this function to not succeed, afterall, we are using it to test whether or not the condition is met.</p>
<p>There are also more &#8220;pure&#8221; arguments for either side. Exceptions are essentially objects, so they can convey more information than just the fact that things went wrong. On the other hand, an exception can be thrown deep in the code but appear on the surface unless caught early enough. This could make tracking the origin and cause of the exception more difficult.</p>
<p>Ned Batcheler has some <a href="http://nedbatchelder.com/text/exceptions-vs-status.html">good reasons for exceptions compared to error returns</a> on his blog in addition to these. However, is this all?</p>
<h3>Deeper considerations</h3>
<p>It&#8217;s not all just talk about exceptional conditions or technological preferences. </p>
<p>An often forgotten fact is <em>application stability</em>. If you are writing critical code that should be as stable as possible, what should you use? <strong>Return values</strong>.</p>
<p>An uncaught exception can bring down a whole application. An unhandled return code might not really cause anything at all. Sometimes it&#8217;s important to consider this factor too.</p>
<p>There are probably other things like this too. If you know any more examples of more special things to take into account, post a comment please.</p>
<h3>Conclusion</h3>
<p>We can say that exceptions can be convenient. We can also say you can easily use exceptions too much, and that how to decide whether to use them or return codes is difficult. It comes with experience, but I hope this post sparked some ideas for you. Damien Katz&#8217;s blog also has a (long, long) post on <a href="http://damienkatz.net/2006/04/error_code_vs_e.html">exceptions, error codes and reliability</a> which is kind of related.</p>
<p>When do you think it&#8217;s a good idea to use exceptions? Return values? How do you decide? Share in the comments!</p>

			<a href="http://codeutopia.net/blog/2010/03/11/should-a-failed-function-return-a-value-or-throw-an-exception/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/03/11/should-a-failed-function-return-a-value-or-throw-an-exception/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Benefits of Developing With Microsoft AND Open Source</title>
		<link>http://codeutopia.net/blog/2010/02/26/benefits-of-developing-with-microsoft-and-open-source/</link>
		<comments>http://codeutopia.net/blog/2010/02/26/benefits-of-developing-with-microsoft-and-open-source/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 18:06:44 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[general]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2010/02/26/benefits-of-developing-with-microsoft-and-open-source/</guid>
		<description><![CDATA[The Internet seems to be the latest battleground for the computer age-old struggle between Microsoft and open source solutions. For some reason, many web developers like to engage in holy wars over various web site hosting solutions and development platforms, fiercely defending their beloved vendor&#8217;s suite of products. They battle over their particular setup so [...]]]></description>
			<content:encoded><![CDATA[
			<p>The Internet seems to be the latest battleground for the computer age-old struggle between Microsoft and open source solutions. For some reason, many web developers like to engage in holy wars over various <a href="http://www.netfirms.ca">web site hosting</a> solutions and development platforms, fiercely defending their beloved vendor&#8217;s suite of products. They battle over their particular setup so much they block out any new ideas they might gain had they kept their minds open to how things are done on the other side of the castle walls.</p>
<p><span id="more-281"></span></p>
<p>Different platforms are developed by different groups, each with their own mindsets and ways of doing things. If you take the time to step outside your own world, you might be able to learn new ways of doing stuff you never would have if you stuck to the familiar toolsets of your current platform. Take PHP and ASP, for example. Although very similar in nature, much of the &#8220;nitty gritty&#8221; implementation details are considerably different.</p>
<p>It&#8217;s actually in a developer&#8217;s best interests to know the best of both worlds. The Internet is heavily intertwined, with many companies seeking out <a href="http://www.hosting.com/cloudhosting/">cloud hosting</a> solutions while integrating more than one vendor&#8217;s product. Besides, whether you like it or not, Microsoft maintains a huge presence in the computing world, and they aren&#8217;t going anywhere anytime soon. Plus with today&#8217;s mergers and acquisitions, many companies are no longer homogeneous with their technologies even if they originally started out that way. Knowing more than one technology would put you in a good career position for interfacing and merging new in-house technologies. </p>
<p>In today&#8217;s lackluster economy, what&#8217;s more important to you anyway: platform snobbery or job security? Let&#8217;s say you&#8217;re working in a code shop that&#8217;s totally dependent upon some open source LAMP flavor. But one day that place, like many dot coms of recent past, goes belly-up. It sure would be nice to be able to immediately jump ship to another place dependent mostly on ASP.NET, wouldn&#8217;t it?</p>
<p>So break out of the mold. Stop thinking whatever IDE you&#8217;re using is the best thing since pre-sliced bagels. Besides, &#8220;real&#8221; programmers don&#8217;t cling do their toolset. Real programmers don&#8217;t brag about how good they are at using &#8220;x&#8221; text editor or &#8220;y&#8221; compiler. Real programmers aren&#8217;t clueless like the recruiters who land them their jobs, mindlessly performing keyword scanning for the latest technology buzzwords. Real programmers are supposed to know how to determine/evaluate requirements, design systems, and write the code necessary for the task at hand, finding the right tools for the job instead of turning every problem into a nail for their only hammer.</p>
<p><strong>Disclaimer:</strong> This is a sponsored post not written by the blogger. </p>
<p>Please leave your thoughts on this, and whether you found it an interesting read in the comments. I wanted to publish this one to see how it works out. Depending on your comments I may or may not run more sponsored articles.</p>

			<a href="http://codeutopia.net/blog/2010/02/26/benefits-of-developing-with-microsoft-and-open-source/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/02/26/benefits-of-developing-with-microsoft-and-open-source/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Should PC&#8217;s be easy to use for everyone?</title>
		<link>http://codeutopia.net/blog/2010/02/24/should-pcs-be-easy-to-use-for-everyone/</link>
		<comments>http://codeutopia.net/blog/2010/02/24/should-pcs-be-easy-to-use-for-everyone/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 17:02:37 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2010/02/24/should-pcs-be-easy-to-use-for-everyone/</guid>
		<description><![CDATA[Should PC&#8217;s be easy to use for everyone, or should you actually need some kind of understanding of how they work?
I recently saw an article about a new computer aimed at &#8220;computer illiterate&#8221; users, the Alex PC. Elsewhere on the &#8216;net, people were arguing about whether or not trying to make it easy for &#8220;non-geeks&#8221; [...]]]></description>
			<content:encoded><![CDATA[
			<p>Should PC&#8217;s be easy to use for everyone, or should you actually need some kind of understanding of how they work?</p>
<p>I recently saw an article about a new computer aimed at &#8220;computer illiterate&#8221; users, the <a href="http://www.electronista.com/articles/10/02/23/alex.pc.in.the.uk.gets.broadband.ease.of.use/">Alex PC</a>. Elsewhere on the &#8216;net, people were arguing about whether or not trying to make it easy for &#8220;non-geeks&#8221; to use computers and internet was a good idea or not, which provoked some thoughts which I thought I&#8217;d share.</p>
<p><span id="more-280"></span></p>
<h3>Why not?</h3>
<p>If you think about it, why would it be a bad thing?</p>
<p>Well, I&#8217;m sure we&#8217;ve all helped &lt;insert someone who knows very little about computers here&gt; - some of us may even be the go-to guy for all family and cousins for computer problems (thank god I&#8217;m not) - and let&#8217;s face it, it&#8217;s not always very fun; It can be very frustrating as a matter of fact.</p>
<p>That&#8217;s not all though. Why do botnets grow, despite all the measures we take to protect our computers? Yep, a lot of these are most likely computers from people who don&#8217;t understand them very well. Security issues such as this and phishing can be major concerns unless you understand things correctly (and even if you do things can slip past you)</p>
<p>So we can safely say that uneducated users can fall prey to various things on the internet, and computer problems that fall on the shoulders of others aren&#8217;t always fun to work out.</p>
<p>I can&#8217;t find the discussion at the moment, but a lot of Linux people (the Alex PC uses Linux) were also saying the same - If you can&#8217;t use computers, you shouldn&#8217;t, and computer makers shouldn&#8217;t cater to them.</p>
<h3>Apple has figured it out</h3>
<p>This may come as a bit of a surprise from me, as I&#8217;m not exactly the first person to praise Apple&#8217;s products. I often call their stuff &#8220;fashion gadgets&#8221;, in good humor of course <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Especially after they unveiled the iPad I was pretty quick to join the ranks of people who said it was a pointless, pointless device, with tons of better alternatives available.</p>
<p>But that&#8217;s only if you think of it as a <em>computer&#8217;ish, tablet PC or smartphone</em> type.</p>
<p>If you think of the iPad as a beginning of something everyday-like, such as a TV, but for internet use etc., it may suddenly start making some sense.</p>
<p>For a computer for computer illiterate to work, <strong>it must not be a computer</strong>.</p>
<p>Part of the problem could be that some people are indeed <em>afraid</em> of using computers. That&#8217;s why the device shouldn&#8217;t be marketed as one, and that&#8217;s why I say Apple may have nailed this with the iPad.</p>
<h3>Not a computer?</h3>
<p>When you think of everyday appliances that even children and the elderly can use, such as TV and radio, what do they share?</p>
<p>They are both much less complex to operate than a computer.</p>
<p>To watch TV, you just turn it on. It turns on usually pretty quick, and that&#8217;s it. It&#8217;s on, it&#8217;s working. You don&#8217;t need to do anything else. </p>
<p>What happens when you have problems with a TV? You turn it off and back on, which usually fixes it. If the image doesn&#8217;t show, you press a button to have it automatically search for channels and so on. </p>
<p>When you turn on a computer, it boots up. You get a few screens, which each in turn tell you to press a key if you want to do thing X (such as go to BIOS). Then it takes some more time. You wait, probably input a username and a password, wait, and now you get to the desktop. </p>
<p>You probably know you don&#8217;t want to go to the BIOS or the Windows Memory Diagnostic tool on bootup, but someone who doesn&#8217;t know what they are doesn&#8217;t know whether they should be pressing the keys or not. </p>
<p>I could go on about the things that happen after you&#8217;re in the OS, but I think you get the point - just starting a computer can be an annoying and confusing experience.</p>
<h3>How does a non-computer computer work?</h3>
<p>Let&#8217;s take all the cues from the appliances that are in everyday use: Make it boot up in seconds, not tens of seconds and make it simpler.</p>
<p>I haven&#8217;t used an iPad, but I think it (or a similar device) could fit the bill pretty nicely here.</p>
<p>You start it, it boots up (hopefully very quickly) and you get the applications. In this way, the iPhone OS works perfectly for it, as it abstracts things like applications and file storage - Things just are somewhere and they are always presented in a similar fashion.</p>
<p>When the device or application crashes, the user just reboots it. That should fix it. It should not require understanding cryptic error messages or require changing a setting or anything. Just restart it and it&#8217;ll work again just fine (This is why it should boot very fast)</p>
<p>Currently there are some fast booting Linux distros available which could suit this bill. Also, in some Asus motherboards there&#8217;s a technology dubbed the ExpressGate: Power up PC, and ExpressGate is ready for internet browsing, chat or skype or whatever in a matter of seconds. It&#8217;s essentially a Linux with a custom, simplified, window manager. Features like these could make using a computer simpler as well, however I think a separate not-a-computer type device could work better.</p>
<h3>Great benefits</h3>
<p>I think an internet capable device as simple to use as a TV could bring great benefits. Even though the computer is almost ubiquitous nowadays, there are still plenty of people who don&#8217;t own one.  </p>
<p>Everyone should have a device capable of accessing the internet, if only to use Wikipedia <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /></p>

			<a href="http://codeutopia.net/blog/2010/02/24/should-pcs-be-easy-to-use-for-everyone/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/02/24/should-pcs-be-easy-to-use-for-everyone/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Can you make JavaScript&#8217;s String mutable?</title>
		<link>http://codeutopia.net/blog/2010/02/12/can-you-make-javascripts-string-mutable/</link>
		<comments>http://codeutopia.net/blog/2010/02/12/can-you-make-javascripts-string-mutable/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 18:01:46 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2010/02/12/can-you-make-javascripts-string-mutable/</guid>
		<description><![CDATA[Here&#8217;s a question I was pondering a couple of days ago: Can you make JavaScript&#8217;s String object mutable, as in modifiable without having to re-assign the string.
Why would this be useful? Sometimes you might share a string between objects, and you&#8217;d want changing it in one place get reflected in elsewhere. If you had a [...]]]></description>
			<content:encoded><![CDATA[
			<p>Here&#8217;s a question I was pondering a couple of days ago: Can you make JavaScript&#8217;s String object mutable, as in modifiable without having to re-assign the string.</p>
<p>Why would this be useful? Sometimes you might share a string between objects, and you&#8217;d want changing it in one place get reflected in elsewhere. If you had a single string object, which was referenced from multiple places, this would be simple.</p>
<p>But it turns out, it isn&#8217;t&#8230;</p>
<p><span id="more-279"></span></p>
<h3>foo = &#8217;string&#8217; vs foo = new String(&#8217;string&#8217;)</h3>
<p>You may be aware that you can use either of these two syntaxes to declare a string in JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> strLiteral = <span style="color: #3366CC;">'I am a string literal'</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> strObject = <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'I am a string object'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>But what&#8217;s the difference between these two?</p>
<p>With string literals, assigning the string to another variable copies it, but any properties assigned to it are lost:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> literal = <span style="color: #3366CC;">'hello'</span>;
literal.<span style="color: #006600;">prop</span> = <span style="color: #3366CC;">'xyz'</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> copy = literal;
&nbsp;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>copy.<span style="color: #006600;">prop</span><span style="color: #66cc66;">&#41;</span>
-&gt; undefined</pre></div></div>

<p>If you use new String, it&#8217;s again copied, but since it&#8217;s an object, it&#8217;ll copy the reference. As such, the properties of the object are still there:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> obj = <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'hello'</span><span style="color: #66cc66;">&#41;</span>;
obj.<span style="color: #006600;">prop</span> = <span style="color: #3366CC;">'xyz'</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> copy = obj;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>copy.<span style="color: #006600;">prop</span><span style="color: #66cc66;">&#41;</span>;
-&gt; <span style="color: #3366CC;">'xyz'</span></pre></div></div>

<p>Maybe we can use this to our advantage?</p>
<h3>Making string objects behave funny</h3>
<p>If you&#8217;re familiar with String functions in JavaScript, you may recall that none of them actually modify the string itself - They all return a new string with the modifications.</p>
<p>How could we change a string, then?</p>
<p><em>Array to the rescue!</em></p>
<p>We can use array&#8217;s functions on strings to try and hack it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #009900; font-style: italic;">//Try appending to string</span>
Array.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">push</span>.<span style="color: #006600;">call</span><span style="color: #66cc66;">&#40;</span>x, <span style="color: #3366CC;">' '</span>, <span style="color: #3366CC;">'b'</span>, <span style="color: #3366CC;">'a'</span>, <span style="color: #3366CC;">'r'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Turns out this doesn&#8217;t quite work as expected. If you output <code>x</code>, you still get <code>'foo'</code>. However, if you iterate all properties in it, you will actually see the pushed characters.</p>
<p>Perhaps something else is affecting the output?</p>
<p>In JavaScript objects, the <em>toString</em> method is used to convert them into string representations. Perhaps if we override toString?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #003366; font-weight: bold;">new</span> String<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span>;
x.<span style="color: #006600;">toString</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'foo bar'</span>; <span style="color: #66cc66;">&#125;</span>;
&nbsp;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>;
-&gt; <span style="color: #3366CC;">'foo'</span>
&nbsp;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>String<span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
-&gt; <span style="color: #3366CC;">'foo bar'</span></pre></div></div>

<p>Turns out toString isn&#8217;t used when it comes to strings. However, when casting the variable to string with the String constructor, it outputs the value from toString.</p>
<p>Confusing is it not?</p>
<h3>So what can we actually do?</h3>
<p>It does look like strings can&#8217;t really be made mutable in a sensible way, doesn&#8217;t it?</p>
<p>But there is one more alternative: A custom string class.</p>
<p>We can create a <em>MutableString</em> class:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> MutableString = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">text</span> = value;
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
MutableString.<span style="color: #006600;">prototype</span> = <span style="color: #66cc66;">&#123;</span>
  toString: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">text</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #003366; font-weight: bold;">new</span> MutableString<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bar'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>;
-&gt; object
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> y = <span style="color: #3366CC;">'foo '</span> + x;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span>y<span style="color: #66cc66;">&#41;</span>;
-&gt; foo bar</pre></div></div>

<p>In this case the variable appears as an object when logging, but the toString method gets called when it&#8217;s cast to string, such as when concatenating it with strings.</p>
<p>This example doesn&#8217;t show how you&#8217;d actually change a <em>MutableString</em> - You will need to add some methods that will allow changing the value of the text property to make any use of this as anything else than a string wrapper.</p>
<h3>The usefulness of this exercise</h3>
<p>In the end, this isn&#8217;t a very useful concept - Most of the time you don&#8217;t really need strings to be modifiable like this.</p>
<p>What we can learn from this is that JavaScript&#8217;s built-in functions are surprisngly versatile - We called a function from the Array object on a String, and it actually worked (but due to how string works it did not provide the expected results)</p>
<p>If you know something that I don&#8217;t, and are actually aware of how this could be done without silly hacks like the MutableString class, feel free to point it out in the comments!</p>

			<a href="http://codeutopia.net/blog/2010/02/12/can-you-make-javascripts-string-mutable/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/02/12/can-you-make-javascripts-string-mutable/feed/</wfw:commentRss>
		</item>
		<item>
		<title>6 programming project mistakes you should avoid</title>
		<link>http://codeutopia.net/blog/2010/01/28/6-programming-project-mistakes-you-should-avoid/</link>
		<comments>http://codeutopia.net/blog/2010/01/28/6-programming-project-mistakes-you-should-avoid/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 16:45:34 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2010/01/28/6-programming-project-mistakes-you-should-avoid/</guid>
		<description><![CDATA[During my adventures in programming, I&#8217;ve been involved in many projects. Luckily, despite having made some mistakes, they&#8217;ve gone quite well. Here are a few of them, with tips on how to avoid making them yourself.

No version control
This is a pretty bad one! I&#8217;m glad I learned to use SVN at an early age&#8230;  [...]]]></description>
			<content:encoded><![CDATA[
			<p>During my adventures in programming, I&#8217;ve been involved in many projects. Luckily, despite having made some mistakes, they&#8217;ve gone quite well. Here are a few of them, with tips on how to avoid making them yourself.</p>
<p><span id="more-278"></span></p>
<h3>No version control</h3>
<p>This is a pretty bad one! I&#8217;m glad I learned to use SVN at an early age&#8230; <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Always use version control in your projects. Be it Git, SVN or even CVS, as long as you use something. It&#8217;ll help you avoid a thousand problems!</p>
<p>Here&#8217;s some of my <a href="http://codeutopia.net/blog/tag/git/">posts about Git</a>.</p>
<h3>No plan</h3>
<p>It may feel like just having a general idea (eg. &#8220;Hey, let&#8217;s make a forum!&#8221;) is alright, but it isn&#8217;t.</p>
<p>With no plan or roadmap, your project may easily derail into more and more new features, never actually getting anything finished.</p>
<p>Always try to create some kind of a plan that includes the features you want. Make it a Word document, or even better, put it in a tool like Trac or JIRA where you can easily track what you are working on. Not only will this give you a clearer image of what needs to be done, it can be useful to see what you have already accomplished so far.</p>
<h3>No schedule</h3>
<p>You may have a plan, but do you have a schedule?</p>
<p>Scheduling is as important as having a plan in the first place. Without a schedule, the project may easily keep going endlessly, with very little progress as nobody cares as there are no deadlines.</p>
<p>I have to admit that even now there&#8217;s a project I&#8217;m working on, without a proper schedule, which has been going on way longer than it should. I plan to fix that, though!</p>
<h3>No release strategy</h3>
<p>What&#8217;s a release strategy? This may not be the correct term, as I just made this up, but I think it&#8217;s quite descriptive: It includes how often you want to do releases and the steps involved in making a release.</p>
<p>The most basic requirement is to have a build/deployment script. A single command you, or anyone in your team, can run to completely build and deploy your application. This is important because a minor mistake can cause problems.</p>
<p>How often have you forgot to upload a file to the server when deploying? I bet the number is bigger than zero. Now think how many steps there is even in a simple deployment.. Sending files, setting up the database, maybe installing a default user&#8230;</p>
<p>How often you do releases can be important too. For example, in Scrum you develop in sprints and in the end of each sprint is a natural point to make a release with the features chosen for the sprint.</p>
<p>Tools such as <a href="http://ant.apache.org/">Ant</a> can be used to create build and deployment scripts.</p>
<h3>Developmestruction</h3>
<p>Not having a separate development and production environment is a recipe for disaster, especially if your application is being used in the real world at the same time as development is happening.</p>
<p>I&#8217;m a guilty of doing this, but luckily the applications weren&#8217;t being used by more than a handful of internal users.</p>
<p>Always perform development on separate machine(s) than the actual production deployment. This ensures the production system should stay relatively intact (unless your deployment screws up or you introduce more bugs). </p>
<p>Additionally, having a staging environment can be a good idea. This should usually be on similar hardware and software as production, as this will allow you to test-drive your code on a realistic setup before pushing it to production.</p>
<h3>Not using Test Driven Development</h3>
<p>This may not be the most critical problem, but I think it&#8217;s important nevertheless.</p>
<p>By having a good test-suite, you can ensure you don&#8217;t introduce regressions and that your code works at least as well as your tests were written.</p>
<p>For some reading on testing, check out <a href="http://codeutopia.net/blog/2009/08/17/unit-testing-essentials/">unit testing essentials</a> and <a href="http://codeutopia.net/blog/2009/08/14/how-to-make-your-code-testable/">How to make your code testable</a> in this blog.</p>
<h3>Post your tips/mistakes in the comments</h3>
<p>If you have any tips/mistakes of your own, please share them in the comments. I&#8217;m still learning too, so they would be great additions to this list.</p>

			<a href="http://codeutopia.net/blog/2010/01/28/6-programming-project-mistakes-you-should-avoid/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/01/28/6-programming-project-mistakes-you-should-avoid/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How much would you pay for good headphones?</title>
		<link>http://codeutopia.net/blog/2010/01/14/how-much-would-you-pay-for-good-headphones/</link>
		<comments>http://codeutopia.net/blog/2010/01/14/how-much-would-you-pay-for-good-headphones/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 19:09:05 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2010/01/14/how-much-would-you-pay-for-good-headphones/</guid>
		<description><![CDATA[Time for some consumer advice today. 
I&#8217;m sure many of us have often thought about getting headphones - and I&#8217;m sure many of us already have a pair - but how much is a reasonable amount to spend? And what will a higher-grade (read: more expensive) pair do that&#8217;s worth the money?
I recently decided to [...]]]></description>
			<content:encoded><![CDATA[
			<p>Time for some consumer advice today. </p>
<p>I&#8217;m sure many of us have often thought about getting headphones - and I&#8217;m sure many of us already have a pair - but how much is a reasonable amount to spend? And what will a higher-grade (read: more expensive) pair do that&#8217;s worth the money?</p>
<p>I recently decided to invest on some new headphones. The AKG K 701&#8217;s to be precise. They aren&#8217;t exactly very cheap and you&#8217;d probably want a headphone amplifier to go with them, one which I incidentally also bought. I&#8217;ve also had some more reasonably priced headphones in the past: Sennheiser HD-555&#8217;s and going down in the price range, some Koss and Philips ones.</p>
<p>My opinion is that great headphones can make listening to music much more enjoyable. However, should you go all out and buy expensive ones like the AKG, or is it smarter to purchase something else?</p>
<p><span id="more-277"></span></p>
<h3>No</h3>
<p>Don&#8217;t get me wrong here - The AKG&#8217;s are great, and I&#8217;m happy with my decision to buy them.</p>
<p>However, I would not recommend them for everyone, because <em>cheaper headphones are better value for most people.</em></p>
<p>I am by no means an audiophile or a hi-fi aficionado - in fact, if you ask me about ohms, impedance or such, you may be met with a blank stare - but I do enjoy the beeps and boops coming from the cans on my ears sounding nice.</p>
<p>So what do I base on my recommendation that everyone shouldn&#8217;t get the AKGs?</p>
<h3>The sound quality difference in high-end headphones is less obvious</h3>
<p>The Koss and Philips headphones I have previously owned were quite reasonably priced - probably around 50 USD or so.</p>
<p>What did they sound like?</p>
<ul>
<li><strong>Koss</strong>: Okay but the bass was totally overbearing. Most likely because they were a closed headphone.</li>
<li><strong>Philips</strong>: Okay, less bass than Koss so overall I liked these more. Open type headphones.</li>
</ul>
<p>The Philips headphones eventually broke. They had this auto-adjusting strap thing, which eventually just snapped and applying overly generous amounts of duct-tape did not help much.</p>
<p>Next, I got the Sennheiser HD-555&#8217;s. They are on the level many people may consider &#8220;too much&#8221;: I paid about 150 USD for mine.</p>
<p>What do they know? I loved the HD-555&#8217;s - they were <em>so much better</em> than the cheaper ones I had had before!</p>
<p>Actually, the difference between them and the cheaper ones was rather large. Clean sound, good bass but not too much&#8230; and very comfortable to wear for hours. </p>
<h3>The really hi-fi stuff</h3>
<p>After years of use I wanted to get something even better than the HD-555&#8217;s.</p>
<p>I decide to buy the AKG K 701&#8217;s after listening to them in a local hifi shop. I paid 340 USD for mine, which is quite a lot. Add to that a specialized headphone amplified at 160 USD.</p>
<p>The AKG&#8217;s have much cleaner sound than the Sennheiser&#8217;s. They have a wider soundstage, which is apparently a term meaning they can make the sound seem like it&#8217;s coming from farther away than it actually does (or something like that), and in general they have much more accurate response. For example, guitars and cymbals often have reverb or echo that wasn&#8217;t there with the cheaper phones.</p>
<p>When going from Philips to Sennheiser, I paid about <strong>100 USD more</strong> for the whole deal.</p>
<p>When going from Sennheiser to AKG, I paid about <strong>400 USD more</strong>.</p>
<p>You would expect the difference to be something like 4 times better, right?</p>
<h3>The truth and the bottom line</h3>
<p>The truth is that the difference is not 4x.</p>
<p>The difference is there, but it&#8217;s <em>much less noticeable than when I just added 100</em>.</p>
<p>In my experience, paying around 100 - 150 USD will get you a pair of headphones that are <em>very good</em>. They aren&#8217;t as great as paying even more, but I think at this point the differences between price start to be larger, and the differences between sound start to be smaller.</p>
<p>If you have been wondering how much is enough for headphones, I say go for the range of 100 to 150 USD. Of course, <em>go and test listen the headphones</em> before you buy. If you already have something at that range, I suggest caution unless you don&#8217;t mind spending more money on smaller enhancements.</p>
<p>Share your thoughts on this in the comments!</p>

			<a href="http://codeutopia.net/blog/2010/01/14/how-much-would-you-pay-for-good-headphones/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/01/14/how-much-would-you-pay-for-good-headphones/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rendering graphics in JavaScript games</title>
		<link>http://codeutopia.net/blog/2010/01/07/rendering-graphics-in-javascript-games/</link>
		<comments>http://codeutopia.net/blog/2010/01/07/rendering-graphics-in-javascript-games/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 12:49:42 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[general]]></category>

		<category><![CDATA[Games]]></category>

		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2010/01/07/rendering-graphics-in-javascript-games/</guid>
		<description><![CDATA[As I&#8217;ve been rewriting TankWar, I&#8217;ve been thinking of various approaches to displaying the game&#8217;s graphics. At the moment, I can count three feasible approaches for rendering game graphics:

DHTML - In other words, using divs with images and moving them around
Canvas
A hybrid: Canvas with DHTML

I&#8217;ve considered each of these, and they all have some pros [...]]]></description>
			<content:encoded><![CDATA[
			<p>As I&#8217;ve been rewriting TankWar, I&#8217;ve been thinking of various approaches to displaying the game&#8217;s graphics. At the moment, I can count three feasible approaches for rendering game graphics:</p>
<ul>
<li>DHTML - In other words, using divs with images and moving them around</li>
<li>Canvas</li>
<li>A hybrid: Canvas with DHTML</li>
</ul>
<p>I&#8217;ve considered each of these, and they all have some pros and cons which we&#8217;ll look at next.</p>
<p><span id="more-276"></span></p>
<h3>DHTML</h3>
<p>This is the most compatible of the three: Even Internet Explorer supports creating divs and moving them around on the fly.</p>
<p>This approach is quite simple too. The APIs used are well-known and usually quite problem-free when used correctly.</p>
<p>Moving divs etc. around tends to be pretty fast too, so even if you have a big huge animated explosion or something, the performance should still be relatively good. Compared to canvas this is DHTML&#8217;s biggest win.</p>
<p>One of the downsides with DHTML is sometimes jerky movements: You always need to draw and move things pixel-precise, as you can&#8217;t tell a div to be exactly 15.5 pixels from something. Canvas can draw &#8220;between&#8221; pixels, as it automatically aliases the graphics to look more like it.</p>
<p>With DHTML you also can&#8217;t draw shapes like you can with canvas, so you&#8217;ll need to come up with the graphics in an image editor beforehand.</p>
<p>With CSS transitions and other tricks, you can bend DHTML surprisingly far. However if using transitions or other advanced features, you sacrifice compatibility with more browsers than you would if you were just using canvas.</p>
<h3>Canvas</h3>
<p>Canvas is great. I wish I could leave it at that.</p>
<p>However, in addition to being generally easy to use after getting familiar with the basic API, there is one significant downside: Canvas is slow. Slow slow slow slow slow.</p>
<p>Unless you apply considerable amount of thought into the code, getting stuff drawn at reasonable speeds with canvas is difficult. It&#8217;s like going back to the times when computers had 66 mhz CPUs.</p>
<p>Canvas does have a lot of good things though: You can draw on it directly, with direct pixel manipulation, a vector-like API, or images.. or compose a new image using another canvas.. It is certainly very flexible, and it makes it very simple to do complex and dynamic shapes that would be much trickier otherwise.</p>
<p>If you&#8217;ve been following the <a href="http://codeutopia.net/projects/tankwar-2.0/">TankWar 2.0 development</a>, you may have noticed it does draw some okay looking graphics but it still is somewhat reasonable quick. It only uses canvas for rendering, so yes, it is indeed doable, but it requires tricks.</p>
<p>I&#8217;ll be discussing the finer details of how to make canvas rendering faster in another post.</p>
<h3>Hybrid</h3>
<p>A hybrid graphics engine is exactly what it sounds like: It combines both DHTML and canvas to overcome the downsides of both approaches.</p>
<p>While this may sound like the stone to drop two birds with, it may not be.</p>
<p>One of the good things about this approach is easily seen when you need to display large graphics that move. Canvas can be very slow in cases where you update larger areas of it, so in a case like this using a div for the big graphic is good. Then, you can use canvas for the rest, to create different shapes etc. which would be tricky with divs.</p>
<p>The main downside of this approach is complexity. Since you need to deal with both canvas and DOM, it&#8217;ll become twice as complex as using either of the approaches alone.</p>
<h3>In closing</h3>
<p>The approach I&#8217;ve chosen for TankWar 2.0 is pure canvas. I may need to resort to a hybrid approach if the performance drops too low, but right now it&#8217;s still tolerable at least on a moderately quick PC.</p>
<p>In the past, I&#8217;ve used canvas on most of my games: TankWar, <a href="http://codeutopia.net/blog/2009/07/14/opera-command-javascript-based-missile-command-game/">Opera Command</a>, <a href="http://widgets.opera.com/widget/5681/">WidgetRacer</a>, Asteroids&#8230; but I&#8217;ve also used a DHTML-like approach in a Tetris game and in <a href="http://codeutopia.net/blog/2009/04/28/i-wrote-a-sim-city-clone-in-javascript/">WidgetCity</a>. In these cases the DHTML-based approach was simpler or had other reasons, such as being much faster in case of WidgetCity (<a href="http://codeutopia.net/blog/2009/06/15/how-widgetcity-does-a-tile-based-map-using-just-css/">My post about the table/CSS rendering in WidgetCity</a>).</p>
<p>I&#8217;ll be discussing the details of how the new TankWar 2.0 canvas engine works in a later post. I&#8217;ve had to do a significant amount of performance trickery for it too.</p>

			<a href="http://codeutopia.net/blog/2010/01/07/rendering-graphics-in-javascript-games/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2010/01/07/rendering-graphics-in-javascript-games/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Best of 2009</title>
		<link>http://codeutopia.net/blog/2009/12/29/best-of-2009/</link>
		<comments>http://codeutopia.net/blog/2009/12/29/best-of-2009/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 18:34:51 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2009/12/29/best-of-2009/</guid>
		<description><![CDATA[In the tradition of end of the year posts, here&#8217;s some &#8220;best of&#8221; statistics from this blog for the year 2009.
Next week we will return to our regular programming. 

Most popular posts
The top 5 most popular posts for this year are:

Zend_Acl part 1: Misconceptions and Simple ACLs
Zend_Acl part 3: Creating and storing dynamic ACLs
Doctrine vs [...]]]></description>
			<content:encoded><![CDATA[
			<p>In the tradition of end of the year posts, here&#8217;s some &#8220;best of&#8221; statistics from this blog for the year 2009.</p>
<p>Next week we will return to our regular programming. </p>
<p><span id="more-275"></span></p>
<h3>Most popular posts</h3>
<p>The top 5 most popular posts for this year are:</p>
<ol>
<li><a href="http://codeutopia.net/blog/2009/02/06/zend_acl-part-1-misconceptions-and-simple-acls/">Zend_Acl part 1: Misconceptions and Simple ACLs</a></li>
<li><a href="http://codeutopia.net/blog/2009/02/18/zend_acl-part-3-creating-and-storing-dynamic-acls/">Zend_Acl part 3: Creating and storing dynamic ACLs</a></li>
<li><a href="http://codeutopia.net/blog/2008/03/02/doctrine-vs-propel/">Doctrine vs Propel</a></li>
<li><a href="http://codeutopia.net/blog/2009/03/02/handling-errors-in-zend-framework/">Handling errors in Zend Framework</a></li>
<li><a href="http://codeutopia.net/blog/2008/08/07/zend_form-decorator-tips/">Zend_Form decorator tips</a></li>
</ol>
<p>Doctrine vs Propel seems very popular, even though it&#8217;s getting slightly out-dated. It was in <a href="http://codeutopia.net/blog/2008/12/30/best-of-2008/">2008&#8217;s top 5</a> as well.</p>
<h3>Miscelaneous</h3>
<p>According to Google Analytics, this blog had 232,805 visits and 335,823 pageviews this year. That&#8217;s more than twice as much as last year (81,027 visits, 119,024 pageviews), so I&#8217;m quite happy about the figure!</p>
<p>I might go as far as claim that my blog is one of the most popular IT-related blogs written by a finnish person. Too bad there&#8217;s very little reliable data available on that, but from my empiric research I&#8217;d say it may not be completely untrue (feel free to provide statistics if you know of any) - however I have very few visitors from Finland, a mere 1% of the total.</p>

			<a href="http://codeutopia.net/blog/2009/12/29/best-of-2009/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2009/12/29/best-of-2009/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Vim / Opera tip: Open highlighted text in Vim</title>
		<link>http://codeutopia.net/blog/2009/12/22/vim-opera-tip-open-highlighted-text-in-vim/</link>
		<comments>http://codeutopia.net/blog/2009/12/22/vim-opera-tip-open-highlighted-text-in-vim/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 16:50:53 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[general]]></category>

		<category><![CDATA[Opera]]></category>

		<category><![CDATA[Vim]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2009/12/22/vim-opera-tip-open-highlighted-text-in-vim/</guid>
		<description><![CDATA[Here&#8217;s a quick tip for Opera and Vim users: How to add a menu item which allows you to quickly open highlighted text on a web page in Vim.
This trick can be applied with any other application too, like &#60;insert your favorite editor&#62;

Step 1: Create copy of the standard menu configuration
First, you need to create [...]]]></description>
			<content:encoded><![CDATA[
			<p>Here&#8217;s a quick tip for Opera and Vim users: How to add a menu item which allows you to quickly open highlighted text on a web page in Vim.</p>
<p>This trick can be applied with any other application too, like &lt;insert your favorite editor&gt;</p>
<p><span id="more-273"></span></p>
<h3>Step 1: Create copy of the standard menu configuration</h3>
<p>First, you need to create a copy of the standard menu configuration so you can go back if something goes wrong.</p>
<p>This can be achieved by going to preferences (Ctrl+F12), Advanced, Toolbars. Now choose &#8220;Opera Standard&#8221; from under &#8220;Menu Setup&#8221;, hit Duplicate and then click on the newly duplicated setup. Hit OK and you&#8217;re done with this step</p>
<p><img src="http://codeutopia.net/blog-images/opera-menuconfig.PNG" alt="" /></p>
<h3>Step 2: Edit menu configuration</h3>
<p>Now, you need to find the menu configuration file. It&#8217;s located in the directory where Opera stores your profile - typically in Documents and Settings, but the easiest way to find it is by going to <a href="opera:about">opera:about</a>.</p>
<p>The path to your profile is displayed under &#8220;Paths&#8221;, as shown below:</p>
<p><img src="http://codeutopia.net/blog-images/opera-profiledir.PNG" alt="" /></p>
<p>In my case the profile directory is the one highlighted in the image. Find yours, and you should see a directory called <i>Menu</i> under it.</p>
<p>In the menu dir, you should have a file called <i>standard_menu (1).ini</i>. Open it and find the line which says <b>Hotclick Popup Menu</b></p>
<p>Under it, you should see lines such as &#8220;Item, M_COPY_TO_NOTE=Copy to note&#8221;</p>
<p>To add a new Copy to Vim item, insert the following line:</p>
<p><q>Item, Copy to Vim = Go to page, &#8220;data:text/open-in-vim,%t&#8221;</q></p>
<p>Here we are essentially adding a new command to go to a specific URL. In this case, it&#8217;s a <a href="http://en.wikipedia.org/wiki/Data_URI">Data URI</a>, using mime-type <i>text/open-in-vim</i>, which is something we made up, and then including the text highlighted with the identifier <i>%t</i></p>
<h3>Step 3: Add new mime-type handler</h3>
<p>Now that we have modified the menu configuration, the last thing we need to do is add a handler for our own <i>text/open-in-vim</i> mime-type.</p>
<p>Go back to Opera&#8217;s preferences, but this time choose Advanced, Downloads.</p>
<p>Click on Add&#8230; and fill in the dialog:</p>
<p>Mime-Type: text/open-in-vim<br />
Extension: openinvim (this can be anything you want actually)<br />
Action: Open in Other Application, fill in path to vim&#8217;s executable.</p>
<p><img src="http://codeutopia.net/blog-images/opera-openinvim.PNG" alt="" /></p>
<h3>Done!</h3>
<p>If you didn&#8217;t restart Opera yet, do that now. You should now get a new Copy to Vim menu option whenever you highlight text and right click.</p>

			<a href="http://codeutopia.net/blog/2009/12/22/vim-opera-tip-open-highlighted-text-in-vim/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2009/12/22/vim-opera-tip-open-highlighted-text-in-vim/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rewriting TankWar: Assessing the damage</title>
		<link>http://codeutopia.net/blog/2009/12/16/rewriting-tankwar-assessing-the-damage/</link>
		<comments>http://codeutopia.net/blog/2009/12/16/rewriting-tankwar-assessing-the-damage/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 17:22:14 +0000</pubDate>
		<dc:creator>Jani Hartikainen</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Games]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[TankWar]]></category>

		<guid isPermaLink="false">http://codeutopia.net/blog/2009/12/16/rewriting-tankwar-assessing-the-damage/</guid>
		<description><![CDATA[I have decided to rewrite TankWar, and I&#8217;ll be writing a bunch of blog posts about the process. If you ever wanted to know how to write a scorched earth/worms-like game using just JavaScript, now is your chance to learn  
This is the first one: Assessing the damage. In other words, take a look [...]]]></description>
			<content:encoded><![CDATA[
			<p>I have decided to rewrite <a href="http://codeutopia.net/projects/tankwar/">TankWar</a>, and I&#8217;ll be writing a bunch of blog posts about the process. If you ever wanted to know how to write a scorched earth/worms-like game using just JavaScript, now is your chance to learn <img src='http://codeutopia.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>This is the first one: Assessing the damage. In other words, take a look at the old code and see what can be salvaged. I&#8217;ll also list some issues with the current implementation, and how I plan on solving them in the new version.</p>
<p><span id="more-272"></span></p>
<h3>Issues in the current implementation</h3>
<p>I wrote the most of the code in the current version of TankWar somewhere in 2006. Let&#8217;s be frank: The codebase <em>sucks</em>.</p>
<p>It&#8217;s a mess.</p>
<p>I&#8217;m surprised some of it ever worked. At all.</p>
<p>My coworker <a href="http://daniliants.com">Artem</a> tells me recognizing your old code is crap is a good sign: It means you have improved - and I agree with him.</p>
<p>In addition to being pretty poor code, the current version does have some bugs too:</p>
<ol>
<li>If your bullet moves very fast, it can go through the ground.</li>
<li>The graphics system is poorly implemented which can be seen especially when using the bomber weapon - it can get pretty glitchy</li>
<li>The bullet trajectories deviate wildly depending on the computer&#8217;s speed</li>
</ol>
<p>While some of these issues could be patched into the current code, it would just make the code even more fragile.</p>
<h3>Damage control</h3>
<p>In most cases there&#8217;s some useful code even if most of the code is messy. </p>
<p>In the case of TankWar, the main implementation of the game engine, menus, etc. will be thrown away. They make most of the code, but they are also the worst parts of it.</p>
<p>The good parts of TankWar include the terrain generator, some vector math classes and some utility classes. </p>
<p>It seems we can apply the statistic that simpler code contain less problems here too.</p>
<h3>Solutions to the current problems</h3>
<p>First and foremost, the codebase will need to be organized better. Now it&#8217;s just a jumble of code and HTML, CSS and whatnot mixed together like <a href="http://hijinksensue.com/2009/11/27/the-special-sauce/">magic hobo gravy</a>.</p>
<p>The game engine itself will need to be more robust and be able to handle fast moving bullets properly, and the trajectories need to be correct.</p>
<h4>Bullet collision solution</h4>
<p>The solution to fast moving bullets going through stuff is improving the collision detection mechanism. Currently the implementation is quite simple: It simply moves the bullet each cycle and checks if the bullet intersects the ground or other objects. This is very easy to implement, as the <code>canvas</code> element has a function for it, but it&#8217;s not very accurate with fast moving objects.</p>
<p>A better approach with fast moving objects is using ray intersection detection. Put simply, this means we will check all the points in the trajectory, including the points between the positions the bullet actually is in. In the old implementation, depending on the speed, there could be tens of pixels of space which was never checked for hits.</p>
<h4>Glitchy graphics</h4>
<p>The solution to the issues with the graphics system is to make it separate from the other stuff. The current graphics engine was just quickly slapped on top of the physics engine to make it display pretty pictures. This is more of an architectural problem than an algorithmic issue as the collision detection.</p>
<p>Additionally, the graphics system may be a little problematic in general. While it works with the current simplistic graphics, I&#8217;m not sure whether it would function very well if I wanted to include more complex graphics.</p>
<h4>Bullet trajectory problems</h4>
<p>For the third problem - framerate dependent trajectories - the solution may be more problematic. The current version already attempts to fix this by using time-based movement. However, it does not quite work, meaning I&#8217;ve either implemented it wrong or the timer is not accurate enough.</p>
<p>If the timer turns out to be inaccurate, the solution may be to precalculate the trajectory using a predefined framerate. This means we would have to first calculate the trajectory, and only after it&#8217;s been calculated, we could render what happened. This might be difficult, because calculating the trajectory may take so long that the user would notice a significant delay between &#8220;shooting&#8221; and the game actually performing the shot.</p>
<p>Then again, the frame rate calculator was (as far as I can remember) also slapped on top of the existing stuff and never really properly planned. The timer itself works quite similar to <a href="http://codeutopia.net/blog/2009/08/21/using-canvas-to-do-bitmap-sprite-animation-in-javascript/">the timer in my JavaScript bitmap sprite animations post</a>.</p>
<h3>In closing</h3>
<p>The rewrite will probably take a while, depending on how much time I&#8217;ll have to work on it. I will probably put together some more posts on the topic as I have some progress. Let me know if there&#8217;s anything specific about it you&#8217;d like to hear.</p>
<p>Naturally the new version will generally be more awesome than the old version - it would be rather pointless to just rewrite it and add nothing else than better code.</p>
<p><b>Further reading:</b><br />
<a href="http://codeutopia.net/blog/2010/01/07/rendering-graphics-in-javascript-games/">Rendering graphics in JavaScript games</a></p>

			<a href="http://codeutopia.net/blog/2009/12/16/rewriting-tankwar-assessing-the-damage/#comments">Post a comment</a>
		]]></content:encoded>
			<wfw:commentRss>http://codeutopia.net/blog/2009/12/16/rewriting-tankwar-assessing-the-damage/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.761 seconds -->
<!-- Cached page generated by WP-Super-Cache on 2010-03-16 07:34:00 -->
