<?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>Andrew R. Jones &#187; Development</title>
	<atom:link href="http://andrew-jones.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrew-jones.com</link>
	<description>Andrew Jones' Site</description>
	<lastBuildDate>Tue, 01 Dec 2009 20:30:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using a Quarter Interval with the Simile Timeline</title>
		<link>http://andrew-jones.com/2009/12/01/using-a-quarter-interval-with-the-simile-timeline/</link>
		<comments>http://andrew-jones.com/2009/12/01/using-a-quarter-interval-with-the-simile-timeline/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 20:19:20 +0000</pubDate>
		<dc:creator>Andrew Jones</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Simile Timeline]]></category>

		<guid isPermaLink="false">http://andrew-jones.com/?p=434</guid>
		<description><![CDATA[The Simile Timeline allows you to make beautiful, interactive timelines for web pages using only JavaScript. It is fairly flexible and allows you to create timelines with intervals of Seconds, Hours and Days right up to Centuries and Millenniums.
However, it does not support Quarters, which is what I needed. So I have submitted a patch [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.simile-widgets.org/timeline/">Simile Timeline</a> allows you to make beautiful, interactive timelines for web pages using only JavaScript. It is fairly flexible and allows you to create timelines with intervals of Seconds, Hours and Days right up to Centuries and Millenniums.</p>
<p>However, it does not support Quarters, which is what I needed. So I have submitted a <a href="http://code.google.com/p/simile-widgets/issues/detail?id=337">patch</a> to the project, and have also been using the following code so I can use Quarters now without needing to patch my local install.</p>
<p>The code is not the most elegant, as it duplicates a lot of the code from the library, but until my patch gets incorporated it is the only way to have Quarters.</p>
<p>To use it, download the js file and include it in your web page after the timeline files. You can then use a Quarter in the same way as you would any interval, as shown below:</p>
<pre>
Timeline.createBandInfo({
    ...
    intervalUnit:   Timeline.DateTime.QUARTER,
    ...
});
</pre>
<p>Find the code on <a href="http://gist.github.com/246389">GitHub</a>, or <a style="display:none;" id="ddetlink854918825" href="javascript:expand(document.getElementById('ddet854918825'))">expand it below:</a>
<div class="ddet_div" id="ddet854918825"><script language="JavaScript" type="text/javascript">expand(document.getElementById('ddet854918825'));expand(document.getElementById('ddetlink854918825'))</script>
<script src="http://gist.github.com/246389.js"></script><br />
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://andrew-jones.com/2009/12/01/using-a-quarter-interval-with-the-simile-timeline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coding Standards</title>
		<link>http://andrew-jones.com/2009/06/08/coding-standards/</link>
		<comments>http://andrew-jones.com/2009/06/08/coding-standards/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 07:00:51 +0000</pubDate>
		<dc:creator>Andrew Jones</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Code Quality]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Coding Standards]]></category>

		<guid isPermaLink="false">http://andrew-jones.com/?p=267</guid>
		<description><![CDATA[

What are Coding Standards?
Why do we need Coding Standards?
Discussion of Specific Standards

Spaces Around Parenthesis
The Location of Braces
Indenting
Tabs vs Spaces
Comments
Function Headers
Naming Conventions


Writing the Coding Standards
Publishing and Agreeing the Coding Standards
Conclusion
Links


Having been working in a team on a large-ish project I quickly found out that we all have very different coding styles. This has provided constant headaches [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-what-are-coding-standards">What are Coding Standards?</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-why-do-we-need-coding-standards">Why do we need Coding Standards?</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-discussion-of-specific-standards">Discussion of Specific Standards</a>
<ol>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-spaces-around-parenthesis">Spaces Around Parenthesis</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-the-location-of-braces">The Location of Braces</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-indenting">Indenting</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-tabs-vs-spaces">Tabs vs Spaces</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-comments">Comments</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-function-headers">Function Headers</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-naming-conventions">Naming Conventions</a></li>
</ol>
</li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-writing-the-coding-standards">Writing the Coding Standards</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-publishing-and-agreeing-the-coding-standards">Publishing and Agreeing the Coding Standards</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-conclusion">Conclusion</a></li>
<li><a href="http://andrew-jones.com/2009/06/08/coding-standards/#toc-links">Links</a></li>
</ol>
</div>
<p>Having been working in a team on a large-ish project I quickly found out that we all have very different coding styles. This has provided constant headaches and frustrations throughout the project as we try to work on the same files, re-factoring each others code so it fits in with our preferred styles. Not only is this a waste of time, but it can also make it hard when using subversion to merge changes to the files, as it often thinks the file has significantly changed when really it was only the amount of white-space in the indentation.</p>
<p>In an effort to overcome these problems I have looked into creating some coding standards for my group at work. This article discusses my research into coding standards, including what benefits they might bring and what they should specify.</p>
<h3 id="toc-what-are-coding-standards">What are Coding Standards?</h3>
<p>Coding standards (also <em>coding conventions</em> or <em>coding guidelines</em>) could be defined as follows:</p>
<blockquote><p>Coding standards are a set of guidelines that recommend programming style, practices and methods for each aspect of program. They aim to help improve the readability of the source code and make software maintenance easier. </p></blockquote>
<h3 id="toc-why-do-we-need-coding-standards">Why do we need Coding Standards?</h3>
<p>As defined above, coding standards are designed to help improve the readability and maintainability of your source code. This is important because:</p>
<ul>
<li>80% of the lifetime cost of a piece of software goes to maintenance</li>
<li>Hardly any software is maintained for its whole life by the original author<sup>[1]</sup></li>
</ul>
<p>Coding standards help to enforce consistency over different parts of the software when there are a team of developers working together and help make the code more predictable.</p>
<p>Finally it can be difficult to produce and/or read diffs and patches against source code if it does not adhere to any standards, in particular when it comes to white space and indentation.<em></em></p>
<h3 id="toc-discussion-of-specific-standards">Discussion of Specific Standards</h3>
<p>I will now look at some of the specific coding standards that are often defined in guidelines and discuss whether what benefits they bring and how appropriate they are. They will form the decision of what I include in my first version of the coding standards for my team. This is not a complete list but should capture the main sections of a coding standards document.</p>
<h4 id="toc-spaces-around-parenthesis">Spaces Around Parenthesis</h4>
<p>Everyone has their own style when it comes to having spaces around parenthesis. Some examples are:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> foo <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> foo <span style="color: #009900;">&#41;</span></pre></div></div>

<p>Some coding standards define where the white space is acceptable. However, I do not think it makes a significant difference to the readability of the code and so will not be specifying a standard.</p>
<h4 id="toc-the-location-of-braces">The Location of Braces</h4>
<p>As with the spaces around the parenthesis, everyone will have their own style when it comes to the location of the braces. Some editors will also be configured to place the braces in a particular location. Below are some examples of braces placement<sup>[2]</sup>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>condition<span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
    doSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I don&#8217;t really think the location of braces matter a great matter a great deal, so long as they are sensible and the indenting is consistent. It is not something that is worth defining a standard for, especially at the risk of upsetting people who have their own ideas on the right place to put them.</p>
<h4 id="toc-indenting">Indenting</h4>
<p>One thing that nearly everyone does agree on is the need to indent your code. This makes the code so much more readable as you can easily see where one code block starts and finishes and if there are nested blocks.</p>
<p>Although it is agreed that indenting is a good thing, there are arguments about how many columns to use for indenting. Most specify between two or four columns, although it really does not matter much as long as it is consistent.</p>
<p>In the coding standards it might be worth defining the amount of columns to use for indenting and a reminder that it should always be used where appropriate.</p>
<h4 id="toc-tabs-vs-spaces">Tabs vs Spaces</h4>
<p>The issue of using tabs to create white space seems to be a contentious one. The problem is the tool you are using might be configured to use tab stops of four, where another persons might be configured to use tab stops of eight, and yet another person might use spaces. So you might have spent some time formatting your code to look nice in your editor, but when the next person opens the file it might look very ugly and difficult to read.</p>
<p>To avoid this issue a lot of coding standards forbid the use of tabs and only allow the use of spaces to create white space. Using only spaces ensures that the code will look the same in all editors and will also avoid problems when printing or emailing code and producing/reading diffs. It should be possible to configure your editor to use spaces instead of tabs (often under the setting of &#8220;soft tabs&#8221;).</p>
<p>I have certainly found the issue of tabs and spaces to be very annoying and so would suggest that there should be a guideline defined in the coding standards.</p>
<h4 id="toc-comments">Comments</h4>
<p>As with indenting, it is generally agreed that commenting your code is a good idea. Comments help to explain what the code is doing, which can be especially useful if it is a complex piece of code. They can also be used to highlight where some code needs later attention, using comments that start with <strong>TODO</strong>, <strong>FIXME</strong> or <strong>SMELL</strong><sup>[3]</sup>.</p>
<p>There is a question on how many comments there should be. Some people think you can never have too much of a good thing and therefore your code should be littered with comments. Others might say that if you need a comment then the code isn&#8217;t sufficiently simple enough<sup>[4]</sup>.</p>
<p>I don&#8217;t think its worth specifying how many comments people should be writing in the coding conventions. However, it might be a good idea to include a reminder that ensures everyone is using comments where appropriate.</p>
<h4 id="toc-function-headers">Function Headers</h4>
<p>Function headers use comments before a function is defined that documents what it does and possibly more. A function header might be as simple as the following<sup>[5]</sup>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> <span style="color: #006600; font-style: italic;">// This function trims right and left of the string</span>
 <span style="color: #003366; font-weight: bold;">function</span> trim<span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span></pre></div></div>

<p>Or it could be a bit more verbose:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"> <span style="color: #006600; font-style: italic;">// -----------------</span>
 <span style="color: #006600; font-style: italic;">// function trim</span>
 <span style="color: #006600; font-style: italic;">// input: theString = string</span>
 <span style="color: #006600; font-style: italic;">// returns: middle portion of theString, using the Wiki2001StringAlgorithmNotByKnuth</span>
 <span style="color: #006600; font-style: italic;">// assumptions: theString must be at least 3 characters long (plus terminating null)</span>
 <span style="color: #006600; font-style: italic;">//  to avoid buffer overflow error.  NotKnuthStringLibrary must be opened &amp; initialized </span>
 <span style="color: #006600; font-style: italic;">//  before calling this function.</span>
 <span style="color: #006600; font-style: italic;">// ------------------</span>
 <span style="color: #003366; font-weight: bold;">function</span> trim<span style="color: #009900;">&#40;</span>theString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></div></div>

<p>Like comments, there are mixed feelings about the usefulness of function headers. While they can help to give you an overview of what the function does, it could also be argued that if the code is clear enough then you should be able to work out what it does.</p>
<p>Many languages have their own format for documenting functions in the code, such as Perl&#8217;s POD<sup>[6]</sup> and Java&#8217;s Javadoc<sup>[7]</sup>. This can then be extracted and formatted for output in a number of ways, such as HTML and PDF.</p>
<p>Depending on what you are working on function headers might need to be included with the code. However for the kind of projects my group works on it should be enough to specify one or two lines before each function to describe exactly what it does.</p>
<h4 id="toc-naming-conventions">Naming Conventions</h4>
<p>A naming convention defines how variables, types and functions should be named in a program. This is another contentious issue where most developers have their idea of what is the right naming convention and will not want to be told to do it in a different way.</p>
<p>Naming conventions might need to answer a number of questions. For example, should you use bumpy case, where you use capitalization to divide each word from the next (i.e. <code>formatNumber</code>) or should you use underscores (i.e. <code>format_number</code>). They might also use the name to identify the type of macro, for example a <code>CONSTANT</code> in all capitals, a <code>Class</code> with an initial capital and an <code>instance</code> in lower case. A <code>subroutine</code> would also be lower case and would be clear from context as its usually followed by parentheses.</p>
<p>A famous variable naming convention is the Hungarian Notation and is where the type of the variable is encoded into its name. There is no standard for which characters should be used as the identifier, but an example might be <code>iSize</code> for an integer.</p>
<p>Naming conventions do sound like a good idea. However, different people will have different ideas on the right way to name a type. As long as it is consistent with the names of functions already in the program then it does not really matter how it is done. Therefore, when I wrote my first version of the coding standards for the team I work in I decided not to suggest a particular naming convention.</p>
<h3 id="toc-writing-the-coding-standards">Writing the Coding Standards</h3>
<p>It is worth considering what the coding standards document might look like and how much detail it should go in. Some coding standards are fairly short and concise, such as the <a href="http://www.bbc.co.uk/guidelines/futuremedia/technical/perl.shtml">BBC&#8217;s Perl Coding Guidelines</a>, while others are quite the opposite and provide guidelines for almost all parts of writing code, such as <a href="http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions">Adobe&#8217;s Flex SDK coding conventions</a>.</p>
<p>It is also a good idea to think about the style and layout of the document. Should it be listed as bullet points or discussed in paragraphs?</p>
<p>The coding conventions I think got the best layout are the BBC&#8217;s coding conventions for <a href="http://www.bbc.co.uk/guidelines/futuremedia/technical/cpp.shtml">C++</a> and <a href="http://www.bbc.co.uk/guidelines/futuremedia/technical/perl.shtml">Perl</a>, and I wrote mine in a similar way.</p>
<h3 id="toc-publishing-and-agreeing-the-coding-standards">Publishing and Agreeing the Coding Standards</h3>
<p>So I now have an idea of what I want to outline in the coding standards and now need to think about the best place to publish it and getting everyone&#8217;s agreement on it. At work we have a wiki, which would make an ideal home for the coding standards as it is accessible to everyone and also editable by anyone.</p>
<p>Once I have published the first version of the standards it will be important to get everyone&#8217;s feedback and discuss whether they are the best practices for our group. It will also be important to ensure that everyone feels a sense of ownership for the standards, which will make people more likely to adhere to them.</p>
<h3 id="toc-conclusion">Conclusion</h3>
<p>After completing this research I feel I have a good idea of what makes good coding standards and how much depth they should go into. I have also discussed many specific standards and tried to capture a developers thoughts on them.</p>
<p>You can view the coding standards using the link below. It turned out to be a very short document, but that&#8217;s not necessarily a bad thing. I have purposely decided not to define specific standards where we should be able to rely on the developers common sense. Also this is only a first version of the standards and they may well grow as feedback is received.</p>
<p>The success of the standards will be measured by their uptake and whether people feel like they have involvement in the document, rather than just being told what is right. In a months time I will write a follow up discussing how successful the coding standards have been in my group and whether people feel it has helped to improve the consistency and quality of the code.</p>
<p><a href="http://andrew-jones.com/wp-content/uploads/2009/06/codingstandards.pdf">View the coding standards (PDF)</a></p>
<h3 id="toc-links">Links</h3>
<p>Since I read so many articles related to coding standards it would probably be easiest to browse them on <a href="http://delicious.com/andrewrjones/coding_standards">Delicious</a>. You might also want to see the <a href="http://en.wikipedia.org/wiki/Coding_conventions">Wikipedia article</a>.</p>
<h4>References</h4><ol class="footnotes"><li id="footnote_0_267" class="footnote"><a href="http://java.sun.com/docs/codeconv/">Code Conventions for the Java Programming Language</a></li><li id="footnote_1_267" class="footnote"><a href="http://c2.com/cgi/wiki?CodingStandard">http://c2.com/cgi/wiki?CodingStandard</a></li><li id="footnote_2_267" class="footnote"><a href="http://foswiki.org/Development/CodingStandards">Foswiki Coding Conventions</a></li><li id="footnote_3_267" class="footnote"><a href="http://c2.com/cgi/wiki?MassiveFunctionHeaders">http://c2.com/cgi/wiki?MassiveFunctionHeaders</a></li><li id="footnote_4_267" class="footnote"><a href="http://c2.com/cgi/wiki?SmallFunctionHeaders">http://c2.com/cgi/wiki?SmallFunctionHeaders</a></li><li id="footnote_5_267" class="footnote"><a href="http://perldoc.perl.org/perlpod.html">Perl&#8217;s Plain Old Documentation format</a></li><li id="footnote_6_267" class="footnote"><a href="http://java.sun.com/j2se/javadoc/writingdoccomments/index.html">Java&#8217;s Javadoc format</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://andrew-jones.com/2009/06/08/coding-standards/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working With Cookies? Get Firecookie!</title>
		<link>http://andrew-jones.com/2009/05/11/working-with-cookies-get-firecookie/</link>
		<comments>http://andrew-jones.com/2009/05/11/working-with-cookies-get-firecookie/#comments</comments>
		<pubDate>Mon, 11 May 2009 08:00:22 +0000</pubDate>
		<dc:creator>Andrew Jones</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Firebug]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://andrew-jones.com/?p=196</guid>
		<description><![CDATA[I have recently been working with cookies from JavaScript and started looking for a Firefox extension to help. I knew there would be plenty[1], so I started to try a couple. Then I stumbled on Firecookie and the search was over!
Firecooke is an extension for Firebug and is written by Jan Odvarko, who is also [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been working with cookies from JavaScript and started looking for a Firefox extension to help. I knew there would be plenty<sup>[1]</sup>, so I started to try a couple. Then I stumbled on <a href="http://www.softwareishard.com/blog/firecookie/">Firecookie</a> and the search was over!</p>
<p>Firecooke is an extension for <a href="http://getfirebug.com/">Firebug</a> and is written by <a href="http://www.softwareishard.com/blog/about/">Jan Odvarko</a>, who is also a full time contributor to Firebug. Among other things it allows you to:</p>
<ul>
<li>Browse and inspect cookies</li>
<li>Add, edit or delete cookies</li>
<li>Log cookie events</li>
</ul>
<p><a href="http://www.softwareishard.com/firecookie/images/scr-overview.png"><img src="http://www.softwareishard.com/firecookie/images/scr-overview-thumb.png" alt="" /></a><br />
<em>Screenshot taken from <a href="http://www.softwareishard.com/blog/firecookie/">http://www.softwareishard.com/blog/firecookie/</a></em>.</p>
<p>I love the way it plugs into Firebug and it provides all the functionality I needed (plus a bit more). For more information about Firecookie check out its <a href="http://www.softwareishard.com/blog/firecookie/">homepage</a>.</p>
<p>Incidentally working with cookies in JavaScript is fairly easy. For a good overview take a look at the articles by PPK over at <a href="http://www.quirksmode.org/js/cookies.html">QuirksMode</a> and by <a href="http://www.nczonline.net/blog/2009/05/05/http-cookies-explained/">Nicholas C. Zakas</a>, or use a JavaScript library such as <a href="http://developer.yahoo.com/yui/cookie/">YUI</a> or <a href="http://plugins.jquery.com/project/cookies">jQuery</a>.</p>
<h4>References</h4><ol class="footnotes"><li id="footnote_0_196" class="footnote"><a href="http://mashable.com/2008/11/11/firefox-extensions-for-managing-cookies/">http://mashable.com/2008/11/11/firefox-extensions-for-managing-cookies/</a></li></ol>]]></content:encoded>
			<wfw:commentRss>http://andrew-jones.com/2009/05/11/working-with-cookies-get-firecookie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copying Objects in JavaScript</title>
		<link>http://andrew-jones.com/2009/04/20/copying-objects-in-javascript/</link>
		<comments>http://andrew-jones.com/2009/04/20/copying-objects-in-javascript/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 08:00:40 +0000</pubDate>
		<dc:creator>Andrew Jones</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://andrew-jones.com/?p=156</guid>
		<description><![CDATA[The other day I got tripped up by the way JavaScript assigns variables. In many languages, assigning a variable to another variable creates a copy of the existing variable. If you wanted to make the new variable a reference to the existing variable, you would have to explicitly define that. For example, in Perl you [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I got tripped up by the way JavaScript assigns variables. In many languages, assigning a variable to another variable creates a copy of the existing variable. If you wanted to make the new variable a reference to the existing variable, you would have to explicitly define that. For example, in Perl you can do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!perl</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@array</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@newArray</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@array</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># creates a copy of the @array</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$arrayRef</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@array</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># creates a reference to @array</span></pre></div></div>

<p>In JavaScript things are a bit different. While assigning a boolean or string to a variable does make a copy of that value, assigning an array or an object to a variable actually makes a <em>reference</em> to the value. If your not careful this can easily catch you out, as you might expect to be able to change the new variable without affecting the old object.</p>
<p>Strangely, nothing exists in JavaScript that will help you clone an object. However, other people have came across this and kindly shared their solutions. The code I am now using was submitted by <a href="http://extjs.com/forum/member.php?s=4d7da2856da10dcbee8b5b4022fa3b1b&amp;u=2178">Jozef Sakalos</a> over at the <a href="http://extjs.com/forum/showthread.php?t=26644">ExtJS forums</a> and is displayed below in a more neutral namespace.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> cloneObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>o <span style="color: #339933;">||</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">!==</span> <span style="color: #000066; font-weight: bold;">typeof</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> o<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #3366CC;">'[object Array]'</span> <span style="color: #339933;">===</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> p<span style="color: #339933;">,</span> v<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>p <span style="color: #000066; font-weight: bold;">in</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">hasOwnProperty</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            v <span style="color: #339933;">=</span> o<span style="color: #009900;">&#91;</span>p<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>v <span style="color: #339933;">&amp;&amp;</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">===</span> <span style="color: #000066; font-weight: bold;">typeof</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c<span style="color: #009900;">&#91;</span>p<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> cloneObject<span style="color: #009900;">&#40;</span>v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                c<span style="color: #009900;">&#91;</span>p<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> v<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> c<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So to copy an object you can now do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> array <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> arrayRef <span style="color: #339933;">=</span> array<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// reference of the array</span>
<span style="color: #003366; font-weight: bold;">var</span> newArray <span style="color: #339933;">=</span> cloneObject<span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// copy of the array</span></pre></div></div>

<h4 id="toc-links">Links</h4>
<ul>
<li><a href="http://my.opera.com/GreyWyvern/blog/show.dml/1725165">Article from Opera</a></li>
<li><a href="http://extjs.com/forum/showthread.php?t=26644">ExtJS Forum Post</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://andrew-jones.com/2009/04/20/copying-objects-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Removing Referral URL Using a Perl Proxy Script</title>
		<link>http://andrew-jones.com/2009/02/03/removing-referral-url-using-a-perl-proxy-script/</link>
		<comments>http://andrew-jones.com/2009/02/03/removing-referral-url-using-a-perl-proxy-script/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 21:57:03 +0000</pubDate>
		<dc:creator>Andrew Jones</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://andrew-jones.com/?p=133</guid>
		<description><![CDATA[When you follow links on the internet, your browser will send a referral URL to the server to let it know where it has came from. This can often be useful to the server administrators, as you can use these to analyse how visitors are finding your site or a particular page.
However, there can also [...]]]></description>
			<content:encoded><![CDATA[<p>When you follow links on the internet, your browser will send a referral URL to the server to let it know where it has came from. This can often be useful to the server administrators, as you can use these to analyse how visitors are finding your site or a particular page.</p>
<p>However, there can also be times when the user does not to share this information. Some users might say that it is an invasion of privacy. You might also be coming from a corporate intranet, and are worried about sharing internal URLs with outside servers.</p>
<p>Luckily it is very easy to spoof or remove the referral URL. Some web browsers, such as Opera, allow the user to disable referrer logging<sup>[1]</sup>, while it can also be removed by the firewall or the anti-virus software. This method uses a small proxy script to trick the browser into thinking that there was no referral URL, as if a user has entered the URL straight into their browsers location bar.</p>
<h4 id="toc-the-script">The Script</h4>
<p>The script itself is extremely simple and is shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!perl</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOT;
Content-type: text/htmln
&lt;html&gt;&lt;head&gt;
&lt;meta http-equiv=&quot;refresh&quot; content=&quot;0; URL=$ENV{QUERY_STRING}&quot;&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
window.location=&quot;$ENV{QUERY_STRING}&quot;;
&lt;/script&gt;
&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;
EOT</span></pre></div></div>

<p>The variable <code>$ENV{QUERY_STRING}</code> contains the part of the link that comes after the first question mark. So for <code>http://andrew-jones.com?http://wikipedia.org</code> it would contain <code>http://wikipedia.org</code>. It does not do anything with the string, so any ampersands or further question marks are left as they are.</p>
<p>The script has two was of redirecting the user. The first uses the <code>meta</code> tag. This tells the browser to refresh instantly (0 seconds) and load the URL that is in <code>$ENV{QUERY_STRING}</code>. As the browser thinks it has just reloaded, it does not send the referrer information with the request.</p>
<p>As not all browsers understand the <code>meta</code> tag, we have also used some simple JavaScript to do the redirect. As you could probably guess, the JavaScript causes the browser to redirect to the location that we have set. This will probably not be used, as most browsers do understand the <code>meta</code> tag.</p>
<p>As a last resort we could also have made a HTML link that the user can click on if the browser will not redirect them, but this will cause the URL of the script to be used as the referrer URL, which may not be desirable.</p>
<h4 id="toc-summary">Summary</h4>
<p>So this very simple proxy script will take any URL that&#8217;s passed in and redirect the user without the browser passing a referrer URL, for whatever reason you might have. It also works with links that may contain parameters, such as <code>http://www.google.co.uk/search?q=site:wikipedia.org+http</code>. Hope you found it useful.</p>
<h4 id="toc-links">Links</h4>
<ul>
<li><a title="Wikipedia Article" href="http://en.wikipedia.org/wiki/Referer">Wikipedia Article</a></li>
<li style="text-align: left;"><a title="Article on Nation Master" href="http://www.nationmaster.com/encyclopedia/Referer">Article on NationMaster</a></li>
</ul>
<h4>References</h4><ol class="footnotes"><li id="footnote_0_133" class="footnote">http://www.opera.com/support/kb/view/93/</li></ol>]]></content:encoded>
			<wfw:commentRss>http://andrew-jones.com/2009/02/03/removing-referral-url-using-a-perl-proxy-script/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
