<?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>OJ's rants &#187; C#</title>
	<atom:link href="http://buffered.io/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://buffered.io</link>
	<description>What would OJ do?</description>
	<lastBuildDate>Tue, 06 Jul 2010 20:32:00 +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>A Quirk in List.Find()</title>
		<link>http://buffered.io/2009/03/25/a-quirk-in-listfind/</link>
		<comments>http://buffered.io/2009/03/25/a-quirk-in-listfind/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 19:31:30 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[generics]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=675</guid>
		<description><![CDATA[Earlier today I was having a chat with a friend of mine, who lives in Vancouver, about finding items that are stored in generic Lists. He flicked me a code snippet that looked something like this: List&#60;foo&#62; list = new List&#60;foo&#62;&#40;&#41;; // .. do some stuff Foo f = list.Find&#40;delegate&#40;Foo f&#41; &#123; return foo.Name == [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today I was having a chat with a <a href="http://doomkeeper.com/" title="Jimmy's Blog">friend of mine</a>, who lives in Vancouver, about finding items that are stored in <a href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" title="List(T)">generic Lists</a>. He flicked me a code snippet that looked something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span> list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// .. do some stuff</span>
Foo f <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span>Foo f<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> foo.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Bar&quot;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Straight away I fired back with an update to the code which used <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" title="Lambda Expressions (C# Programming Guide)">lambda expressions</a> instead, as I'm a fan of how concise they are <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span> list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>foo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// .. do some stuff</span>
Foo f <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>foo <span style="color: #008000;">=&gt;</span> foo.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Bar&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>My friend ran this code against a data set that he had constructed and found that when the call to <a href="http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx" title="List(T).Find">Find()</a> was made, a <a href="http://msdn.microsoft.com/en-us/library/system.nullreferenceexception.aspx" title="System.NullReferenceException Class">NullReferenceException</a> was being thrown. I found this odd as I hadn't seen that before. <em>list</em> was definitely a valid reference and the lambda expression was well-formed as well. So what was wrong?</p>
<p>It turns out that even though <em>list</em> was a valid reference, it <strong>didn't contain any elements</strong>.</p>
<p>How odd! Why would the generic List object throw an exception when the user calls Find() when no elements are present? After a little bit of thinking I thought that I had the answer. I thought to myself:<br />
<blockquote cite="Myself">
<p>What if the List was a container for a <a href="http://msdn.microsoft.com/en-us/library/34yytbws(VS.71).aspx" title="Value Types">value-type</a>, such as <em>int</em>? If you attempt to find a value in an empty list, then the function cannot return <em>null</em> because that isn't valid for value-types! Throwing an exception <em>does</em> make sense!</p>
</blockquote>
<p>Isn't it amazing how easy it is to convince yourself of your own greatness? I thought I'd nailed it first go. So I proposed my argument to my friend, who initially was semi-sold on the idea.</p>
<p>Then I thought about it again and managed to convince myself that my apparent "brilliance" was, in fact, a failure. The perfect counter-argument to the above point is:<br />
<blockquote cite="Myself">
<p>What happens when you have a List of ints which <em>does</em> contain elements and you attempt to search for a value that <strong>is not in the list</strong>?</p>
</blockquote>
<p>It wasn't immediately obvious. So I tried something to see what would happen:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// ....</span></pre></div></div>

<p>So what do you think the value of <em>i</em> is after those first two lines? Yes, you guessed it: <strong>Zero</strong>. Why? Well, duh, it's because <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx" title="default Keyword">default(T)</a> for integers is Zero!</p>
<p>This is where little alarm bells started to ring in my head. I immediately whipped up an example where this would be considered bad:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// ....</span></pre></div></div>

<p>Again, <em>i</em> is Zero when this code is executed, but the result is very misleading. Zero is contained in the collection but doesn't match the predicate, yet Zero is returned because that's the default value for this value-type.</p>
<p>I thought this was a bit of a glaring hole in the design. So I went straight to the <a href="http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx" title="List(T).Find">documentation</a> and found this:<br />
<blockquote>
<p>
<strong>Important Note:</strong></p>
<p>When searching a list containing value types, make sure the default value for the type does not satisfy the search predicate. Otherwise, there is no way to distinguish between a default value indicating that no match was found and a list element that happens to have the default value for the type. If the default value satisfies the search predicate, use the <a href="http://msdn.microsoft.com/en-us/library/0k601hd9.aspx" title="FindIndex">FindIndex</a> method instead.</p>
</blockquote>
<p>This was concerning for a couple of reasons. First of all, the designers have left it up to you to determine that this is the default behaviour. Yes I should be able to come to that conclusion myself, but I didn't until I got bitten <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> So shut up! Secondly, you have to check your result value against your predicate <em>again</em> to be sure that it's not dodgey. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>i <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// .. valid value, do stuff ..</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">else</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// .. no item found</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Do <em>you</em> want to do that? I certainly don't. After a bit of back-and-forth with Jimbo, I thought that the best option for a generic List Find() function would be one that is akin to the good old C++ days. It would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">bool</span> Find<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>Predicate<span style="color: #008000;">&lt;</span>t<span style="color: #008000;">&gt;</span> predicate, <span style="color: #0600FF;">ref</span> T output<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This would mean that you could change your code to something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span> i<span style="color: #008000;">;</span>
List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> list <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>list.<span style="color: #0000FF;">Find</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</span>, <span style="color: #0600FF;">ref</span> i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// .. valid value, do stuff ..</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">else</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// .. no item found, or empty list!</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note how with this option you could easily support the case for empty lists at the same time. It would be helpful and meaningful. Only when the function returns true can you rely on the output parameter. It's very clear and caters for value-types and reference-types. It'd be easy to implement in an <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" title="Extension Methods (C# Programming Guide)">extension method</a> as well. I'd prefer this solution over using FindIndex().</p>
<p>In case it's not obvious, this problem would no doubt exist in all functions on generic objects that attempt to return a single instance of <em>T</em> based on some form of predicate. <a href="http://msdn.microsoft.com/en-us/library/5kthb929.aspx" title="List(T).FindLast">FindLast()</a> would be another example.</p>
<p>I'm very keen to know the reasons behind the original design decision. I'm sure that minds far greater than mine parsed that problem and came up with that solution, probably for a very good reason.</p>
<p>What do you guys think?</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2009/03/25/a-quirk-in-listfind/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Always Question the Source (aka &#8220;Don&#8217;t Lock on Type Objects&#8221;)</title>
		<link>http://buffered.io/2009/02/10/always-question-the-source-aka-dont-lock-on-type-objects/</link>
		<comments>http://buffered.io/2009/02/10/always-question-the-source-aka-dont-lock-on-type-objects/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 11:22:27 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WTF]]></category>
		<category><![CDATA[assumptions]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[stupidity]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=623</guid>
		<description><![CDATA[For one reason or another, I recently found myself perusing some code based on the CSLA framework. While nosing around I came upon a snippet of code that I found rather disturbing. An example can be found here in the function called InitializeAuthorizationRules. For those who are lazy, here is the particular snippet of code [...]]]></description>
			<content:encoded><![CDATA[<p>For one reason or another, I recently found myself perusing some code based on the <a href="http://www.lhotka.net/cslanet/" title="CSLA">CSLA</a> framework. While nosing around I came upon a snippet of code that I found rather disturbing. An example can be found <a href="http://www.lhotka.net/cslacvs/viewvc.cgi/trunk/cslacs/Csla/Core/BusinessBase.cs?revision=3690&#038;view=markup" title="BusinessBase.cs">here</a> in the function called InitializeAuthorizationRules.</p>
<p>For those who are lazy, here is the particular snippet of code that caught my eye:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">lock</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// .. stuff ..</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If you want to see more, head over there and read up. There are quite a few instances of the code listed above.</p>
<p>So why is this disturbing? If you're not sure of the answer, take a bit of time to go and read up on C#'s <a href="http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx" title="lock Statement (C# Reference)">lock</a> keyword. When you're done, ask yourself "what kind of object should I be using alongside the lock keyword?".</p>
<p>If the answer escapes you, then toodle over to <a href="http://www.mail-archive.com/bdotnet@groups.msn.com/msg06816.html" title="Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad">this little doozy</a> for a blow by blow account. I'll quote a rather gifted developer friend of mine (who for now shall remain anonymous) who summed up nicely one of the issues that could occur if the above code makes it into your code base:</p>
<blockquote><p>This is terrible, terrible, terrible.</p>
<p>Good luck with the cross-appdomain deadlock which brings down prod and can't be diagnosed without 2 weeks behind windbg.</p>
</blockquote>
<p> Soooo true.</p>
<p>Now that you know it's bad you might be wondering how a framework like CSLA managed to get polluted by it. Time to speculate!</p>
<p>You may have noticed that the article I linked above mentions that the practice of locking type objects was actually demonstrated/advocated on MSDN:</p>
<blockquote><p>
This is even done in MSDN sample, makuing it the holy grail.<br />
.<br />
.<br />
Rico Mariani, performance architect for the Microsoft® .NET runtime and longtime<br />
Microsoft developer, mentioned to Dr. GUI in an e-mail conversation recently that a<br />
fairly common practice (and one that's, unfortunately, described in some of our<br />
documentation right now, although we'll be fixing that) is actually quite problematic.
</p>
</blockquote>
<p><em>Note: Spelling mistakes and awful grammar in the quote above are, for once, not my fault.</em></p>
<p>I'm guessing that the author(s) of CSLA were reading up on some multithreading documentation on MSDN and came across a sample which demonstrated locking type objects as shown above. Since they were reading MSDN, the apparent Bible for all things .NET, they may have assumed that whatever they saw could be taken as Gospel.</p>
<p>Unfortunately, no resource is perfect. Not even MSDN.</p>
<p>This is where I have my gripe. The authors of <strong>any</strong> software should <strong>always</strong> critique <strong>all</strong> of the code they come across during the course of development. Whether they wrote it themselves, got it from MSDN, read it on a blog site while researching or saw it in a book written by the author of the language. <em>NEVER EVER</em> assume that the code you are reading is 100% sound.</p>
<p>If the authors had thought about the <em>meaning</em> of the lock statement and had an understanding of exactly what <a href="http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx" title="Object.GetType Method">GetType()</a> does (ie. always returns the <em>same</em> reference when called on the same type), then perhaps they might have figured out that using a lock on something that's accessible from <strong>any object in the process</strong> is a bad idea. It is opening the door for potential deadlocks if somebody else decides to do the same.</p>
<p>So I say again: <em>do not assume that the code you get off the Internet is safe!</em> Scrutinise it. Pull it to pieces. Understand it. Then, if all is safe and you're still comfortable with it, consider using it in your software. Don't assume that the author of the code knows what they're doing...</p>
<p>... unless it's me of course <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';)' class='wp-smiley' /> </p>
<p>
<!-- Begin Google Adsense code -->
<p><center>
<script type="text/javascript"><!--
google_ad_client = "pub-5662448744756930";
/* 468x60, created 2/11/09 */
google_ad_slot = "8047929723";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></center></p>
<!-- End Google Adsense code -->
</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2009/02/10/always-question-the-source-aka-dont-lock-on-type-objects/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>WPF Shader FX on Codeplex</title>
		<link>http://buffered.io/2008/09/09/wpf-shader-fx-on-codeplex/</link>
		<comments>http://buffered.io/2008/09/09/wpf-shader-fx-on-codeplex/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 10:22:15 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Effects]]></category>
		<category><![CDATA[FX]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Shader]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=531</guid>
		<description><![CDATA[This is just a quick post to point out a new project that has fired up on Codeplex which may be of interest to a few of you graphics and rich client fans. Joseph Cooney of LearnWPF.com (and WPF MVP to the stars) has kicked off a Codeplex project targetting funky shaders for use with [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post to point out a new project that has fired up on <a href="http://www.codeplex.com/" title="Codeplex">Codeplex</a> which may be of interest to a few of you graphics and rich client fans.</p>
<p><a href="http://jcooney.net/" title="JCooney.NET">Joseph Cooney</a> of <a href="http://learnwpf.com/" title="Learn WPF">LearnWPF.com</a> (and <a href="http://en.wikipedia.org/wiki/Windows_Presentation_Foundation" title="WPF">WPF</a> <a href="http://mvp.support.microsoft.com/" title="MVP">MVP</a> to the stars) has kicked off a <a href="http://www.codeplex.com/fx" title="FX project @ Codeplex">Codeplex project</a> targetting funky shaders for use with the new features of WPF that were included in <a href="http://www.microsoft.com/downloads/details.aspx?familyid=ab99342f-5d1a-413d-8319-81da479ab0d7" title=".NET 3.5 SP1">SP1</a>. Specifically, it's intended to be a collection of open source/free shaders that can be easily plugged into your WPF apps to make them look schmick (for more info, <a href="http://learnwpf.com/Posts/Post.aspx?postId=398ed9d5-c56a-4ad5-830d-02e2d8d3bf26" title="WPF Shader Effects Community Project Launched">check this out</a>).</p>
<p>It's an open source/community thing where everyone is encouraged to contribute. I'll be aiming to add as much as I can given the time constraints of work and family life. JC has already added 5 effects, and I've added my first, which was a simple Radial Blur shader.</p>
<p>This stuff really is fun, so check it out!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/09/09/wpf-shader-fx-on-codeplex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET-fu: Signing an Unsigned Assembly (without Delay Signing)</title>
		<link>http://buffered.io/2008/07/09/net-fu-signing-an-unsigned-assembly-without-delay-signing/</link>
		<comments>http://buffered.io/2008/07/09/net-fu-signing-an-unsigned-assembly-without-delay-signing/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 02:13:23 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tips/Tricks]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[disassemble]]></category>
		<category><![CDATA[ilasm]]></category>
		<category><![CDATA[ildasm]]></category>
		<category><![CDATA[MSIL]]></category>
		<category><![CDATA[sign]]></category>
		<category><![CDATA[snk]]></category>
		<category><![CDATA[strong name]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=373</guid>
		<description><![CDATA[This article is also available in: Italian The code-base that I am currently working with consists of a large set of binaries that are all signed. The savvy .NET devs out there will know that any assembly that's used/referenced by a signed assembly must also be signed. This is an issue when dealing with third-party [...]]]></description>
			<content:encoded><![CDATA[<p>This article is also available in: <a href="http://www.otherbit.com/modules/blog/BlogContent.aspx?ID=174" title=".NET-FU : come trasformare in SIGNED un assembly UNSIGNED (senza ricorrere al DELAY SIGNING)">Italian</a></p>
<hr/>
The code-base that I am currently working with consists of a large set of binaries that are all <a href="http://msdn.microsoft.com/en-us/library/xc31ft41.aspx" title="Sign an Assembly with a Strong Name">signed</a>. The savvy .NET devs out there will know that any assembly that's used/referenced by a signed assembly must <em>also</em> be signed.</p>
<p>This is an issue when dealing with third-party libraries that are not signed. Sometimes you'll be lucky enough to be dealing with vendor that is happy to provide a set of signed assemblies, other times you won't. If your scenario fits the latter (as a recent one did for my colleagues and I), you need to sign the assemblies yourself. Here's how.</p>
<p><em>Note:</em> <a href="http://msdn.microsoft.com/en-us/library/t07a3dye(VS.80).aspx" title="Delay Signing an Assembly">delay signing</a> is not covered in this article.</p>
<h2>Scenario 1 - Foo and Bar</h2>
<p><strong>Foo</strong> is the component that you're building which has to be signed.<br />
<strong>Bar</strong> is the third-party component that you're forced to use that <em>isn't</em>.</p>
<p><img src="http://buffered.io/wp-content/uploads/2008/07/foobar.png" alt="Relationship between Foo and Bar" /><br />
Grab <a href="http://buffered.io/wp-content/uploads/2008/07/bar.zip" title="Project/Binary for Bar"><em>Bar.dll</em> and project</a> along with <a href="http://buffered.io/wp-content/uploads/2008/07/foobar.zip" title="Project/Binary for Foo"><em>Foo.dll</em> and project</a> to see a source sample.</p>
<p>You'll notice <em>Foo</em> has a .snk which is used to sign <em>Foo.dll.</em> When you attempt to compile <em>Foo</em> you get the following error message:<br />
<blockquote>
<p>Assembly generation failed -- Referenced assembly 'Bar' does not have a strong name</p>
</blockquote>
<p>We need to sign <em>Bar</em> in order for <em>Foo</em> to compile.</p>
<p><img src="http://buffered.io/wp-content/uploads/2008/07/step1.jpg" style="float: right; margin-left: 5px; margin-bottom: 2px;" alt="Disassemble Bar" /><br />
<h3>Step 1 - Disassemble Bar</h3>
<p>We need to open a command prompt which has the .NET framework binaries in the <a href="http://en.wikipedia.org/wiki/Path_%28computing%29" title="Path">PATH</a> <a href="http://en.wikipedia.org/wiki/Environment_variable" title="Environment variable">environment variable</a>. The easiest way to do this is to open a Visual Studio command prompt (which is usually under the "Visual Studio Tools" subfolder of "Visual Studio 200X" in your programs menu). Change directory so that you're in the folder which contains <em>Bar.dll</em>.</p>
<p>Use <a href="http://msdn.microsoft.com/en-us/library/f7dy01k1(VS.80).aspx" title="MSIL Disassembly">ildasm.exe</a> to disassemble the file using the <strong>/all</strong> and <strong>/out</strong>, like so:</p>
<pre>
C:\Foo\bin> ildasm /all /out=Bar.il Bar.dll
</pre>
<p>The result of the command is a new file, <em>Bar.il</em>, which contains a dissassembled listing of <em>Bar.dll</em>.</p>
<p><img src="http://buffered.io/wp-content/uploads/2008/07/step2.jpg" style="float: right; margin-left: 5px; margin-bottom: 2px;" alt="Rebuild and Sign Bar" /><br />
<h3>Step 2 - Rebuild and Sign Bar</h3>
<p>We can now use <a href="http://msdn.microsoft.com/en-us/library/496e4ekx.aspx" title="MSIL Assembler">ilasm</a> to reassemble <em>Bar.il</em> back into <em>Bar.dll</em>, but at the same time specify a strong-name key to use to sign the resulting assembly. We pass in the value <em>Foo.snk</em> to the <strong>/key</strong> switch on the command line, like so:
<div style="clear:both;"></div>
<pre>
C:\Foo\bin> ilasm /dll /key=Foo.snk Bar.il

Microsoft (R) .NET Framework IL Assembler.  Version 2.0.50727.1434
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembling 'Bar.il'  to DLL --> 'Bar.dll'
Source file is ANSI

Assembled method Bar.Bar::get_SecretMessage
Assembled method Bar.Bar::.ctor
Creating PE file

Emitting classes:
Class 1:        Bar.Bar

Emitting fields and methods:
Global
Class 1 Methods: 2;
Resolving local member refs: 1 -> 1 defs, 0 refs, 0 unresolved

Emitting events and properties:
Global
Class 1 Props: 1;
Resolving local member refs: 0 -> 0 defs, 0 refs, 0 unresolved
Writing PE file
Signing file with strong name
Operation completed successfully
</pre>
<p><em>Bar.dll</em> is now signed! All we have to do is reopen <em>Foo</em>'s project, remove the reference to <em>Bar.dll</em>, re-add the reference to the new signed assembly and rebuild. Sorted!</p>
<h2>Scenario 2 - Foo, Bar and Baz</h2>
<p><strong>Foo</strong> is the component that you're building which has to be signed.<br />
<strong>Bar</strong> is the third-party component that you're forced to use that <em>isn't</em>.<br />
<strong>Baz</strong> is another third-party component that is required in order for you to use <em>Bar</em>.</p>
<div class="LargeImage"><img src="http://buffered.io/wp-content/uploads/2008/07/foobarbaz.png" alt="Relationship between Foo, Bar and Baz"/></div>
<p>Grab <a href="http://buffered.io/wp-content/uploads/2008/07/baz.zip" title="Project/Binary for Baz"><em>Baz.dll</em> and project</a>, <a href="http://buffered.io/wp-content/uploads/2008/07/barbaz.zip" title="Project/Binary for Bar"><em>Bar.dll</em> and project</a> along with <a href="http://buffered.io/wp-content/uploads/2008/07/foobarbaz.zip" title="Project/Binary for Foo"><em>Foo.dll</em> and project</a> for a sample source.</p>
<p>When you attempt to build <em>Foo</em> you get the same error as you do in the previous scenario. Bear in mind that this time, <strong>both</strong> <em>Bar.dll</em> and <em>Baz.dll</em> need to be signed. So first of all, follow the steps in <strong>Scenario 1</strong> for both <em>Bar.dll</em> and <em>Baz.dll</em>.</p>
<p>Done? OK. When you attempt to build <em>Foo.dll</em> after pointing the project at the new <em>Bar.dll</em> no compiler errors will be shown. Don't get too excited <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>When you attempt to <strong>use</strong> <em>Foo.dll</em> your world will come crashing down. The reason is because <em>Bar.dll</em> was originally built with a reference to an <u>unsigned version</u> of <em>Baz.dll</em>. Now that <em>Baz.dll</em> is signed we need to force <em>Bar.dll</em> to reference the <strong>signed</strong> version of <em>Baz.dll</em>.</p>
<p><img src="http://buffered.io/wp-content/uploads/2008/07/step3.jpg" style="float: right; margin-left: 5px; margin-bottom: 2px;" alt="Hack the Disassembled IL" /><br />
<h3>Step 1 - Hack the Disassembled IL</h3>
<p>Just like we did in the previous steps we need to disassemble the binary that we need to fix. This time, make sure you disassemble the new binary that you created in the previous step (this binary has been signed, and will contain the signature block for the strong name). Once <em>Bar.il</em> has been created using ildasm, open it up in a <a href="http://www.vim.org/" title="VIM - secretGeek loves it.. no really, he does!">text editor</a>.</p>
<p>Search for the reference to <em>Baz</em> -- this should be located a fair way down the file, somewhere near the top of the actual code listing, just after the comments. Here's what it looks like on my machine:</p>
<pre>.assembly extern /*23000002*/ Baz
{
  .ver 1:0:0:0
}</pre>
<p>This external assembly reference is missing the all-important public key token reference. Before we can add it, we need to know what the public key token is for <em>Bar.dll</em>. To determine this, we can use the <a href="http://msdn.microsoft.com/en-us/library/k5b5tt23(VS.80).aspx" title="Strong Name Tool">sn.exe</a> utility, like so:</p>
<pre>C:\Foo\bin> sn -Tp Baz.dll

Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.21022.8
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key is
0024000004800000940000000602000000240000525341310004000001000100a59cd85e10658d
9229d54de16c69d0b53b31f60bb4404b86eb3b8804203aca9d65412a249dfb8e7b9869d09ce80b
0d9bdccd4943c0004c4e76b95fdcdbc6043765f51a1ee331fdd55ad25400d496808b792723fc76
dee74d3db67403572cddd530cadfa7fbdd974cef7700be93c00c81121d978a3398b07a9dc1077f
b331ca9c

Public key token is 2ed7bbec811020ec
</pre>
<p>Now we return to <em>Bar.il</em> and modify the assembly reference so that the public key token is specified. This is what it should look like after modification:</p>
<pre>.assembly extern /*23000002*/ Baz
{
  .publickeytoken = (2E D7 BB EC 81 10 20 EC )
  .ver 1:0:0:0
}</pre>
<p>Save your changes.</p>
<p><img src="http://buffered.io/wp-content/uploads/2008/07/step4.jpg" style="float: right; margin-left: 5px; margin-bottom: 2px;" alt="Reassemble Bar" /><br />
<h3>Step 2 - Reassemble Bar</h3>
<p>This step is just a repeat of previous steps. We are again using ilasm to reassemble <em>Bar.dll</em>, but this time from the new "hacked" <em>Bar.il</em> file. We must use the exact same command line as we did previously, and we still need to specify the <em>Foo.snk</em> for signing the assembly. To save you having to scroll up, here it is again:</p>
<pre>C:\Foo\bin> ilasm /dll /key=Foo.snk Bar.il

Microsoft (R) .NET Framework IL Assembler.  Version 2.0.50727.1434
Copyright (c) Microsoft Corporation.  All rights reserved.
Assembling 'Bar.il'  to DLL --> 'Bar.dll'
Source file is ANSI

Assembled method Bar.Bar::get_SecretMessage
Assembled method Bar.Bar::.ctor
Creating PE file

Emitting classes:
Class 1:        Bar.Bar

Emitting fields and methods:
Global
Class 1 Fields: 1;      Methods: 2;
Resolving local member refs: 3 -> 3 defs, 0 refs, 0 unresolved

Emitting events and properties:
Global
Class 1 Props: 1;
Resolving local member refs: 0 -> 0 defs, 0 refs, 0 unresolved
Writing PE file
Signing file with strong name
Operation completed successfully
</pre>
<p>Open up <em>Foo</em>'s project, remove and re-add the reference to <em>Bar.dll</em>, making sure you point to the new version that you just created. <em>Foo.dll</em> will not only build, but this time it will run!</p>
<h2>Disclaimer</h2>
<p>"Hacking" third-party binaries in this manner <strong><em>may</em> breach the license agreement</strong> of those binaries. Please make sure that you are not breaking the license agreement before adopting this technique.</p>
<p>I hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/07/09/net-fu-signing-an-unsigned-assembly-without-delay-signing/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Screencast &#8211; Setting up Unity Builds</title>
		<link>http://buffered.io/2008/06/19/screencast-setting-up-unity-builds/</link>
		<comments>http://buffered.io/2008/06/19/screencast-setting-up-unity-builds/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 13:20:05 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Screencasts]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tips/Tricks]]></category>
		<category><![CDATA[screencast]]></category>
		<category><![CDATA[unity build]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=368</guid>
		<description><![CDATA[It has taken me a bit longer than expected, but I've finally got the screencast up! That's right folks, my angelic voice is now online for you all to experience. 8 minutes of Unity Build glory! Get it here: Setting up Unity Builds I made the screencast using the demo version of Camtasia (which I [...]]]></description>
			<content:encoded><![CDATA[<p>It has taken me a bit longer than expected, but I've finally got the screencast up!</p>
<p>That's right folks, my angelic voice is now online for you all to experience. 8 minutes of Unity Build glory! </p>
<p>Get it here:</p>
<div style="text-align: center;"><a href="http://oj.blackapache.net.s3.amazonaws.com/UnityBuilds.html" target="_blank" title="Setting up Unity Builds"><img src="http://buffered.io/wp-content/uploads/2008/06/unitybuild-screengrab-300x264.png" alt="View the Screencast" title="View the Screencast" width="300" height="264" /></a><br />
<em><a href="http://oj.blackapache.net.s3.amazonaws.com/UnityBuilds.html" target="_blank" title="Setting up Unity Builds">Setting up Unity Builds</a></em></div>
<p>I made the screencast using the demo version of <a href="http://www.techsmith.com/camtasia.asp" title="Camtasia">Camtasia</a> (which I give a big thumbs up to!). It weighs in at around 19MB in total, but it's well worth watching if you haven't seen a Unity Build in action before. Remember to check out <a href="http://buffered.io/2007/12/10/the-magic-of-unity-builds/" title="The Magic of Unity Builds">The Magic of Unity Builds</a> if you want some more technical information on how these builds work.</p>
<p>The project and associated source code that I used for this screencast is available for download <a href="http://buffered.io/wp-content/uploads/2008/06/win32wrap.zip" title="Old code, but it's great for the demo">right here</a>.</p>
<p>As a final note, please forgive me for sounding tired!</p>
<p>I look forward to hearing your feedback. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/06/19/screencast-setting-up-unity-builds/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How do you Interact with your ViewState?</title>
		<link>http://buffered.io/2008/05/21/how-do-you-interact-with-your-viewstate/</link>
		<comments>http://buffered.io/2008/05/21/how-do-you-interact-with-your-viewstate/#comments</comments>
		<pubDate>Wed, 21 May 2008 10:50:43 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Tips/Tricks]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[viewstate]]></category>

		<guid isPermaLink="false">http://buffered.io/?p=353</guid>
		<description><![CDATA[There comes a time in every ASP.NET developer's life when the need arises for information to be persisted into ViewState. For the sake of this post I'm not really interested in the reasons why. What I am interested in is how. How do you interact with your ViewState? I've seen two methods in use in [...]]]></description>
			<content:encoded><![CDATA[<p>There comes a time in every ASP.NET developer's life when the need arises for information to be persisted into <a href="http://msdn.microsoft.com/en-us/library/ms972976.aspx" title="ViewState">ViewState</a>. For the sake of this post I'm not really interested in the reasons why. What I am interested in is <em>how</em>.</p>
<p>How do you interact with your ViewState? </p>
<p>I've seen two methods in use in the various codebases that I've worked on. The first that I'll demonstrate is the most common form I've seen.</p>
<p>Let's pretend we have a web form which has a property called <strong>MyMagicValue</strong> which needs to be stored across <a href="http://www.xefteri.com/articles/show.cfm?id=18" title="How postback works in ASP.NET">postbacks</a>.</p>
<p>Most developers would do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #FF0000;">int</span> MyMagicValue
<span style="color: #000000;">&#123;</span>
    get
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>ViewState<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;MyMagicValue&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    set
    <span style="color: #000000;">&#123;</span>
        ViewState<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;MyMagicValue&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Information is stored directly in the ViewState hash/dictionary by accessing the ViewState property that exists on every web form/control. In this case there's a danger of things going horribly wrong "get" is invoked before "set"!</p>
<p>The second isn't so common. In fact, the only times I've seen it in a codebase is when I've implemented it myself. This leads me to believe that most of the time developers don't even know that this method exists. Here's the code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _myMagicValue<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #FF0000;">int</span> MyMagicValue
<span style="color: #000000;">&#123;</span>
    get
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> _myMagicValue<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    set
    <span style="color: #000000;">&#123;</span>
        _myMagicValue <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> SaveViewState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> state <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    state<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> _myMagicValue<span style="color: #008000;">;</span>
    state<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">SaveViewState</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> state<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> LoadViewState<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> savedState<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>savedState <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> state <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>savedState<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>state.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            _myMagicValue <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>state<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">LoadViewState</span><span style="color: #000000;">&#40;</span>state<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Quite a bit different from the first version. But which one is better? Which is considered best practice? Which has the better performance?</p>
<p>I haven't done any extensive research into this topic other than chatting to a <a href="http://secretgeek.net/" title="secretGeek">workmate</a> or two. So I wouldn't say that I have hard data to back up what I'm about to say.</p>
<p>It seems to me that the latter implementation is preferrable for the following reasons:</p>
<ol>
<li>Your page/control contains the usual definitions for container variables, just like most other classes without references to ViewState in every property definition.</li>
<li>You're not spreading references to ViewState all through your code.</li>
<li>You're not indexing into the ViewState hash for every since get/set for a given property.</li>
<li>If you want to add something else to the ViewState you can easily add it in the two overridden functions without affecting other areas of the code.</li>
<li>You don't have to worry about checking for <em>null</em> in the getters.</li>
<li>You could easily rip out the implementation and store in another class without a great deal of refactoring or code modification.</li>
</ol>
<p>The former is preferrable to many coders because:</p>
<ol>
<li>It's less code to write.</li>
<li>They don't know that the latter method exists.</li>
</ol>
<p>So what's the verdict? Which of these is better, or considered "best practice"?</p>
<p><strong>Edit:</strong> As per my comment below about using <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.pair(VS.80).aspx" title="System.Web.UI.Pair">System.Web.UI.Pair</a>, I thought I would add an example of how this should be used. It's not overly pretty when you get more and more values to store, but I still prefer it to directly manipulating the ViewState object.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> SaveViewState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> Pair<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">SaveViewState</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, _myMagicValue<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> LoadViewState<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> savedState<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>savedState <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Pair state <span style="color: #008000;">=</span> savedState <span style="color: #0600FF;">as</span> Pair<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>state <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">LoadViewState</span><span style="color: #000000;">&#40;</span>state.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            _myMagicValue <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>state.<span style="color: #0000FF;">Second</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This doesn't look so bad with just a single value, but for each value you add to the ViewState you need another Pair instance.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2008/05/21/how-do-you-interact-with-your-viewstate/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Magic of Unity Builds</title>
		<link>http://buffered.io/2007/12/10/the-magic-of-unity-builds/</link>
		<comments>http://buffered.io/2007/12/10/the-magic-of-unity-builds/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 08:03:26 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[unity builds]]></category>

		<guid isPermaLink="false">http://buffered.io/2007/12/10/the-magic-of-unity-builds/</guid>
		<description><![CDATA[I realise that as time goes by, people are using my beloved C++ less and less. .NET (C# and VB.NET) and Java seem to be taking over the mainstream coding world. Languages such as Ruby and Python seem to be taking over the scripting world. For the most part, C and C++ seem to exist [...]]]></description>
			<content:encoded><![CDATA[<p>I realise that as time goes by, people are using my beloved <a href="http://en.wikipedia.org/wiki/C++" title="C++">C++</a> less and less. <a href="http://en.wikipedia.org/wiki/.NET_Framework" title=".NET Framework">.NET</a> (<a href="http://en.wikipedia.org/wiki/C_Sharp" title="C#">C#</a> and <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET" title="VB.NET">VB.NET</a>) and <a href="http://en.wikipedia.org/wiki/Java_%28programming_language%29" title="Java">Java</a> seem to be taking over the mainstream coding world. Languages such as <a href="http://en.wikipedia.org/wiki/Ruby_%28programming_language%29" title="Ruby">Ruby</a> and <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29" title="Python">Python</a> seem to be taking over the scripting world. For the most part, C and C++ seem to exist only in the gaming/entertainment, real-time and driver worlds.</p>
<p>In many colleges and univerties C++ is no longer taught as a core subject (along with <a href="http://en.wikipedia.org/wiki/Assembly_language" title="Assembly Language">Assembly language</a>) which I find quite galling. It's a great language to learn, even if you never use it again. But the purpose of this post is not to preach the virtues and failures of the C++ language, but instead to talk about something that might aid those people who <em>are</em> using C++.</p>
<p>Anyone who's worked on a large C++ (or C) application has felt the pain of long build times. The first "big" C++ system that I worked on took 10 minutes to build. This was much bigger than anything I had experienced up until that point. Later in my career when I joined the games industry I really felt the pain of rebuilding a <em>massive</em> code base. The game took just under an hour to build from start to finish without any distributed compiling mechanism in place. When I finally got a copy of <a href="http://www.xoreax.com/" title="Xoreax Incredibuild">Incredibuild</a> the build time dropped noticably, but not to a point where recompilation was an option every time I wanted it.</p>
<p>The solution that we implement: <strong>Unity builds</strong> (which I'll refer to as UB from now on because I'm lazy).</p>
<p>Now, a lot of C and C++ that have used these before might consider the idea of a UB to be a bit of a hack. I'm not sure if I agree or disagree. There is an air of "hackyness" about it, but it solves the problem of huge compile times on most platforms with most compilers.</p>
<p>Before I go into the definition, and give instructions on how to set up a UB, let me just state for the record that this isn't designed to replace normal builds for releasing code. The idea behind these is to dramatically reduce the build times for developers who are spending 8 hours a day modifying the code. Fixing bugs and adding enhancements generally means that a developer is constantly recompiling. Every time you compile, you have to wait (or perhaps <a href="http://xkcd.com/303/" title="Compiling">entertain yourself in some way</a>), and in waiting you are wasting precious time. Wasting time means a reduction in productivity. This can only be a bad thing. It's bad enough with short build times, but with long build times it becomes a huge problem. So please bear in mind that the normal release and product builds, builds that happen on automated build servers, etc, do <em>not</em> need to have a UB in place, they can continue to use normal builds.</p>
<p>The principle behind a UB is quite simple. It's all about reducing...
<ol>
<li>... the number of times a file is opened.</li>
<li>... the number of files that are opened.</li>
</ol>
<p>In esssence, we're going to abuse the power of the <a href="http://en.wikipedia.org/wiki/Preprocessor" title="Preprocessor">preprocessor</a> to do the above.</p>
<p>Every source file (.c, .cxx, .cc, .cpp, etc) that is compiled results in the creation of an <a href="http://en.wikipedia.org/wiki/Object_files" title="Object File">object file</a>. When all of the source files are compiled into object files, the <a href="http://en.wikipedia.org/wiki/Linker" title="Linker">linker</a> then collects up all these object files and links them together into an executable application. For those of you who don't know: reading/writing to/from disk is the slowest thing you can do on a computer. Actually, it isn't so much the reading and writing, it's more the <a href="http://en.wikipedia.org/wiki/Seeking" title="Seeking">seeking</a>. But the point remains the same. Disks are the only core parts of your system that have moving parts, so they're going to be slow! So if you want to speed things up, a good place to start is reducing I/O.</p>
<p>I don't want to turn this into a lesson on how C and C++ programs are compiled, so I'm going to cut to the chase. We know that the preprocessor parses each file before it's compiled which means that each file is going to be opened. When those parsed source files are then compiled, they're essentially opened again. Each of those files requires another seek. When the object file is generated, that's another seek. When the file is linked, that's another seek. There's lots of disk activity going on here!</p>
<p>If you haven't caught on yet, let me give you another hint. Each source file results in a couple of new files and a stack of seeks. We also have a neat preprocessor statement at our disposal (<em>*cough*</em> <a href="http://en.wikipedia.org/wiki/Header_file" title="Header File">include</a> <em>*cough*</em>).</p>
<p>That's right guys, we don't actually compile every file individually. We instead create a master file (or set of master files, depending on how big your source base is), which <em>includes</em> all of the other source files. This file (or set of files) is compiled instead.</p>
<p><a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-01.png" rel="lightbox[unitybuild]" title="Sample Project"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-01.thumbnail.png" class="InlineImageLeft" alt="Sample Project" /></a>To make it clear I'm going to show you the principle in action on a fake project in Visual Studio 2008. However, the principle can be applied regardless of the compiler and platform. Check out the screenshot on the left. This shows a standard C++ with a few files in it. Ordinarily each of those files will be compiled individually, and hence slowly.</p>
<p>The first step in creating a UB is to make a new project configuration which you can play with. Doing so will allow you to set up an area you can modify without breaking the "normal" project which will continue to be built as usual. So let's do that now. Create a new project configuration based on the debug build. See the following screenies:<br />
<a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-02.png" rel="lightbox[unitybuild]" title="Configuration Manager - New Config"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-02.thumbnail.png" class="InlineImageLeft" alt="Configuration Manager - New Config" /></a><br />
<a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-03.png" rel="lightbox[unitybuild]" title="New Solution Configuration"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-03.thumbnail.png" class="InlineImageLeft" alt="New Solution Configuration" /></a><br />
<a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-04.png" rel="lightbox[unitybuild]" title="Configuration Manager - Config Added and Selected"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-04.thumbnail.png" class="InlineImageLeft" alt="Configuration Manager - Config Added and Selected" /></a></p>
<div style="float: none; clear: both;"></div>
<p><a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-05.png" rel="lightbox[unitybuild]" title="Build Toolbar - Config Selected"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-05.thumbnail.png" class="InlineImageRight" alt="Build Toolbar - Config Selected" /></a>When you've created the new configuration, make sure you have it set as the active configuration as shown on the right.</p>
<p><a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-06.png" rel="lightbox[unitybuild]" title="Solution with Unity Build File"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-06.thumbnail.png" class="InlineImageLeft" alt="Solution with Unity Build File" /></a>Then, you need to add a new file which will be the master UB file. I usually create a sub-folder in the project and add the file there. Both folder and file I tend to call UnityBuild.</p>
<p><a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-07.png" rel="lightbox[unitybuild]" title="Unity Build File Contents"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-07.thumbnail.png" class="InlineImageRight" alt="Unity Build File Contents" /></a>When the file is created, the first thing we need to do is make sure that we edit the UnityBuild.cpp so that it includes <em>all</em> of the other files in the project. Check out the image for the example.</p>
<p>If we attempted to compile at this point, we have all kinds of issues. Everything right now is being included twice. So we need to fix that so that we don't have issues in <em>every</em> configuration. Let's start by fixing up the Unity build project. Select all of the source files <em>except</em> UnityBuild.cpp, and <strong>exclude them</strong> from the project (right-click on them to see the properties). Check out the pics below to see how it's done.<br />
<a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-08.png" rel="lightbox[unitybuild]" title="Files selected"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-08.thumbnail.png" class="InlineImageLeft" alt="Files selected" /></a><br />
<a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-09.png" rel="lightbox[unitybuild]" title="Exclude from build"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-09.thumbnail.png" class="InlineImageLeft" alt="Exclude from build" /></a><br />
<a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-10.png" rel="lightbox[unitybuild]" title="Solution with files excluded"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-10.thumbnail.png" class="InlineImageLeft" alt="Solution with files excluded" /></a></p>
<div style="float: none; clear: both;"></div>
<p>So that this point you've excluded the files from the UnityDebug configuration, and hence the unity configuration <em>should</em> build.</p>
<p><strong>Note:</strong> if you're implementing this in a large existing source base you may find that the UB doesn't actually compile. You need to bear in mind that the preprocessor is making <strong>one large file</strong> out of all of your source files. Which means global variables, static variables, global functions, etc that appear more than once in the source code will now tread all over each other. Technically you shouldn't have this problem if you're writing "proper" code <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> But the harsh reality is that lots of devs copy and paste code, duplicate function names, and all kinds of other horrible things. To get it working, you're going to have to do a bit of housekeeping! But since this is going to affect the source in a positive way, it can only be a good thing!</p>
<p><a href="http://buffered.io/wp-content/uploads/2007/12/unitybuild-11.png" rel="lightbox[unitybuild]" title="Unity build file excluded"><img src="http://buffered.io/wp-content/uploads/2007/12/unitybuild-11.thumbnail.png" class="InlineImageLeft" alt="Unity build file excluded" /></a>OK, next up let's get the other builds working again. You need to exclude the UnityBuild.cpp file from the build in both the Debug and Release builds (not the UnityDebug configuration). I won't show you how to do that, as it should be obvious. When you're done, you should see similar to the image on the left when you use the Debug or Release configurations from this point on.</p>
<p>You're done! The old builds should work just fine because your UB file isn't included in the configuration. Once you've sorted out the code duplication issues with the UB configuration, you should have a perfectly working configuration.</p>
<p>You may find that the whole process is much easier if you're building from the command line, especially using <a href="http://en.wikipedia.org/wiki/Make_%28software%29" title="make">make</a> on <a href="http://en.wikipedia.org/wiki/%2Anix" title="Unix-like">*nix</a> machines, as you don't have to worry about excluding things from configurations. Instead, you just simply need to pass in a single file to the compiler - the UB file!</p>
<p>So after all that, what kind of improvements should expect to find? Well let me give you a few stats. When the UB was implemented at the game company I worked for, the build time dropped from 55 minutes to <strong>just over 6 minutes</strong>. When I implemented UB in a previous job the build time dropped from 10 minutes to <strong>less than 3 minutes</strong>. When I put them in place at home the build times dropped on average by <strong>60%</strong>.</p>
<p>I'd be very surprised if you didn't notice a huge improvement in your build times if you use this mechanism.</p>
<p>Before I finish up, I need to highlight a few issues around housekeeping. As already stated, these builds are not used to replace the normal compilations. They should sit alongside to make it faster for the developer to do his or her work. This means that you need to make sure that both types of builds are <em>working all the time</em>. Keep your automatic builds using the normal build type, and you'll soon find out if you've not been maintaining your configurations properly.</p>
<p>Make sure that every time you add a new source file you <strong>exclude it from the UB configuration</strong>, and that you <strong>add it to the content of the UnityBuild.cpp file</strong> otherwise it won't be compiled and included in the build properly.</p>
<p>On the whole, UBs have really improved the speed in which I've been able to do things. My wait time for compilations is generally a third of what it was. That, in my view, is worth the "hack" <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for reading!</p>
<p>PS. Please let me know of any typos, grammar errors, etc. It was a hefty post to put together and I might have missed a couple of obvious stuff-ups!</p>
<p><em>EDIT: I have fixed up a few grammar and spelling errors. Thank you Yoann and Bryce <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </em></p>
<hr/>
<strong>This post is a bit old, and some of the images are missing. While it's still got a valid description of Unity Builds it's not really complete without the piccies. So I've created a full screencast and posted that <a href="http://buffered.io/2008/06/19/screencast-setting-up-unity-builds/" title="Screencast - Setting up Unity Builds">over here</a> to clear up any confusion. Cheers!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2007/12/10/the-magic-of-unity-builds/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>WTF: Random Memory Contents</title>
		<link>http://buffered.io/2007/09/03/wtf-random-memory-contents/</link>
		<comments>http://buffered.io/2007/09/03/wtf-random-memory-contents/#comments</comments>
		<pubDate>Mon, 03 Sep 2007 05:40:45 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WTF]]></category>

		<guid isPermaLink="false">http://buffered.io/2007/09/03/wtf-random-memory-contents/</guid>
		<description><![CDATA[If any of you out there are able to give me ONE GOOD REASON why anyone would do something like this, then please let me know. Below are "customised" realloc() and malloc() I recently stumbled across (yes, they get called. A LOT): void *mcRealloc&#40; void *P, int SIZE &#41; &#123; int oldSize = _msize&#40; P [...]]]></description>
			<content:encoded><![CDATA[<p>If any of you out there are able to give me <strong>ONE GOOD REASON</strong> why <em>anyone</em> would do something like this, then please let me know. Below are "customised" realloc() and malloc() I recently stumbled across (yes, they get called. A <strong>LOT</strong>):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>mcRealloc<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>P, <span style="color: #0000ff;">int</span> SIZE <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> oldSize <span style="color: #000080;">=</span> _msize<span style="color: #008000;">&#40;</span> P <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  P <span style="color: #000080;">=</span> <span style="color: #0000dd;">realloc</span><span style="color: #008000;">&#40;</span> P, SIZE <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> P <span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> oldSize<span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> P<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span> <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> P<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>mcMalloc<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> SIZE <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span>P<span style="color: #008080;">;</span>
&nbsp;
  P <span style="color: #000080;">=</span> <span style="color: #0000dd;">malloc</span><span style="color: #008000;">&#40;</span> SIZE <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> P <span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> P<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #008000;">&#41;</span> <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> P<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Is it just me, or is this a <strong>huge</strong> WTF?</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2007/09/03/wtf-random-memory-contents/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Safer Code through Object-Orientation</title>
		<link>http://buffered.io/2007/08/09/safer-code-through-object-orientation/</link>
		<comments>http://buffered.io/2007/08/09/safer-code-through-object-orientation/#comments</comments>
		<pubDate>Thu, 09 Aug 2007 08:55:21 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://buffered.io/2007/08/09/safer-code-through-object-orientation/</guid>
		<description><![CDATA[In my current position I spend a lot of time battling against a fairly poorly-written C++ code base. The code, while technically written in C++, is actually more of a C-like "splat" with a few classes thrown in. Since I began working on this project I've seen many cases where proper object-orientation would have made [...]]]></description>
			<content:encoded><![CDATA[<p>In my current position I spend a lot of time battling against a fairly poorly-written C++ code base. The code, while <em>technically</em> written in C++, is actually more of a C-like "splat" with a few classes thrown in. Since I began working on this project I've seen many cases where proper <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object-oriented programming">object-orientation</a> would have made a drastic improvement to the quality of the code.</p>
<p>And it's these cases which are the inspiration for this blog post. </p>
<p>Objects are not only good for encapsulation and making more readable/maintainable code. They can also be used to improve <em>code safety</em>. Rather than try and explain through words what I mean by 'code safety', let's work through a couple of examples.</p>
<h3>Example 1: Data Integrity in a Multithreaded Application</h3>
<p>Let's say that we're working on a <a href="http://en.wikipedia.org/wiki/Thread_(computer_science)" title="Threading">multithreaded</a> application. We have a set of data that is constantly read and written across the different threads. We obviously don't want to have different threads writing to the dataset at the same time, as this may cause all kinds of issues with the data's integrity. We want to put a mechanism in place to ensure that the data's integrity is never put at risk. We can do this simply by 'locking' the sections of code that perform the data writes via a synchronisation technique such as a <a href="http://en.wikipedia.org/wiki/Critical_section" title="Critical section">critical section</a>, a <a href="http://en.wikipedia.org/wiki/Semaphore_(programming)" title="Semaphore">semaphore</a> or a <a href="http://en.wikipedia.org/wiki/Mutex" title="Mutex">mutex</a>. For the sake of this example, we'll use a critical section. We'll wrap it up in an object for easy use. Let's pretend that we have a C++ critical section class that looks like this:[source:cpp]class CriticalSection<br />
{<br />
public:<br />
  CriticalSection();<br />
  ~CriticalSection();</p>
<p>  bool Lock();<br />
  bool Unlock();</p>
<p>  // ...<br />
};[/source]<br />
To use the critical section object, all we need to do is this:[source:cpp]CriticalSection importantDataLocker;<br />
// ...<br />
if( importantDataLocker.Lock() )<br />
{<br />
  // do stuff to important data<br />
  // ...</p>
<p>  importantDataLocker.Unlock();<br />
}[/source]<br />
Easy enough isn't it! We'll use this code in our data writer object, so that we can make sure that all of our threads are playing nicely while writing the data. During the course of our data writing, we might want to do some other processing that is not related to writing data. Even though this task isn't related to writing the data out, we still retain the lock so that we can finish off the job after without having to unlock and relock. So we may have some code that looks like this:<br />
[source:cpp]bool DataWriter::WriteFunkyData( FunkyData* data )<br />
{<br />
  if( m_dataLocker.Lock() )<br />
  {<br />
    WriteABitOfData( data );<br />
    DoSomeProcessing( data );<br />
    WriteSomeMoreData( data );<br />
    // ...<br />
    m_dataLocker.Unlock();<br />
    return true;<br />
  }<br />
  return false;<br />
}[/source]<br />
Not really rocket science is it? The above code makes sure that all the code between <em>Lock()</em> and <em>Unlock()</em> is only run when no other threads are requesting a lock (or own a lock) on the critical section.</p>
<p>Unfortunately we have a problem. In a non-perfect world (like the one we live in) there's always a chance that somewhere between the call to <em>Lock()</em> and the call to <em>Unlock()</em> an error might occur. An exception may be thrown. The code might just <em>return;</em>. If that's the case, the critical section will <strong>not be unlocked</strong> because the <em>Unlock()</em> function won't be called before the function finishes.</p>
<p>The result? A critical section that's permanently locked, and all future <em>Lock()</em> requests will either hang or fail. This is not ideal. If we were using system-wide mutexes we'd do some serious damage! Cleary this isn't acceptable.</p>
<p>Most <a href="http://en.wikipedia.org/wiki/Procedural_programming" title="Procedural programming">procedural</a> programmers will adjust the code in the following manner:<br />
[source:cpp]bool DataWriter::WriteFunkyData( FunkyData* data )<br />
{<br />
  if( m_dataLocker.Lock() )<br />
  {<br />
    if( !WriteABitOfData( data ) )<br />
    {<br />
      m_dataLocker.Unlock();<br />
      return false;<br />
    }</p>
<p>    try<br />
    {<br />
      DoSomeProcessing( data );<br />
    }<br />
    catch(...)<br />
    {<br />
      m_dataLocker.Unlock();<br />
      return false;<br />
    }</p>
<p>    if( !WriteSomeMoreData( data ) )<br />
    {<br />
      m_dataLocker.Unlock();<br />
      return false;<br />
    }<br />
    // ...<br />
    m_dataLocker.Unlock();<br />
    return true;<br />
  }<br />
  return false;<br />
}[/source]<br />
Some of you might be laughing at the above code. And rightly so! But don't think that it's uncommon because it's <strong>not</strong>. As you can imagine, the code gets bigger, nastier and tougher to maintain. As the function increases in size, code duplication increases and the chance of making mistakes increases. What if a new maintenance coder decides to early-out without unlocking? You're still stuck with the same problem.</p>
<p>Further use of object-orientation is what's required to solve this problem. Let's use and abuse a couple of the core features of objects: <a href="http://en.wikipedia.org/wiki/Constructor_%28computer_science%29" title="Constructor">construction</a> and <a href="http://en.wikipedia.org/wiki/Destructor_%28computer_science%29" title="Destructor">destruction</a>:
<ul>
<li>When an object is instantiated, it's <em>constructor</em> is called.</li>
<li>When an object is destroyed, it's <em>destructor</em> is called</li>
</ul>
<p>For those of you who don't know, objects are destroyed in two ways:
<ol>
<li>When they go out of scope.</li>
<li>When they are <a href="http://en.wikipedia.org/wiki/Operator_delete" title="Operator delete">deleted</a>.</li>
</ol>
<p>Each function in C++ has it's own scope. As soon as the function is finished, the scope is no longer valid and any local variables are cleaned up. This happens when ...
<ul>
<li>... program execution reaches the end of the function.</li>
<li>... a <em>return</em> statement is reached.</li>
<li>... a exception is thrown that is not caught.</li>
</ul>
<p>If there was an object that was able to lock the critical section when it's constructed and unlock the same critical section when it's destroyed, we could use it to make sure that the critical section is unlocked <strong>regardless</strong> of the way in which the function is exited. That was a bit of a mouthful, so to demonstrate the point have a look at the following code:[source:cpp]class CriticalSectionLocker<br />
{<br />
public:<br />
  CriticalSectionLocker( CriticalSection&#038; cs )<br />
  : m_criticalSection( cs )<br />
  {<br />
    m_isLocked = m_criticalSection.Lock();<br />
  }<br />
  ~CriticalSectionLocker()<br />
  {<br />
    if( m_isLocked )<br />
    {<br />
      m_criticalSection.Unlock();<br />
    }<br />
  }<br />
  bool IsLocked()<br />
  {<br />
    return m_isLocked;<br />
  }<br />
private:<br />
  bool m_isLocked;<br />
  CriticalSection&#038; m_criticalSection;<br />
};[/source]<br />
If we use this object instead of the verbose and repetitive method shown above, our code would look like this:<br />
[source:cpp]bool DataWriter::WriteFunkyData( FunkyData* data )<br />
{<br />
  CriticalSectionLocker csl( m_dataLocker );<br />
  if( csl.IsLocked() )<br />
  {<br />
    if( !WriteABitOfData( data ) )<br />
    {<br />
      return false;<br />
    }</p>
<p>    DoSomeProcessing( data );</p>
<p>    if( !WriteSomeMoreData( data ) )<br />
    {<br />
      return false;<br />
    }<br />
    // ...<br />
    return true;<br />
  }</p>
<p>  return false;<br />
}[/source]<br />
This code is substantially easier to maintain. As soon as <em>csl</em> goes out of scope, the critical section will be unlocked automatically via the destructor. This will happen on early-outs <em>and</em> when exceptions are thrown. Job done!</p>
<h3>Example 2: Flag/Value Toggling</h3>
<p>Developers have been known to write code which relies on object state to function differently. While this might not be considered 'best practice', it's fairly common out in the wild. There are times where a developer may want to temporarily toggle a flag, or change the value of a variable, and change it back before the rest of the code is run. Here's an example (it's not real-life, but it should give you the idea):[source:cpp]void Person::GotToParty( NightClub* nc )<br />
{<br />
  if( !nc->AllowsEntry( this ) )<br />
  {<br />
    // temporarily pretend we're older<br />
    // so we can go clubbing!<br />
    int backupAge = m_Age;<br />
    m_Age = 25;</p>
<p>    if( nc->AllowsEntry( this ) )<br />
    {<br />
      this->Party( nc, HARD_BUT_SUBTLE );<br />
    }</p>
<p>    // best restore our age now<br />
    m_Age = backupAge;<br />
  }<br />
  else<br />
  {<br />
    this->Party( nc, HARD );<br />
  }<br />
}[/source]<br />
Don't laugh, we've all been there! <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>It should be obvious that the code above suffers from the same shortcomings as the previous example. It's not resiliant against early-outs or exceptions. A similar type of construct/destruct mechanism can be used to reset the original value of a variable in a much safer way. An example class might look like this:[source:cpp]template< typename T ><br />
class ValueToggler<br />
{<br />
public:<br />
  ValueToggler( T&#038; variableToToggle, T tempValue )<br />
  : m_toggleVar( variableToToggle ),<br />
    m_originalValue( variableToToggle )<br />
  {<br />
    m_toggleVar = tempValue;<br />
  }</p>
<p>  ~ValueToggler()<br />
  {<br />
    m_togglerVar = m_originalValue;<br />
  }<br />
private:<br />
  T&#038; m_toggleVar;<br />
  T m_originalValue;<br />
};[/source]<br />
The object above is templated so that it can be used with a variety of types (eg. int, double, float). Using an object such as this, the example program code would change to the following:[source:cpp]void Person::GotToParty( NightClub* nc )<br />
{<br />
  if( !nc->AllowsEntry( this ) )<br />
  {<br />
    ValueToggler<int> vt( m_Age, 25 );</p>
<p>    if( nc->AllowsEntry( this ) )<br />
    {<br />
      this->Party( nc, HARD_BUT_SUBTLE );<br />
    }<br />
  }<br />
  else<br />
  {<br />
    this->Party( nc, HARD );<br />
  }<br />
}[/source]<br />
Again, the code is smaller and a lot more bulletproof. The value of <em>m_Age</em> will be restored no matter what happens inside the function.</p>
<h3>Conclusion and Final Thoughts</h3>
<p>The above samples should show you how easy it is to use objects to help you write more defensive code. Though some people (usually the procedural programmers <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_wink.png' alt=';)' class='wp-smiley' /> ) would say that this kind of code is a hack, it's my view that they're wrong. The code is clear, easy to maintain, and very safe.</p>
<p><em>Disclaimer:</em> The code above hasn't been compiled, so it may contain errors. The point of the post is to show you the idea behind using objects for safety. Ideal implementations are beyond the scope of the article.</p>
<p>Thoughts and feedback are always welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2007/08/09/safer-code-through-object-orientation/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Creating Concrete Objects</title>
		<link>http://buffered.io/2007/07/14/creating-concrete-objects/</link>
		<comments>http://buffered.io/2007/07/14/creating-concrete-objects/#comments</comments>
		<pubDate>Sat, 14 Jul 2007 10:21:05 +0000</pubDate>
		<dc:creator>OJ</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://buffered.io/2007/07/14/creating-concrete-objects/</guid>
		<description><![CDATA[Being a fan of OOP, I tend to write a lot of object-oriented code. Coming up with a meaningful object model that behaves in an appropriate way is just as important as having a meaningful interface to your objects. A concrete object is an object that actually behaves in the manner you'd expect without any [...]]]></description>
			<content:encoded><![CDATA[<p>Being a fan of <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object-oriented Programming">OOP</a>, I tend to write a lot of object-oriented code. Coming up with a meaningful object model that behaves in an appropriate way is just as important as having a meaningful interface to your objects. A concrete object is an object that actually behaves in the manner you'd expect without any wierd side-effects, and has the same kind of attributes that you'd expect of a primitive data type.</p>
<p>Creating concrete data objects/classes is a good thing to do, as it reduces the probability of bugs, and crazy side-effects. It's also an important first step in writing intuitive code - which will be the topic of a later blog post. </p>
<p>I'd like to quote one of my lecturers that I learned from during my time at <a href="http://www.uts.edu.au/" title="University of Technology, Sydney">UTS</a>...<br />
<blockquote>
<p>If in doubt, do as the ints do.</p>
</blockquote>
<p>I realise the meaning of this point isn't obvious on the surface, but with some example code it'll all become clear.</p>
<h3>An example concrete data type - a 3D Vector class</h3>
<p>Let's say you're writing some code to do some 3D rendering, and you're in need of a class that can handle the functionality and behaviour of Vectors in 3D space. The first and most obvious thing that you need to handle are x, y and z coordinates. Let's start with a very basic Vector3 class:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #0000ff;">float</span> X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> Z<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>At this point you've got an object that gives public access to its internal workings. Can we say that's what the int datatype does? Are you able to mess around with its inner workings? No, you're not. Not just that, but the idea of public member variables breaks the whole notion of <a href="http://en.wikipedia.org/wiki/Information_hiding" title="Information Hiding">encapsulation/information hiding</a>. We need to hide the internal workings, but in doing so we remove the ability to set and get the values on the object. We need to expose some functions that will allow us to do that. Our improved version of the code might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This is an improvement, as we now have control over the inner workings of the object without exposing the implementation to external classes. Now let's say that we want to be able to construct a new Vector3 object through a variety of ways, ie. in exactly the same way we can with int. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// this is what we can do with ints:</span>
<span style="color: #0000ff;">int</span> a<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> b <span style="color: #000080;">=</span> a<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> c<span style="color: #008000;">&#40;</span> b <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> d, e<span style="color: #008080;">;</span>
d <span style="color: #000080;">=</span> e <span style="color: #000080;">=</span> c<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// we want do the same kind of things with our</span>
<span style="color: #666666;">// own class</span>
Vector3 a<span style="color: #008000;">&#40;</span> <span style="color:#800080;">1.0</span>, <span style="color:#800080;">0.0</span>, <span style="color: #000040;">-</span><span style="color:#800080;">1.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
Vector3 b <span style="color: #000080;">=</span> a<span style="color: #008080;">;</span>
Vector3 c<span style="color: #008000;">&#40;</span> b <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
Vector3 d, e<span style="color: #008080;">;</span>
d <span style="color: #000080;">=</span> e <span style="color: #000080;">=</span> c<span style="color: #008080;">;</span></pre></div></div>

<p>We need to expose some options for construction and assignment. So our next iteration might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// construction - default to zero if nothing is passed in</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Vector3<span style="color: #008000;">&#40;</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// assigment operator</span>
  <span style="color: #0000ff;">void</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The above interface should do what we need it to... or should it? A more careful examination will reveal that it doesn't actually behave exactly as you'd expect. The copy constructor takes a reference to another Vector3, which <em>could be modified</em> inside the copy constructor. We have the same issue with our assignment operator. Integers do not behave this way, so we need to modify our interface a bit more to tidy it up:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// construction - default to zero if nothing is passed in</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// assigment operator</span>
  <span style="color: #0000ff;">void</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>We've now made sure that the Vector3 class does not modify anything that doesn't belong to it, which again is how the integers behave. But there is still something missing. The assignment operator doesn't allow for chaining (eg. int a = b = c = d;) just like the ints do, so we need to make a slight adjustment to the overload:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// return a const reference to ourself</span>
<span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>There, much better.</p>
<p>We're now at the stage where we want to be able to add/subtract/multiply/divide vectors together, but before we start overloading the operators we should look at the way integers behave when they go through the same operations:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> a, b, c, d<span style="color: #008080;">;</span>
a <span style="color: #000080;">=</span> b<span style="color: #008080;">;</span> <span style="color: #666666;">// b doesn't change, a does.</span>
a <span style="color: #000040;">*</span> b<span style="color: #008080;">;</span> <span style="color: #666666;">// both a and b do not change.</span>
a <span style="color: #000080;">=</span> b <span style="color: #000040;">*</span> c<span style="color: #008080;">;</span> <span style="color: #666666;">// both b and c do not change, but a does.</span>
a <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> b <span style="color: #000040;">+</span> c <span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> d<span style="color: #008080;">;</span> <span style="color: #666666;">// a is the only thing that changes</span>
a <span style="color: #000040;">+</span><span style="color: #000080;">=</span> b <span style="color: #000040;">/</span> c<span style="color: #008080;">;</span> <span style="color: #666666;">// again, a is the only thing that changes.</span></pre></div></div>

<p>It's obvious from this that when we overload the operators, we only modify the content of the object if it is on the left hand side of one of the assignment operators. Our interface should make this obvious:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// construction - default to zero if nothing is passed in</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// assigment operators - not const because the current object</span>
  <span style="color: #666666;">// needs to change</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">/</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// other operators - all const functions to make sure that the</span>
  <span style="color: #666666;">// internal state of the object doesn't get modified when the</span>
  <span style="color: #666666;">// function is called. Separate temporary instances of Vector3</span>
  <span style="color: #666666;">// objects are created and  returned when executed.</span>
  Vector3 operator<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 + v2</span>
  Vector3 operator<span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 - v2</span>
  Vector3 operator<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 * v2</span>
  Vector3 operator<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 / v2</span>
&nbsp;
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The class is starting to take shape, but there are some other functions missing that are an integral part of any Vector class: normalise(), dot() and cross().</p>
<p>The normalise function is used to create a <a href="http://en.wikipedia.org/wiki/Unit_vector" title="Unit Vector">unit vector</a> (ie. a vector with a length of 1.0). But the question here is: when calling the function, should the object be modified, or should it return a copy of the Vector with a unit length? Let's look at the difference in function signatures:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// this function would modify the object directly</span>
<span style="color: #0000ff;">void</span> Normalise<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// this function would return a new vector that is normalised</span>
Vector3 Normalise<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span></pre></div></div>

<p>Making a decision like this can be a bit of a pain in the butt, but we're fortunate in this case because we can deduce what should be done! Generally when dealing with normalised vectors, we tend to retain a reference to the normal while dealing with a stack of other vectors. So another instance of a vector is used alongside the other vectors. Let's look at how this might be done with both above functions if we had a vector that we wanted to reuse, but get a normalise version of at the same time:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// this is the vector we want to keep as is, but need a</span>
<span style="color: #666666;">// normalised copy of</span>
Vector3 someVector<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// here's how we'd do it with the first option:</span>
Vector3 normal1 <span style="color: #000080;">=</span> someVector<span style="color: #008080;">;</span>
normal1.<span style="color: #007788;">Normalise</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// here's how we'd do it with the second option:</span>
Vector3 normal2 <span style="color: #000080;">=</span> someVector.<span style="color: #007788;">Normalise</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>In my view, the second option is easier to read, and is a bit more intuitive. Not just that, but it's less code! So based on this, we'll utilise the second version of the function. Our class now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// construction - default to zero if nothing is passed in</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// assigment operators - not const because the current object</span>
  <span style="color: #666666;">// needs to change</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">/</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// other operators - all const functions to make sure that the</span>
  <span style="color: #666666;">// internal state of the object doesn't get modified when the</span>
  <span style="color: #666666;">// function is called. Separate temporary instances of Vector3</span>
  <span style="color: #666666;">// objects are created and  returned when executed.</span>
  Vector3 operator<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 + v2</span>
  Vector3 operator<span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 - v2</span>
  Vector3 operator<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 * v2</span>
  Vector3 operator<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 / v2</span>
&nbsp;
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// helpers</span>
  Vector3 Normalise<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>So, next up we need the dot() function, which gives us the <a href="http://en.wikipedia.org/wiki/Dot_product" title="Dot Product">dot product</a> of two vectors. The dot product is a single value which represents the angle between the two vectors, so that's what the function should return. Since the dot product requires two vectors, it would make sense for the function to be given a reference to the secont vector that is part of the dot product equation. <strong>None</strong> of the objects should be modified at all during the course of the function, so we should make that obvious in the function signature. Our class now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// construction - default to zero if nothing is passed in</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// assigment operators - not const because the current object</span>
  <span style="color: #666666;">// needs to change</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">/</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// other operators - all const functions to make sure that the</span>
  <span style="color: #666666;">// internal state of the object doesn't get modified when the</span>
  <span style="color: #666666;">// function is called. Separate temporary instances of Vector3</span>
  <span style="color: #666666;">// objects are created and  returned when executed.</span>
  Vector3 operator<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 + v2</span>
  Vector3 operator<span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 - v2</span>
  Vector3 operator<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 * v2</span>
  Vector3 operator<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 / v2</span>
&nbsp;
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// helpers</span>
  Vector3 Normalise<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> Dot<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Note that the function is marked as const to imply that the object will <em>not</em> have a different state when it's called, and the parameter is a const reference to imply that the parameter will not be modified as well.</p>
<p>Finally, we need a function which will determine the <a href="http://en.wikipedia.org/wiki/Cross_product" title="Cross Product">cross product</a> of two vectors (commonly used to determine the vector that is perpendicular to two input vectors. The cross product equation takes two vectors and results in another vector, which again implies that the two input vectors do not change. Based on this implication, our function signature should be easy to deduce. While we're there, let's just chuck in a cheeky length() function which will give us the magnitude of the vector. Our class now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// construction - default to zero if nothing is passed in</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// assigment operators - not const because the current object</span>
  <span style="color: #666666;">// needs to change</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">/</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// other operators - all const functions to make sure that the</span>
  <span style="color: #666666;">// internal state of the object doesn't get modified when the</span>
  <span style="color: #666666;">// function is called. Separate temporary instances of Vector3</span>
  <span style="color: #666666;">// objects are created and  returned when executed.</span>
  Vector3 operator<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 + v2</span>
  Vector3 operator<span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 - v2</span>
  Vector3 operator<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 * v2</span>
  Vector3 operator<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 / v2</span>
&nbsp;
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// helpers</span>
  Vector3 Normalise<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> Dot<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> Length<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  Vector3 Cross<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The interface to our class now looks fine.. except for one small omission. Const objects are read-only objects, and hence reading values from a const object should be legal. On the flip side, when we read a given x, y, or z value from a vector the vector shouldn't be modified at all. So with that in mind, we should mark each of the getter function as const as well:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Vector3
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// change the variable names to make it obvious that</span>
  <span style="color: #666666;">// they are member variables, not local or global.</span>
  <span style="color: #0000ff;">float</span> m_X<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Y<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> m_Z<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  <span style="color: #666666;">// construction - default to zero if nothing is passed in</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> y <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span>, <span style="color: #0000ff;">float</span> z <span style="color: #000080;">=</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  Vector3<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// assigment operators - not const because the current object</span>
  <span style="color: #666666;">// needs to change</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">/</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// other operators - all const functions to make sure that the</span>
  <span style="color: #666666;">// internal state of the object doesn't get modified when the</span>
  <span style="color: #666666;">// function is called. Separate temporary instances of Vector3</span>
  <span style="color: #666666;">// objects are created and  returned when executed.</span>
  Vector3 operator<span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 + v2</span>
  Vector3 operator<span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 - v2</span>
  Vector3 operator<span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 * v2</span>
  Vector3 operator<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span> <span style="color: #666666;">// v1 / v2</span>
&nbsp;
  <span style="color: #666666;">// getters</span>
  <span style="color: #0000ff;">float</span> GetX<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetY<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">float</span> GetZ<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// setters</span>
  <span style="color: #0000ff;">void</span> SetX<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetY<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">void</span> SetZ<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #666666;">// helpers</span>
  Vector3 Normalise<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> Dot<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> Length<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
  Vector3 Cross<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>We're done! Our class looks complete (enough), so let's pump out a definition for the functions (just for the sake of clarity).</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Vector3<span style="color: #008080;">::</span><span style="color: #007788;">Vector3</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x, <span style="color: #0000ff;">float</span> y, <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span>
<span style="color: #008080;">:</span> m_X<span style="color: #008000;">&#40;</span> x <span style="color: #008000;">&#41;</span>,
  m_Y<span style="color: #008000;">&#40;</span> y <span style="color: #008000;">&#41;</span>,
  m_Z<span style="color: #008000;">&#40;</span> z <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// don't need to do anything in the body of the</span>
  <span style="color: #666666;">// function because we're using the initialisation</span>
  <span style="color: #666666;">// lists instaed.</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Vector3<span style="color: #008080;">::</span><span style="color: #007788;">Vector3</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span>
<span style="color: #008080;">:</span> m_X<span style="color: #008000;">&#40;</span> v.<span style="color: #007788;">m_X</span> <span style="color: #008000;">&#41;</span>,
  m_Y<span style="color: #008000;">&#40;</span> v.<span style="color: #007788;">m_Y</span> <span style="color: #008000;">&#41;</span>,
  m_Z<span style="color: #008000;">&#40;</span> v.<span style="color: #007788;">m_Z</span> <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// don't need to do anything in the body of the</span>
  <span style="color: #666666;">// function because we're using the initialisation</span>
  <span style="color: #666666;">// lists instaed.</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// check for self-assignment</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">this</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>v <span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    m_X <span style="color: #000080;">=</span> v.<span style="color: #007788;">m_X</span><span style="color: #008080;">;</span>
    m_Y <span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Y</span><span style="color: #008080;">;</span>
    m_Z <span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Z</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #666666;">// return a reference to ourselves for chaining</span>
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  m_X <span style="color: #000040;">+</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_X</span><span style="color: #008080;">;</span>
  m_Y <span style="color: #000040;">+</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Y</span><span style="color: #008080;">;</span>
  m_Z <span style="color: #000040;">+</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Z</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  m_X <span style="color: #000040;">-</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_X</span><span style="color: #008080;">;</span>
  m_Y <span style="color: #000040;">-</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Y</span><span style="color: #008080;">;</span>
  m_Z <span style="color: #000040;">-</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Z</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">*</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  m_X <span style="color: #000040;">*</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_X</span><span style="color: #008080;">;</span>
  m_Y <span style="color: #000040;">*</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Y</span><span style="color: #008080;">;</span>
  m_Z <span style="color: #000040;">*</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Z</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">/</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  m_X <span style="color: #000040;">/</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_X</span><span style="color: #008080;">;</span>
  m_Y <span style="color: #000040;">/</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Y</span><span style="color: #008080;">;</span>
  m_Z <span style="color: #000040;">/</span><span style="color: #000080;">=</span> v.<span style="color: #007788;">m_Z</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Vector3 Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> Vector3<span style="color: #008000;">&#40;</span> m_X <span style="color: #000040;">+</span> v.<span style="color: #007788;">m_X</span>, m_Y <span style="color: #000040;">+</span> v.<span style="color: #007788;">m_Y</span>, m_Z <span style="color: #000040;">+</span> v.<span style="color: #007788;">m_Z</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Vector3 Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">-</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> Vector3<span style="color: #008000;">&#40;</span> m_X <span style="color: #000040;">-</span> v.<span style="color: #007788;">m_X</span>, m_Y <span style="color: #000040;">-</span> v.<span style="color: #007788;">m_Y</span>, m_Z <span style="color: #000040;">-</span> v.<span style="color: #007788;">m_Z</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Vector3 Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">/</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> Vector3<span style="color: #008000;">&#40;</span> m_X <span style="color: #000040;">/</span> v.<span style="color: #007788;">m_X</span>, m_Y <span style="color: #000040;">/</span> v.<span style="color: #007788;">m_Y</span>, m_Z <span style="color: #000040;">/</span> v.<span style="color: #007788;">m_Z</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Vector3 Vector3<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> Vector3<span style="color: #008000;">&#40;</span> m_X <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_X</span>, m_Y <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_Y</span>, m_Z <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_Z</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">float</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">GetX</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> m_X<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">float</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">GetY</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> m_Y<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">float</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">GetZ</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> m_Z<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">SetX</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> x <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  m_X <span style="color: #000080;">=</span> x<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">SetY</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> y <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  m_Y <span style="color: #000080;">=</span> y<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">SetZ</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">float</span> z <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  m_Z <span style="color: #000080;">=</span> z<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Vector3 Vector3<span style="color: #008080;">::</span><span style="color: #007788;">Normalise</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">double</span> length <span style="color: #000080;">=</span> Length<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> length <span style="color: #000080;">==</span> <span style="color:#800080;">0.0</span> <span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// return a zero vector if it's got zero length</span>
    <span style="color: #0000ff;">return</span> Vector3<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">double</span> invLength <span style="color: #000080;">=</span> <span style="color:#800080;">1.0</span> <span style="color: #000040;">/</span> length<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> Vector3<span style="color: #008000;">&#40;</span> m_X <span style="color: #000040;">*</span> invLength, m_Y <span style="color: #000040;">*</span> invLength, m_Z <span style="color: #000040;">*</span> invLength <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">double</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">Dot</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span>  m_X <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_X</span> <span style="color: #000040;">+</span> m_Y <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_Y</span> <span style="color: #000040;">+</span> mZ <span style="color: #000040;">*</span> v.<span style="color: #007788;">mZ</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">double</span> Vector3<span style="color: #008080;">::</span><span style="color: #007788;">Length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">sqrt</span><span style="color: #008000;">&#40;</span> m_X <span style="color: #000040;">*</span> m_X <span style="color: #000040;">+</span> m_Y <span style="color: #000040;">*</span> m_Y <span style="color: #000040;">+</span> mZ <span style="color: #000040;">*</span> mZ <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
Vector3 Vector3<span style="color: #008080;">::</span><span style="color: #007788;">Cross</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Vector3<span style="color: #000040;">&amp;</span> v <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> Vector3<span style="color: #008000;">&#40;</span> m_Y <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_Z</span> <span style="color: #000040;">-</span> m_Z <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_Y</span>,
                  m_Z <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_X</span> <span style="color: #000040;">-</span> m_X <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_Z</span>,
                  m_X <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_Y</span> <span style="color: #000040;">-</span> m_Y <span style="color: #000040;">*</span> v.<span style="color: #007788;">m_X</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now that we have a functional class, we should have no problem using it in all of the above scenarios without any crazy side-effects. The class <em>should</em> be concrete! <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
<p>Disclaimer: This code hasn't been compiled, and might not run without a few tweaks. I wrote this off the top of my head while sitting in front of the TV! Comments, questions and flames are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://buffered.io/2007/07/14/creating-concrete-objects/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
