<?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 19-year-old web developer from Melbourne, Australia</description>
	<lastBuildDate>Sat, 02 Jan 2010 14:49:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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>3</slash:comments>
		</item>
		<item>
		<title>Daniel15&#8217;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 [...]]]></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>
