<?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>Daniel15&#039;s Blog &#187; Programming</title>
	<atom:link href="http://dan.cx/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.cx/blog</link>
	<description>Blog of Daniel, a slightly awesome 19-year-old web developer from Melbourne, Australia</description>
	<lastBuildDate>Sun, 15 Aug 2010 02:22:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>My favourite web development blogs</title>
		<link>http://dan.cx/blog/2010/04/favourite-web-development-blogs/</link>
		<comments>http://dan.cx/blog/2010/04/favourite-web-development-blogs/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 10:04:35 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[giveupinternet]]></category>
		<category><![CDATA[I love these people]]></category>

		<guid isPermaLink="false">http://dan.cx/blog/?p=257</guid>
		<description><![CDATA[I thought I&#8217;d give some link love to some of the lesser-known web development blogs I enjoy reading. This post was prompted by a post about my site at GiveUpInternet.com. I didn&#8217;t expect the link (as I don&#8217;t think my blog is very good for web development stuff), but I do appreciate it heaps! This [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I&#8217;d give some link love to some of the lesser-known web development blogs I enjoy reading. This post was prompted by a <a href="http://giveupinternet.com/2010/04/22/daniels-dan-cx-a-nice-web-development-blog/">post about my site at GiveUpInternet.com</a>. I didn&#8217;t expect the link (as I don&#8217;t think my blog is very good for web development stuff), but I do appreciate it heaps! This blog hasn&#8217;t really focused too much on web development, perhaps I should post more web development articles <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<ul>
<li><a href="http://giveupinternet.com/">GiveUpInternet.com</a> &mdash; As it says on the site, &#8220;<em>Give Up Internet is a Humor Blog for Internet People and Developers.</em>&#8221; While it&#8217;s technically not a web development blog, I love the posts on this site. Unlike a lot of other &#8220;humour&#8221; sites that post stupid things a lot of the time, it&#8217;s got actual funny posts. It&#8217;s one of the only humour blogs that I&#8217;m subscribed to.</li>
<li><a href="http://www.thecssninja.com/">The CSS Ninja</a> by Ryan Seddon&mdash; This is a great blog about nice little tricks that can be done in CSS. One of its focuses is doing things that previously required JavaScript, in pure CSS (no JavaScript whatsoever). This includes <a href="http://www.thecssninja.com/css/custom-inputs-using-css">cross-browser CSS-styled checkboxes</a>, <a href="http://www.thecssninja.com/css/futurebox3">a lightbox in pure CSS</a>, and <a href="http://www.thecssninja.com/css/even-better-image-preloading-with-css2">an easy way to preload images using CSS2</a>.</li>
<li><a href="http://davidwalsh.name/">David Walsh&#8217;s blog</a> &mdash; David is one of the core MooTools developers, and as such, he blogs mainly about JavaScript, and occasionally some PHP snippets. While, in my opinon, his posts on PHP are often messy <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> , his posts on JavaScript are excellent.</li>
<li><a href="http://www.adequatelygood.com/">AdequatelyGood.com</a> by Ben Cherry &mdash; If you&#8217;re interested in JavaScript (especially the nitty gritty of its internals), this is by far the best blog on the topic that I&#8217;ve seen. Ben has written detailed articles on a lot of unique features of JavaScript, including <a href="http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting">scoping and &#8220;hoisting&#8221;</a>, and <a href="http://www.adequatelygood.com/2010/3/Object-to-Primitive-Conversions-in-JavaScript">how it handles object to primitive conversions</a>. </li>
<li><a href="http://my.opera.com/hallvors/blog/">Hallvord R. M. Steen&#8217;s blog</a> and the <a href="http://my.opera.com/sitepatching/blog/">Opera sitepatching blog</a> &mdash; Hallvord is a developer for Opera Software. His blog covers the state of the web as it unfortunately is at the moment &#8211; Broken browser sniffer scripts, standards violations, and just general scripting stupidities. Things are definitely improving, but there&#8217;s a LOT of broken scripts out there. Opera has a file called &#8220;browser.js&#8221; that contains patches to make these broken sites work correctly in Opera. Hallford&#8217;s blog (and the Opera site patching blog) detail the things that Opera does to patch these broken sites. There have been some very interesting posts, including <a href="http://my.opera.com/hallvors/blog/2008/09/17/how-to-cook-tag-soup-with-xslt">the horrible XML and XSLT on the Israeli rail website</a>, <a href="http://my.opera.com/hallvors/blog/2008/11/12/how-google-docs-prints">how Google Docs used to print documents</a>, and many others.</li>
</ul>
<p>That&#8217;s all for now&#8230; I might eventually write another blog post like this. Or a proper blog post <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Until next time,<br />
 &mdash; Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2010/04/favourite-web-development-blogs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript for modern sites [Part 1]: Unobtrusive JavaScript and object literal syntax</title>
		<link>http://dan.cx/blog/2010/03/javascript-for-modern-sites/</link>
		<comments>http://dan.cx/blog/2010/03/javascript-for-modern-sites/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 11:07:55 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[MooTools > jQuery]]></category>

		<guid isPermaLink="false">http://dan.cx/blog/?p=239</guid>
		<description><![CDATA[In this post, I&#8217;ll discuss some of the techniques that I personally write JavaScript. There&#8217;s no right or wrong, this is all my opinion (still, feel free to flame me if you feel it&#8217;s necessary ). This post is aimed at people that understand basic JavaScript and HTML techniques, and want to see how I [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I&#8217;ll discuss some of the techniques that I personally write JavaScript. There&#8217;s no right or wrong, this is all <strong>my opinion</strong> (still, feel free to flame me if you feel it&#8217;s necessary <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ). This post is aimed at people that understand basic JavaScript and HTML techniques, and want to see how I code my JavaScript. I will talk about the JavaScript of the past, how it&#8217;s changed, and some techniques used in modern JavaScript development. This will probably be a multi-part series if I ever get around to writing more posts <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  <span id="more-239"></span></p>
<h3>The Bad Old Days</h3>
<p>So where do we begin? In the old days, JavaScript wasn&#8217;t used very much at all. Browsers didn&#8217;t really support much JavaScript, so it was only used to add little bits of functionality to sites. The one site might have had a few little procedural functions for the whole site. Global variables were used everywhere, but as the scripts were very small, this wasn&#8217;t <em>too </em>much of an issue. Usability also wasn&#8217;t considered much of an issue, and there was no proper event model, leading to things like:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:DoSomething();&quot;</span>&gt;</span>Do something<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>However, the JavaScript of today is something very different — It is everywhere, and is used for more than just trivial uses. On a normal website, a large amount of JavaScript might be used. Nowadays we even have <strong>web applications</strong> like Gmail and Google Docs which contain heaps of JavaScript for all their core functionality. There are a lot of web applications that are mainly client-side, with a very small server-side component. Because of this use of large scripts, we can no longer rely on unorganised scripts and use of global variables as in the past. Scripts written using these techniques of the past become very unmaintainable very quickly. Additionally, <strong>usability</strong> is a bigger issue today than it used to be, mainly due to the number of users on the Web (many of which have disabilities and require sites to be accessible).</p>
<p>So what can we do to improve our JavaScript? Well, let&#8217;s first see how we can neatly organise our JavaScript using&#8230;</p>
<h3>Object Literal Syntax</h3>
<p>You might be thinking, &#8220;what the heck is object literal syntax&#8221;? Simply put, it&#8217;s a simple syntax for creating a JavaScript object, added with the release of JavaScript 1.2 in June 1997. Here&#8217;s an example of an object created with this syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> MyObject <span style="color: #339933;">=</span>
<span style="color: #009900;">&#123;</span>
	myVariable<span style="color: #339933;">:</span> <span style="color: #CC0000;">123</span><span style="color: #339933;">,</span>
	hello<span style="color: #339933;">:</span> <span style="color: #3366CC;">'world'</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>MyObject.<span style="color: #660066;">myVariable</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// alerts &quot;123&quot;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>MyObject.<span style="color: #660066;">hello</span><span style="color: #009900;">&#41;</span>      <span style="color: #006600; font-style: italic;">// alerts &quot;world&quot;</span></pre></div></div>

<p>See? Pretty simple. It&#8217;s a lot like a hashtable in other programming languages (such as C#, Perl, and arrays in PHP). One of the great things about JavaScript (and a feature that differentiates it from some other languages) is that you can store<strong> functions inside variables</strong>. We can use this to our advantage:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> MyObject <span style="color: #339933;">=</span>
<span style="color: #009900;">&#123;</span>
	myVariable<span style="color: #339933;">:</span> <span style="color: #CC0000;">123</span><span style="color: #339933;">,</span>
	hello<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello world '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myVariable</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
MyObject.<span style="color: #660066;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// alerts &quot;Hello world 123&quot;</span></pre></div></div>

<p>See what we can do? Instead of having variables functions laying around all over the place, we can <strong>group them together</strong> into objects. This is a similar idea to <strong>namespaces</strong> in other languages, and is probably about the closest equivalent in JavaScript. When coding a site that uses JavaScript, my personal approach is generally to make <strong>one object per unique page that uses JavaScript</strong>. This keeps things nicely organised, and makes each page&#8217;s functions and variables self-contained. </p>
<p>Of course, we can also use proper classes to hold reusable components, but this is outside the scope of this post and will probably be covered in a future post <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Another modern technique is unobtrusive JavaScript&#8230;</p>
<h3>So what is unobtrusive JavaScript?</h3>
<p>In a nutshell, unobtrusive JavaScript is a coding technique that separates JavaScript from HTML in the same way that CSS is separate from HTML. Basically, with unobtrusive JavaScript, you have <strong>no</strong> JavaScript in the HTML at all, except for the &lt;script&gt; tag that loads the script file, and maybe a &lt;script&gt; tag to run an initialisation function. This means <strong>no </strong>onclick=&#8221;whatever()&#8221; attributes on any of your HTML elements. How do we add JavaScript click actions, then? Easy — Event handlers!</p>
<p>When using unobtrusive JavaScript, the JavaScript initialisation function &#8220;connects&#8221; the event handlers to all elements that need them. JavaScript event handlers are similar to that in other languages — Stuff throws events (such as click, mouseover, etc.), and functions can be set to run when these events are thrown. Previously, this was a little annoying, as Internet Explorer uses a different syntax to other browsers. However, today, there is no excuse, as JavaScript frameworks (such as MooTools and Prototype) and libraries (such as jQuery) make it really easy.</p>
<p>Another feature of unobtrusive JavaScript is that it involves <strong>progressive enhancement</strong> and <strong>graceful degradation</strong>. If the JavaScript <strong>doesn&#8217;t</strong> run (ie. if the user has JavaScript disabled), the user&#8217;s experience will still be acceptable. Maybe not as good as if they had JavaScript enabled, but still usable. We don&#8217;t rely on JavaScript, but we do take advantage of it if it&#8217;s available.  This usually involves adding event handlers (as mentioned above) that override the default action for links (that is, go to the URL they link to).</p>
<p>You might be confused by now (seems I ramble a lot in these posts), so let&#8217;s quickly jump to an example HTML file that shows some of the things we&#8217;ve mentioned so far:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JavaScript Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mootools-1.2.4-core-yc.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;script.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>window.addEvent('domready', UserList.init);<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Username<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">th</span>&gt;</span>Tools<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">th</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">thead</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user-1&quot;</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Daniel15<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/user/1/delete&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;delete&quot;</span>&gt;</span>Delete<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user-2&quot;</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Bob<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/user/2/delete&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;delete&quot;</span>&gt;</span>Delete<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;user-3&quot;</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Mark<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
				<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/user/3/delete&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;delete&quot;</span>&gt;</span>Delete<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tbody</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></pre></div></div>

<p>As you can see, absolutely <strong>no inline JavaScript</strong>, just some script loading, and setting the <strong>UserList.init</strong> method to run when the page loads. If JavaScript is disabled, clicking any of those &#8220;Delete&#8221; links will take the user to the corresponding delete page linked with the &lt;a&gt; tag. What happens if JavaScript is enabled? Let&#8217;s see! Here&#8217;s the corresponding JavaScript file:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> UserList <span style="color: #339933;">=</span>
<span style="color: #009900;">&#123;</span>
	init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// Connect up all the delete links</span>
		$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a.delete'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> UserList.<span style="color: #660066;">del</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	del<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> user_id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getParent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">id</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Delete user '</span> <span style="color: #339933;">+</span> user_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #006600; font-style: italic;">// Make sure the link doesn't go anywhere.</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is written using MooTools, but you could achieve a similar thing with another framework/library such as Prototype or jQuery. MooTools is just what I use <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Notice the init function &#8220;connects&#8221; the delete links to the delete JavaScript function (which I named &#8220;del&#8221;, &#8220;delete&#8221; is a reserved word in JavaScript). So, when you have JavaScript enabled, instead of going to the URL specified in the link, the UserList.del method will be run. This example just pops up an alert box, but this could easily be changed to use an AJAX call to delete the user. See? Magic! It&#8217;s fully functional with JavaScript disabled, but JavaScript being enabled adds a lot of niceness.</p>
<p>And yes, I know, that script is messy and could be made better, it&#8217;s just an example.</p>
<h3>Conclusion</h3>
<p>In this post, I&#8217;ve shown how to use object literal syntax to group JavaScript functions and variables together, and unobtrusive JavaScript techniques to progressively enhance pages. The advantages of doing so include neater and more maintanable code, and graceful degradation if JavaScript is disabled (not possible with &lt;a href=&#8221;javascript: &#8230; &#8220;&gt; links. There might eventually be a follow-up post. <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Hope this helped you <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Until next time,<br />
— Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2010/03/javascript-for-modern-sites/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Two types of developers/programmers</title>
		<link>http://dan.cx/blog/2010/01/two-types-of-developers/</link>
		<comments>http://dan.cx/blog/2010/01/two-types-of-developers/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 14:49:58 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://dan.cx/blog/?p=226</guid>
		<description><![CDATA[So, I&#8217;m not sure how many people agree with me (I haven&#8217;t really searched around to see if anyone has the same opinion), but I&#8217;m starting to form the opinion that there are two different types of developers: Those that can develop an application but don&#8217;t really understand the concepts behind it, and those that [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;m not sure how many people agree with me (I haven&#8217;t really searched around to see if anyone has the same opinion), but I&#8217;m starting to form the opinion that there are two different types of developers: Those that can develop an application but don&#8217;t really understand the concepts behind it, and those that have a relatively deep knowledge of how their code works and all fits together. Or, in other words, those think it&#8217;s alright (and perhaps have it as a job), but are not very passionate, versus those that are very passionate about programming. Generally, I guess something like the following could be said:</p>
<p>People in the first group:</p>
<ul>
<li>Have done their main programming study at University or TAFE OR are self-taught with just the basics, generally nothing about best practices. Generally, they&#8217;ve learnt just enough to get by, nothing more</li>
<li>Will copy and paste code and be happy that it works, but might not really understand how it actually works</li>
<li>Don&#8217;t really consider programming a hobby</li>
<li>Might be considered &#8220;code monkeys&#8221; in some situations</li>
<li>Generally need help with fixing odd bugs</li>
</ul>
<p>And people in the second group:</p>
<ul>
<li>May have done a University course, but their main learning is self-taught</li>
<li>Write applications, scripts, websites, whatever for fun (and might actively participate in open-source projects)</li>
<li>Don&#8217;t copy and paste code very often. Instead, they learn from other people&#8217;s code, and then rewrite that code in their own style</li>
<li>Consider programming one of their biggest hobbies</li>
<li>Aware of some of the latest trends in software development</li>
<li>Might often question things, like the ways people do things, and why code is written in a specific way (or is this just me?). Usually I do that just to learn how things are done.</li>
<li>Can generally investigate and solve odd bugs pretty well</li>
</ul>
<p>Anyone else agree with me? Personally I&#8217;m proud to be in the second group, the awesome group <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Anyways, I&#8217;ll write another proper blog post, eventually. I started working recently, and will definitely have to blog about that <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Until next time,<br />
— Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2010/01/two-types-of-developers/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Daniel15&#8242;s Introduction to Object-Oriented Programming</title>
		<link>http://dan.cx/blog/2009/04/introduction-to-object-oriented-programming/</link>
		<comments>http://dan.cx/blog/2009/04/introduction-to-object-oriented-programming/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 05:57:44 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HIT2302]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[object orientation]]></category>
		<category><![CDATA[object oriented]]></category>

		<guid isPermaLink="false">http://d15.biz/blog/?p=162</guid>
		<description><![CDATA[Well, back to posting coding-related blog posts, for now anyways . Seeing as a lot of people seem to be confused by Object Oriented Programming, I thought I&#8217;d post a quick (or maybe not so quick) post about what OOP is, the main features, and how it can benefit you. This is paraphrased from an [...]]]></description>
			<content:encoded><![CDATA[<p>Well, back to posting coding-related blog posts, for now anyways <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . Seeing as a lot of people seem to be confused by Object Oriented Programming, I thought I&#8217;d post a quick (or maybe not so quick) post about what OOP is, the main features, and how it can benefit you. This is paraphrased from an assignment I had on OOP last semester at university. I use C# code examples throughout this, but the concepts are very similar in other languages. Note that in this post, I assume you know the basics of programming, and just want to learn more about object orientation.</p>
<p>Now, let&#8217;s begin looking at what OOP actually means. At its core, the Object Oriented paradigm consists of <strong>classes</strong> and <strong>objects</strong>. A class is a “thing” or entity that has a purpose, and an <strong>object</strong> is an instance of this entity. For example, a Car would be a class, and <strong>my car</strong> would be an object (instance of the Car class).<br />
<span id="more-162"></span></p>
<h3>Classes in C#</h3>
<p>Before we can cover any of the OO principles with code examples, we need to go through some very simple code. A class in C# is made using the class keyword:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The &#8220;<em>public</em>&#8221; is an <strong>access modifier</strong>. The available access modifiers for classes in C# are:</p>
<ul>
<li><strong>public</strong> – No restrictions on accessibility of the class. It can be used from anywhere.</li>
<li><strong>private</strong> – Can only be accessed by code in the same class.</li>
<li><strong>protected</strong> – Can only be accessed by code in the same class, or in a derived class (see <strong>inheritance</strong>, below).</li>
<li><strong>internal</strong> – Can be accessed by any code in the same assembly (basically, the same file), but not from another assembly.</li>
</ul>
<p>Methods in the class are specified like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> StartCar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Like above, <em>public</em> is an access modifier. The <strong>virtual</strong> keyword means that this method can be overridden in child classes  (this is explained under “inheritance” below). “void” is the return type. A method that returns void basically returns nothing (the same as void in languages like C#, or procedures [as opposed to functions] in Pascal and Visual Basic). Some common return types include string, int, uint (unsigned int), long, ulong, float (Single-precision floating point number, 4 bytes) and double (Double-precision floating point number, 8 bytes)</p>
<h2>Core OO principles</h2>
<p>With all that said, let&#8217;s jump into the core OOP concepts. There are three “pillars” of OOP; these are the core principles of Object Oriented Programming. These are Encapsulation, Inheritance and Polymorphism. In addition to these, there are several other core ideas.</p>
<h3>Abstraction</h3>
<p>An abstraction is basically an idea, something that we think about to simplify things. In our car example (which will be used throughout this portfolio), a <strong>Car</strong> is an abstraction.</p>
<p>Encapsulation, Inheritance and Polymorphism (the pillars of OOP) are all abstractions.</p>
<h3>Object Composition</h3>
<p>Object composition is basically a way to combine simpler objects into larger, more complex ones. This is known as a “<strong>has-a</strong>” relationship. For example, a <strong>Car</strong> object might contain an Engine, Wheels, etc. We don&#8217;t need to know how all these other smaller objects work, but only how to interface with them (this is directly related to Encapsulation, see below).</p>
<h3>Encapsulation</h3>
<p>Encapsulation is the “hiding” of internal details of a class. This makes the class “friendlier”, and hides all its internals from the user. For example, a “car” class in C# could look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> StartCar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// Do whatever the car needs to do to start</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// More class members here</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We can use this class without knowing exactly how it starts the car, just that it starts it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
myCar.<span style="color: #0000FF;">StartCar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This also ensures that code outside the class can&#8217;t access data that it shouldn&#8217;t be allowed to touch. Allowing free access to all <strong>fields</strong> (values that the object “knows”) could lead to data corruption, as the object could be in an unknown state. For example, the car could have a field that says how much petrol is left in its petrol tank. We want the car to know how much petrol is left, but we don&#8217;t want other classes to be able to just change this value. In C#, we use private variables for this purpose:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We could then have a <strong>property</strong> to access this, and choose to only have a getter (no setter) for it, meaning that it can only be read:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Petrol
	<span style="color: #000000;">&#123;</span>
			get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _petrol<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Inheritance</h3>
<p>Inheritance basically allows us to build new classes which are based on existing classes. The new (lower-level) classes inherit all the functionality of those above it, and can also add new functionality. For example, we cam make a <strong>SportsCar</strong> class that inherits from Car:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SportsCar <span style="color: #008000;">:</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The SportsCar class will automatically have all the members that Car had (the petrol field, StartCar method, etc.), as well as any new members we define. To override a method in the Car class, we can use the <strong>override</strong> keyword. As long as the function is defined with “virtual”, this will work fine:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> StartCar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In fact, we could take this abstraction even further, and have a <strong>Vehicle</strong> class which the Car inherits from. Then we could have other vehicles (e.g. Bike, Bus, etc.) which all have common elements, and can be treated in the same way (polymorphism, described below)</p>
<h3>Polymorphism</h3>
<p>Polymorphism is the ability to access multiple different types of objects using the same methods. For instance, above, we defined SportsCar as a <strong>derived class</strong> of Car. We can treat SportsCar instances (objects) in the same way as we treat Cars, because the compiler knows that a SportsCar <strong>is a</strong> car:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Car mySportsCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SportsCar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
myCar.<span style="color: #0000FF;">StartCar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
mySportsCar.<span style="color: #0000FF;">StartCar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The two types of cars might start very differently, but they&#8217;re both called using the same StartCar method.</p>
<h2>More OO stuffs</h2>
<h3>Constructors</h3>
<p>A constructor is a function that&#8217;s called when an instance of the class is created. They&#8217;re usually used to set parameters at creation time. The constructor is a method with the same name as the class. For example, in our <strong>Car</strong> class, we could have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> petrol<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_petrol <span style="color: #008000;">=</span> petrol<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This means that when the Car class is created, we&#8217;d need to pass it one argument (not 0, not more than one).</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>We can give a class more than one constructor. In this case, each constructor is differentiated by the number of parameters. If the class is created with one parameter, the constructor with one parameter is called. Same with if the class is created with two parameters, the constructor with two parameters is called.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> DEFAULT_PETROL <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Default constructor</span>
	<span style="color: #0600FF;">public</span> Car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// They didn't pass a parameter, so assume defaults.</span>
		_petrol <span style="color: #008000;">=</span> DEFAULT_PETROL<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> petrol<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_petrol <span style="color: #008000;">=</span> petrol<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>An example of using these constructors:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// This will create a car, calling the default constructor.</span>
Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// And this uses the constructor with one argument.</span>
Car myOtherCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h3>Interfaces</h3>
<p>Interfaces are another core part of OOP in C#. An interface expresses a behaviour that a class may choose to implement. Unlike a derived class, any class may have as many interfaces as you like. For example, we could define a “IExplodable” interface:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IExplodable
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">void</span> Explode<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This defines an IExplodable interface, with a single method (Explode). Interfaces are always abstract; we can&#8217;t create a direct instance of them. This means that we don&#8217;t use the interface directly, instead we have classes that use the interfaces. For example, we could make our SportsCar IExplodable:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> SportsCar <span style="color: #008000;">:</span> Car, IExplodable
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// IExplodable implementation</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Explode<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// ...</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This makes the SportsCar use the IExplodable interface.</p>
<p>We can treat anything that implements IExplodable in the same way (this is Polymorphism at work). For example, a function like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ExplodeIt<span style="color: #000000;">&#40;</span>IExplodable thing<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	thing.<span style="color: #0000FF;">Explode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We can pass this any object that implements IExplodable:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">SportsCar myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SportsCar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Bike myBike <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Bike<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Microwave mike <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Microwave<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Potato awesomePotato <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Potato<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>myCar<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>myBike<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>mike<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>awesomePotato<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h2>Object design</h2>
<p>Now that I&#8217;ve covered all the basics of object orientation, I thought I&#8217;d quickly mention some things about object design. There are several things that contribute to a good design:</p>
<ul>
<li>Don&#8217;t put all the functionality in the one massive class or method (“centralised”). Instead, spread the functionality across multiple classes and multiple methods. This eases development, makes testing easier (see unit testing, above), and also makes the code more reusable.</li>
<li>Excessive coupling is not good, because it prevents the reuse of objects in other projects. Coupling refers to the degree to which each object relies on one of the other objects. If the objects all rely on each other too much, then changes in one will require a whole heap of changes in others, and they&#8217;re not as reusable.</li>
<li>High cohesion is good. Cohesion refers to how strongly-related or focused the responsibilities of a single class are. If all the methods of the class tend to be similar, the class is said to have high cohesion. High cohesion leads to more readable code, and the likelihood of code reuse inceases. As an example, in the Car class, the methods (eg. StartCar, Drive, etc) all do something to the cat.</li>
</ul>
<h2>Culture</h2>
<p>Talking about programming culture usually involves discussion of coding standards. Coding standards are important because we need to stay consistent throughout our code, so other developers are able to understand it. The main standards that are followed in C# (including those <a href="http://mercury.it.swin.edu.au/swinbrain/index.php/Swinburne_.NET_Coding_Standard">followed at Swinburne</a>) include:</p>
<ul>
<li>Use an underscore at the start of private members of a class (e.g. _petrol in the examples above)</li>
<li>Use camel casing for local variables and parameters. Camel Case is basically words stuck together, with each word except the first starting with a capital letter (e.g. myCar)</li>
<li>Use pascal casing (like camel case, except with the first letter capitalised as well) for the names of Classes and Methods (e.g. Car, SportsCar)</li>
<li>Use an I to prefix the names of interfaces (e.g. IExplodable)</li>
<li>File names should be the same as the class name (e.g. Car.cs, SportsCar.cs)</li>
<li>Use meaningful descriptive words for the names of variables, classes, methods, objects, etc. The names should be useful so we know what the class or method is without looking through all the code.</li>
</ul>
<p>Well, that&#8217;s all for now. Hope this has helped someone <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&mdash; Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2009/04/introduction-to-object-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using TCP sockets in Pascal, connect to remote servers</title>
		<link>http://dan.cx/blog/2008/03/using-tcp-sockets-in-pascal/</link>
		<comments>http://dan.cx/blog/2008/03/using-tcp-sockets-in-pascal/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 10:59:30 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[HIT1301 Portfolio]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[pascal]]></category>
		<category><![CDATA[hit1301]]></category>
		<category><![CDATA[sockets]]></category>
		<category><![CDATA[synapse]]></category>
		<category><![CDATA[tcp]]></category>
		<category><![CDATA[winsock]]></category>

		<guid isPermaLink="false">http://www.daniel15.com/blog/2008/03/21/using-tcp-sockets-in-pascal/</guid>
		<description><![CDATA[TCP sockets in Pascal are generally hard to use; Free Pascal doesn&#8217;t come with any high-level socket libraries by default, only a relatively low-level socket library. Some external libraries are available to make using sockets with Pascal easier, and one of these libraries is Synapse. Synapse is an easy-to-use socket library for Pascal, and in [...]]]></description>
			<content:encoded><![CDATA[<p>TCP sockets in Pascal are generally hard to use; Free Pascal doesn&#8217;t come with any high-level socket libraries by default, only a relatively low-level <tt>socket</tt> library. Some external libraries are available to make using sockets with Pascal easier, and one of these libraries is <strong>Synapse</strong>. Synapse is an easy-to-use socket library for Pascal, and in this blog post I&#8217;ll try to show how to use Synapse to connect to a remote server and send/receive data from it. </p>
<p><span id="more-69"></span>From <a href="http://synapse.ararat.cz/">the official Synapse site</a>:</p>
<blockquote><p>
This project deals with network communication by means of blocking (synchronous) sockets or with limited non-blocking mode. This project not using asynchronous sockets! The Project contains simple low level non-visual objects for easiest programming without problems. (no required multithread synchronisation, no need for windows message processing,…) Great for command line utilities, visual projects, NT services
</p></blockquote>
<h3>&#8220;Installing&#8221; it</h3>
<p>Firstly, you&#8217;ll want to <a href="http://synapse.ararat.cz/doku.php/download">download the stable release of Synapse</a>, and place them somewhere. At the time of writing, the latest Synapse version is release number <strong>38</strong>. Once you&#8217;ve downloaded it, extract the files somewhere (it doesn&#8217;t matter where you extract them to, as long as you remember the directory name. I&#8217;d suggest to create a directory for all your Free Pascal library code). Next, we need to edit the config file, so that Free Pascal can find these libraries. Open your Free Pascal configuration file (on Linux, this is at<tt>/etc/fpc.cfg</tt>. On Windows, this <em>should</em> be in the directory you installed Free Pascal to). Search for this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"># searchpath for libraries</pre></div></div>

<p>Right before that, add <tt>-Fu</tt> followed by the path to the directory you made earlier. In my case, I added:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">-Fu/home/daniel/fpc</pre></div></div>

<h3>Using it in your code</h3>
<p>In most cases, you&#8217;ll be using the <tt>TTCPBlockSocket</tt> class. This is included in the <tt>blcksock</tt> unit, so add this unit to the <em>uses</em> clause of your application:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">uses</span> blcksock;</pre></div></div>

<h3>Example</h3>
<h4>Connecting to a server</h4>
<p>This is probably the most common way you&#8217;d use a socket &mdash; Connecting directly to another server. The functionality for this is contained in the <tt>TTCPBlockSocket</tt> class. Firstly, we need to define a variable to store the socket in:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span>
	sock<span style="color: #339933;">:</span> TTCPBlockSocket;</pre></div></div>

<p>And then we need to actually create the socket:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">sock <span style="color: #339933;">:=</span> TTCPBlockSocket.<span style="color: #0066ee;">Create</span>;</pre></div></div>

<p>This creates a socket named <strong>sock</strong> that we&#8217;re able to use. The next step is to connect to the remote server, using the <a href="http://synapse.ararat.cz/doc/help/blcksock.TTCPBlockSocket.html#Connect">Connect</a> method:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">sock.<span style="color: #0066ee;">Connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'66.79.183.71'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'80'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #666666; font-style: italic;">// Was there an error?</span>
<span style="color: #000000; font-weight: bold;">if</span> sock.<span style="color: #0066ee;">LastError</span> &lt;&gt; <span style="color: #cc66cc;">0</span> <span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #000000; font-weight: bold;">begin</span>
	<span style="color: #000066;">writeLn</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Could not connect to server.'</span><span style="color: #009900;">&#41;</span>;
	halt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">end</span>;</pre></div></div>

<p>At this point, the connection to the remote server has been established, and we may send and receive data.</p>
<h4>Sending Data</h4>
<p>Sending data is done via the <a href="http://synapse.ararat.cz/doc/help/blcksock.TBlockSocket.html#SendString">SendString</a> method. Note that this is slightly different to some other languages; it <strong>does not</strong> add a carriage return and linefeed to the end of the line, you have to add this manually if required.</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">sock.<span style="color: #0066ee;">SendString</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'GET /blog/ HTTP/1.1'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #ff0000;">'Host: www.daniel15.com'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h4>Receiving Data</h4>
<p>There are several methods for receiving data, but the two main ones are <tt><a href="http://synapse.ararat.cz/doc/help/blcksock.TBlockSocket.html#RecvString">RecvString</a></tt> and <tt><a href="http://synapse.ararat.cz/doc/help/blcksock.TBlockSocket.html#RecvPacket">RecvPacket</a></tt>. RecvString reads a single string (terminated by a carriage return and linefeed) from the socket, and returns this string <strong>without</strong> the carriage return. RecvPacket reads all data waiting to be read, and returns it unmodified (all carriage returns and linefeeds will still be there). Both commands take one parameter: A timeout. If the socket doesn&#8217;t contain any data within this timeout, it returns a blank string.</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">buffer <span style="color: #339933;">:=</span> sock.<span style="color: #0066ee;">RecvPacket</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h4>Putting it all together</h4>
<p>Here&#8217;s an example application that connects to a web server, does a simple HTTP request, and writes the response to the console:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">program</span> TestApp;
&nbsp;
<span style="color: #000000; font-weight: bold;">uses</span>
	blcksock;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span>
	sock<span style="color: #339933;">:</span> TTCPBlockSocket;
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span>
	buffer<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">String</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span>;
<span style="color: #000000; font-weight: bold;">begin</span>
	sock <span style="color: #339933;">:=</span> TTCPBlockSocket.<span style="color: #0066ee;">Create</span>;
&nbsp;
	sock.<span style="color: #0066ee;">Connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'66.79.183.71'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'80'</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #666666; font-style: italic;">// Was there an error?</span>
	<span style="color: #000000; font-weight: bold;">if</span> sock.<span style="color: #0066ee;">LastError</span> &lt;&gt; <span style="color: #cc66cc;">0</span> <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #000000; font-weight: bold;">begin</span>
		<span style="color: #000066;">writeLn</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Could not connect to server.'</span><span style="color: #009900;">&#41;</span>;
		halt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">end</span>;
	<span style="color: #666666; font-style: italic;">// Send a HTTP request</span>
	sock.<span style="color: #0066ee;">SendString</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'GET /blog/ HTTP/1.1'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #ff0000;">'Host: www.daniel15.com'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #666666; font-style: italic;">// Keep looping...</span>
	<span style="color: #000000; font-weight: bold;">repeat</span>
		buffer <span style="color: #339933;">:=</span> sock.<span style="color: #0066ee;">RecvPacket</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000066;">write</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span>;
	<span style="color: #666666; font-style: italic;">// ...until there's no more data.</span>
	<span style="color: #000000; font-weight: bold;">until</span> buffer <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span>;
<span style="color: #000000; font-weight: bold;">end</span>;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">begin</span>
	Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">end</span>.</pre></div></div>

<p>The loop is needed because the data may come in multiple packets.</p>
<p>Not that this is <strong>not</strong> really a good example, as there&#8217;s a HTTP library built-in to Synapse. The in-built HTTP library has several advantages, including the ability to use HTTP proxies. Perhaps I&#8217;ll cover that in a future blog post <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2008/03/using-tcp-sockets-in-pascal/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
