<?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>Chad Stewart: Game Programmer</title>
	<atom:link href="http://www.chadpluspl.us/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.chadpluspl.us</link>
	<description></description>
	<lastBuildDate>Fri, 18 May 2012 13:30:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>C# Anti-Pattern: try catch flow control</title>
		<link>http://www.chadpluspl.us/?p=557</link>
		<comments>http://www.chadpluspl.us/?p=557#comments</comments>
		<pubDate>Fri, 18 May 2012 13:30:54 +0000</pubDate>
		<dc:creator>Electric Chad</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[Exception]]></category>
		<guid isPermaLink="false">http://www.chadpluspl.us/?p=557</guid>
		<description><![CDATA[Exceptions. You love them or you hate them. You can wade through the arguments of performance yourself. I don&#8217;t think that anyone believes they are faster than a return or something similar, but whether that should matter is another issue. There are people who say they&#8217;re plain slow, some who say, &#8220;Damn the torpedos!&#8221;, and [...]]]></description>
			<content:encoded><![CDATA[<p>Exceptions. You love them or you hate them. You can wade through the arguments of performance yourself. I don&#8217;t think that anyone believes they are faster than a return or something similar, but whether that should matter is another issue. There are <a href="http://www.howzatt.demon.co.uk/articles/12May04.html">people who say they&#8217;re plain slow</a>, <a href="http://stackoverflow.com/questions/134698/catching-exceptions-as-expected-program-execution-flow-control">some who say, &#8220;Damn the torpedos!&#8221;</a>, and <a href="http://www.yoda.arachsys.com/csharp/exceptions.html">people who straddle that line</a>. </p>
<p>My opinion? They have their place. However, it&#8217;s because of the abuse of Exceptions that you sometimes feel dirty for using them.  I tend to agree with those that believe in exceptions for exceptional cases, as lame and / or straightforward as that sounds. </p>
<p>Why do I say this? Sometimes you see Exceptions used for ordinary, average program flow control.</p>
<p><code lang="C#"><br />
try<br />
{<br />
    this.SomeImportantSoundingFunctionThatShouldNotFail(someParam, someLocal, -1);<br />
}<br />
catch (System.Data.SqlException)<br />
{<br />
    return true;<br />
}<br />
catch<br />
{<br />
    return false;<br />
}<br />
return true;<br />
</code></p>
<p>I actually saw this in a very real codebase! Yes, including two unnamed exceptions. (Side note: Don&#8217;t do that either.) Apparently, it&#8217;s okay to fail in the SQL query, but if there was to be some sort of Windows exception (NotEnoughMemory?) would be an actual failure. This code, to me, says that the entire Database is unnecessary or we assume we know exactly what the SqlException. In the end, I investigated an SQL procedure and it was a constraint violation that wasn&#8217;t truly a failure, but this function was ugly none the less. Furthermore, what happens when the SqlException changes because we can&#8217;t even connect to the database? </p>
<p>Looking at this, you might not think the code is THAT bad. Consider it, though. We sweep an exception under the rug, designed a system to silently fail half the time, and ignore the unnamed exception because we <em>think</em> we know exactly what it is. This is a bad assumption with an unnecessary amount of overhead. </p>
<p>This is a pretty simple example and some might argue with the language I have used here. Let me clarify. Exceptions should not be used in everyday code flow as a fifty-fifty chance. Exceptions should be thrown when the caller gives you egregiously bad data. Exceptions are okay if you get some unrecoverable error you need to bubble upwards. Exceptions are a part of the language and they have their place. That place is not multiple times a frame every update loop.</p>
<p>What if I told you this was in some TreeView related code? What if I told you this code was called thousands of times in a second in response to a mouse click? Exceptions have performance and memory implications if used two thousand times in a row in a single frame. There&#8217;s no way around that one. You could balloon loading from a minute to five. (Another side note: That&#8217;s not hypothetical. That&#8217;s anecdotal.) Be careful about how you use exceptions! Please don&#8217;t use exceptions for flow control, especially deep in a stack that gets called often.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chadpluspl.us/?feed=rss2&#038;p=557</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>#P4Groupie at #P4RoadTrip</title>
		<link>http://www.chadpluspl.us/?p=769</link>
		<comments>http://www.chadpluspl.us/?p=769#comments</comments>
		<pubDate>Fri, 11 May 2012 15:00:15 +0000</pubDate>
		<dc:creator>Electric Chad</dc:creator>
				<category><![CDATA[Business]]></category>
		<guid isPermaLink="false">http://www.chadpluspl.us/?p=769</guid>
		<description><![CDATA[Monday I went to an event called the Perforce Road Trip. It was a sales pitch, demo, and networking event. The tagline: Version Everything. The pitch: Get all your Microsoft Office documents in one easy to use, easy to version place now that it is brain-dead simple. At the event they introduced two new products [...]]]></description>
			<content:encoded><![CDATA[<p>Monday I went to an event called the <a href="http://info.perforce.com/2012roadtrip">Perforce Road Trip</a>. It was a sales pitch, demo, and networking event. The tagline: Version Everything. The pitch: Get all your Microsoft Office documents in one easy to use, easy to version place now that it is brain-dead simple. At the event they introduced two new products they made available for download that morning called <a href="http://www.perforcechronicle.com/">Chronicle</a> and <a href="http://www.perforce.com/products/commons">Commons</a>. Chronicle is some fancy website editor and wrapper around Perforce or some other such mumbo jumbo to which I wasn&#8217;t paying attention. Commons is Perforce easy-mode, as if P4V didn&#8217;t seem easy enough. It was actually pretty rad.</p>
<p>After a short coffee and pastry break, we reconvened to hear about the latest server upgrade and how it can be utilized to the fullest as well as a testimonial from National Instruments on the why and how of Perforce. I heard a few cool things and had a few good ideas. The event, however, seemed aimed at executives and administrators. I think I was one of three or four developers.</p>
<p>The real value, for me, came afterwards during the demos and networking. This is where I realized that Commons could be everything that we wanted, got some made-to-order macaroni and cheese, and enjoyed some conversation. I was able to talk to some folks from other companies and get some stellar advice. Use an ssd for your Perforce server and it will be crazy fast. Don&#8217;t bloat your metadata with horrible automatic check-ins. Go to Burning Man and get crazy. You know, all kinds of great advice.</p>
<p>My advice? Go for the swag. Stick around at the end for the networking. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.chadpluspl.us/?feed=rss2&#038;p=769</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code Review Infographic</title>
		<link>http://www.chadpluspl.us/?p=767</link>
		<comments>http://www.chadpluspl.us/?p=767#comments</comments>
		<pubDate>Wed, 09 May 2012 22:42:02 +0000</pubDate>
		<dc:creator>Electric Chad</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Code Review]]></category>
		<guid isPermaLink="false">http://www.chadpluspl.us/?p=767</guid>
		<description><![CDATA[Better Code Starts with Review]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.smartbear.com/post/12-03-14/better-code-starts-with-review/" originalAttribute="href" originalPath="http://blog.smartbear.com/post/12-03-14/better-code-starts-with-review/" target="_blank"> <img src="http://support.smartbear.com/images/blog/better-code-infograph/smartbear-infographic_better-code-starts-with-review-small.jpg" originalAttribute="src" originalPath="http://support.smartbear.com/images/blog/better-code-infograph/smartbear-infographic_better-code-starts-with-review-small.jpg" alt="What Agile Teams Should Avoid in 2012" width="106" height="500" border="0" title="What Agile Teams Should Avoid in 2012" /></a><br/> <a href="http://blog.smartbear.com/post/12-03-14/better-code-starts-with-review/" originalAttribute="href" originalPath="http://blog.smartbear.com/post/12-03-14/better-code-starts-with-review/">Better Code Starts with Review </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.chadpluspl.us/?feed=rss2&#038;p=767</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DebtScape: My First WP7 Application</title>
		<link>http://www.chadpluspl.us/?p=760</link>
		<comments>http://www.chadpluspl.us/?p=760#comments</comments>
		<pubDate>Fri, 13 Apr 2012 13:00:00 +0000</pubDate>
		<dc:creator>Electric Chad</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>
		<guid isPermaLink="false">http://www.chadpluspl.us/?p=760</guid>
		<description><![CDATA[In an earlier post I alluded to the fact that I got a new toy. That toy is a new Windows Phone. Before you ask, it&#8217;s about stability and trying new things. I have had iOS, Android, and now Windows Phone 7.5. It grabbed my attention because I could make applications for it with the [...]]]></description>
			<content:encoded><![CDATA[<p>In an earlier post I alluded to the fact that I got a new toy. That toy is a new Windows Phone. Before you ask, it&#8217;s about stability and trying new things. I have had iOS, Android, and now Windows Phone 7.5. It grabbed my attention because I could make applications for it with the technology I already know and love: C#. Here is my Freshman effort.</p>
<p>It is called Debtscape. It helps you escape your debt. (A port manteau of debt and escape, of course.) You enter your debts and you can use one of two debt snowball methods to calculate how long until you pay off your debts. Plus, it tells you what order to pay off your debts. Here is a screenshot in which you would never be able to tell that I did my own art.</p>
<p><a href="http://chadplusplus.wpengine.netdna-cdn.com/wp-content/uploads/2012/04/DebtScapeScreenshot.png"><img class="alignnone size-full wp-image-764" title="DebtScapeScreenshot" src="http://chadplusplus.wpengine.netdna-cdn.com/wp-content/uploads/2012/04/DebtScapeScreenshot.png" alt="" width="610" height="1109" /></a></p>
<p>Let me break down what is in this photo and what I learned. First of all, here we have a Panorama view. A Panorama is the current Windows Phone hotness in which you can put multiple tabs on a single page and navigate through them by swiping left and right. You&#8217;ll notice that you can see a sliver of the next tab on the right side, and this is to make it intuitive that you are able to swipe from right to left to discover it. You create Panorama Items like so:</p>
<p></p><pre class="crayon-plain-tag">&lt;!--Panorama control--&gt;
&lt;controls:Panorama Title=&quot;DebtScape&quot;&gt;
&lt;controls:Panorama.Background&gt;
&lt;ImageBrush ImageSource=&quot;../DebtScapeBackground.png&quot;/&gt;
&lt;/controls:Panorama.Background&gt;
&lt;!--Panorama item one--&gt;
&lt;controls:PanoramaItem x:Name=&quot;Debts&quot; Header=&quot;Debts&quot;&gt;
&lt;/controls:PanoramaItem&gt;
&lt;/controls:Panorama&gt;</pre><p></p>
<p>You can see that I use the Header attribute in the PanoramaItem. This gives me a default header at the top labeling the tab of the PanoramaItem. DebtScape uses three tabs: Debts, Add Debt, and Settings. This is the Debts PanoramaItem and it contains a DataBound ListBox. This is achieved simply and elegantly through the xaml. Instead of putting any code in the .cs file, I have created a template that transforms a DebtItem from a data structure into a view with a progress bar and fancy markup. It happens in the xaml like so:</p>
<p></p><pre class="crayon-plain-tag">&lt;ListBox Name=&quot;DebtListBox&quot; Margin=&quot;0,0,0,0&quot; DataContext=&quot;{Binding}&quot; ItemsSource=&quot;{Binding Items}&quot; VerticalAlignment=&quot;Top&quot;&gt;
                        &lt;ListBox.ItemTemplate&gt;
                            &lt;DataTemplate&gt;
                                &lt;StackPanel Margin=&quot;0,0,0,0&quot; Width=&quot;432&quot; Height=&quot;198&quot;&gt;
                                    &lt;TextBlock Text=&quot;{Binding Name}&quot; TextWrapping=&quot;Wrap&quot; Style=&quot;{StaticResource PhoneTextExtraLargeStyle}&quot;/&gt;
                                    &lt;Rectangle Height=&quot;26&quot; Margin=&quot;-20,0,0,0&quot; Width=&quot;390&quot; Stroke=&quot;Black&quot;&gt;
                                        &lt;Rectangle.Fill&gt;
                                            &lt;LinearGradientBrush StartPoint=&quot;0,0&quot; EndPoint=&quot;1,1&quot;&gt;
                                                &lt;LinearGradientBrush.GradientStops&gt;
                                                    &lt;GradientStop Color=&quot;Red&quot; Offset=&quot;0.0&quot;&gt;&lt;/GradientStop&gt;
                                                    &lt;GradientStop Color=&quot;Red&quot; Offset=&quot;{Binding PercentPaid}&quot;&gt;&lt;/GradientStop&gt;
                                                    &lt;GradientStop Color=&quot;Transparent&quot; Offset=&quot;1.0&quot;&gt;&lt;/GradientStop&gt;
                                                &lt;/LinearGradientBrush.GradientStops&gt;
                                            &lt;/LinearGradientBrush&gt;
                                        &lt;/Rectangle.Fill&gt;
                                    &lt;/Rectangle&gt;
                                    &lt;TextBlock Text=&quot;{Binding PrettyDebtString}&quot; Margin=&quot;0,-29,0,0&quot; TextWrapping=&quot;Wrap&quot; Style=&quot;{StaticResource PhoneTextNormalStyle}&quot; Padding=&quot;20,1,0,0&quot; /&gt;
                                    &lt;TextBlock Text=&quot;{Binding MinimumPayment, StringFormat='{}{0:C} Per Month' }&quot; TextWrapping=&quot;Wrap&quot; Style=&quot;{StaticResource PhoneTextNormalStyle}&quot; /&gt;
                                    &lt;TextBlock Text=&quot;{Binding APR, StringFormat='{}{0}%' }&quot; HorizontalAlignment=&quot;Right&quot; Margin=&quot;0,-27,35,0&quot; TextWrapping=&quot;Wrap&quot; Style=&quot;{StaticResource PhoneTextNormalStyle}&quot; /&gt;
                                    &lt;TextBlock Text=&quot;{Binding MonthsRemaining, StringFormat='{}{0} Months Remaining' }&quot; TextWrapping=&quot;Wrap&quot; Style=&quot;{StaticResource PhoneTextNormalStyle}&quot; /&gt;
                                    &lt;Button Content=&quot;Pay&quot; Name=&quot;payButton&quot; Width=&quot;130&quot; Height=&quot;70&quot; HorizontalAlignment=&quot;Left&quot; Click=&quot;payButton_Click&quot; /&gt;
                                    &lt;Button Content=&quot;Edit&quot; Name=&quot;editButton&quot; Width=&quot;130&quot; Height=&quot;70&quot; Margin=&quot;0,-70,19,0&quot; HorizontalAlignment=&quot;Center&quot; Click=&quot;editButton_Click&quot; /&gt;
                                    &lt;Button Content=&quot;Delete&quot; Name=&quot;deleteButton&quot; Width=&quot;130&quot; Height=&quot;70&quot; Margin=&quot;0,-70,20,0&quot; HorizontalAlignment=&quot;Right&quot; Click=&quot;deleteButton_Click&quot; /&gt;
                                &lt;/StackPanel&gt;
                            &lt;/DataTemplate&gt;
                        &lt;/ListBox.ItemTemplate&gt;
                    &lt;/ListBox&gt;</pre><p></p>
<p>Do you see the fanciness of Binding? Oh yes! We simply set the ListBox&#8217;s DataContext property to an IEnumerable; and it works like a charm. Actually, we could set it to a IEnumerable of anything as long as it contains the public Properties that are mentioned in the Bindings. This is both a blessing and a curse as it is a simple to do as it is to screw up. Speaking of making sure input is valid; queue an awesome segue way into the Add Debt Panorama Item!</p>
<p>The Add Debt PanoramaItem is a tab that lets the user add new debts. I require the fields to be filled out and the fields that are numbers must be numbers. Here is how you can do that!</p>
<p></p><pre class="crayon-plain-tag">&lt;TextBox x:Name=&quot;minimumPaymentTextBox&quot; Text=&quot;{Binding MinimumPayment, Mode=TwoWay}&quot; InputScope=&quot;Number&quot; /&gt;</pre><p></p>
<p>I have ensured the data is entered appropriately by constraining the input. This actually pops up a keyboard that only contains a number and a period. On the back end, I have bound this to a float property. Should the data be mucked with outside the application somehow they will get a big, fat goose egg. (That&#8217;s a 0.)</p>
<p>Okay, this seems to be running a little long. Stay tuned for the next episode where I&#8217;ll delve into the Settings and how to use IsolatedStorage to store files and other objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chadpluspl.us/?feed=rss2&#038;p=760</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Profiling A C# Application with the Stand-Alone Profiler</title>
		<link>http://www.chadpluspl.us/?p=749</link>
		<comments>http://www.chadpluspl.us/?p=749#comments</comments>
		<pubDate>Fri, 27 Jan 2012 14:30:27 +0000</pubDate>
		<dc:creator>Electric Chad</dc:creator>
				<category><![CDATA[C#]]></category>
		<guid isPermaLink="false">http://www.chadpluspl.us/?p=705</guid>
		<description><![CDATA[Here&#8217;s the scoop. I wanted to profile my application to show someone how we could get the best &#8220;bang for our buck&#8221; on a single hardware upgrade. Did I need more memory or CPU? I expected, in the course of collecting metrics, to find some slower code snippets to improve software side. It can be [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the scoop. I wanted to profile my application to show someone how we could get the best &#8220;bang for our buck&#8221; on a single hardware upgrade. Did I need more memory or CPU? I expected, in the course of collecting metrics, to find some slower code snippets to improve software side. It can be sped up a little bit, but the fact of the matter is that the sheer volume of data has increased to a tipping point the hardware simply could not handle in the same time frame. Also, I wanted like to start doing this on a more regular cadence, so I would need any help I can get to speed up the process. You would think that Visual Studio 2008 would have something built in for C#. Well, it does if you shell out the extra money for the Team Edition. I have Professional. I am thusly screwed, right?</p>
<p>Wrong. For some reason they offer the Performance Tools from the Team Suite as a free download online. <a href="http://www.microsoft.com/downloads/details.aspx?familyid=fd02c7d6-5306-41f2-a1be-b7dcb74c9c0b&amp;displaylang=en">Check it out!</a> How nice, Microsoft! The only draw back is that you will be doing this through the command line. (Even then, for some that is a pro and not a con.) My program is a command line utility, so it did not really change my method, but it is something to note as a draw back for the less command line literate. So I looked for a tutorial to get me on my way and I found everything I ever needed. Sort of. A little bit.</p>
<p>I spent way too much time staring at a blue screen. It was horrendous to behold. All I was trying to do is profile my C# application to gather some metrics for something I already know. The computer needed a memory upgrade. Before you even begin, see if you need this patch: <a href="http://blogs.msdn.com/b/chrissc/archive/2009/02/24/patch-released-to-fix-sampling-on-intel-core-i7-processors.aspx">http://blogs.msdn.com/b/chrissc/archive/2009/02/24/patch-released-to-fix-sampling-on-intel-core-i7-processors.aspx</a>. Otherwise you will blue screen constantly and think you are doing something wrong. Trust me, it is a huge waste of time if you have an i7 processor and don&#8217;t download this patch.</p>
<p>To get us started, here is the article of reference I used: <a href="http://msdn.microsoft.com/en-us/library/aa985628.aspx">http://msdn.microsoft.com/en-us/library/aa985628.aspx</a>. This was probably the best source of information. I will be duplicating some of the information here in walking through how to profile a C# application.</p>
<p>Let&#8217;s begin. Build your binaries in Release! Building in Debug is a waste of time as it does not perform quite the same. Add the location of the VSPerf tools to your PATH. Again, build Release and then fire up the command line. For me, this was located in &#8220;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Team Tools&#8221; and if you have Visual Studio 2008, that&#8217;s where it should be for you. Translate to the proper Visual Studio install and OS for your machine.</p>
<p>Now you have a choice to make: Sampling or Instrumentation. If sampling, you just need to turn sampling on globally with the following command:<br />
<code lang="DOS"><br />
VSPerfCLREnv /sampleon<br />
</code><br />
If you are using instrumentation, you will have to do slightly more. Turn on instrumentation globally:<br />
<code lang="DOS"><br />
VSPerfCLREnv /traceon<br />
</code><br />
For all of the dll and exe files that you will be profiling, (hint: do this for dependencies) you will need to run the following commands to allow instrumentation.<br />
<code lang="DOS"><br />
VSInstr ProgramOrDll.exe<br />
</code></p>
<p>Now you&#8217;re ready to collect some data. I hope you have a simple, repeatable test planned. This is critical to identifying performance issues, especially if you plan on fixing them. Always have a repeatable test and run it multiple times to ensure you get correct data. Run your program from the command line with Performance Monitor by using the following command:<br />
<code lang="DOS"><br />
VsPerfCmd /start:sample /output:Report.vsp /launch:YourProgram.exe<br />
</code></p>
<p>When you&#8217;re done with sampling, you must turn it off with the following command. (Instrumentation will end when your program ends.)<br />
<code lang="DOS"><br />
VSPerfCmd /shutdown<br />
</code></p>
<p>Now you&#8217;ve got a beautiful vsp sitting on your hard-drive! This is the raw performance data. Now you need to change it into a consumable format because of our lowly Visual Studio version. Team Edition opens these bad boys. We are going to use more primitive tools. Microsoft, and I, recommend creating a folder at this point to help stay organized. Run this command to create a bunch of csv files:<br />
<code lang="DOS"><br />
VSPerfReport PeopleTraxReport.vsp /output:path\filename /summary:all</code></p>
<p>Now, don&#8217;t forget to restore your environment!<br />
<code lang="DOS"><br />
VSPerfCLREnv /sampleoff<br />
</code></p>
<p>These csv files are all different types of profiling information. You can tell what they are by the extension. There are functions, threads, modules, etc; My recommendations? If you&#8217;re looking for hotspots, use the _functions file. Application Exclusive Time is a great column to sort. If you&#8217;re trying to find a dead-lock or places of I/O wait try the _threads file. Otherwise, enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chadpluspl.us/?feed=rss2&#038;p=749</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run Time Type Checking In Objective-C</title>
		<link>http://www.chadpluspl.us/?p=727</link>
		<comments>http://www.chadpluspl.us/?p=727#comments</comments>
		<pubDate>Fri, 20 Jan 2012 14:30:12 +0000</pubDate>
		<dc:creator>Electric Chad</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[isa]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[reflection]]></category>
		<guid isPermaLink="false">http://www.chadpluspl.us/?p=727</guid>
		<description><![CDATA[There I was programming away, implementing a factory in Objective-C. My C++ instincts told me I would either need to create an enumeration, a template, or some sort of Factory inheritance chain. My C# intuition pointed me in the direction of Reflection. Reflection was pretty slick, so I opted to look down that path. You [...]]]></description>
			<content:encoded><![CDATA[<p>There I was programming away, implementing a factory in Objective-C. My C++ instincts told me I would either need to create an enumeration, a template, or some sort of Factory inheritance chain. My C# intuition pointed me in the direction of Reflection. Reflection was pretty slick, so I opted to look down that path. You know what? There was a bright light at the end of that tunnel.</p>
<p>Now, what I found wasn&#8217;t exactly reflection, more like run time type checking, but it was certainly a useful tool to accomplish what I needed to do. What&#8217;s that magic?</p><pre class="crayon-plain-tag">[SomeClass class];</pre><p>That&#8217;s right, there is actually an object called Class. Every single object implicitly has a member of type Class, the isa pointer, which you can get with the &#8220;class&#8221; message. This member tells you to what type your object points. You may have noticed this while debugging in XCode. Every time you mouse over a variable you get that lovely isa pointer. You probably thought it was obvious and totally annoying if you came from Visual Studio. Class is a strong type of which you can declare instances, pass to functions, and anything else you can do with a class. You can use this for numerous things, the most obvious among them being run-time type checking. Is my BaseVehicle object ACTUALLY a Car or is it a Plane? Grab that class and compare like so:</p><pre class="crayon-plain-tag">Class realClass = [someVehiclePointer class];
if ([realClass isKindOfClass:[Car class]])
{
// Do something specific to Cars.
}</pre><p>Maybe, you want to know more than whether you&#8217;re a Car. You have a Sedan or a SportsCar class derived from Car and you even have Civic, Camry, Charger, and Lamborghini classes derived from those types. Maybe you only want SportsCar derived classes.</p><pre class="crayon-plain-tag">if ([realClass isSubclassOfClass:[SportsCar class]])
{
// Get ready for the races!
}</pre><p>Now, what if you wanted to test for exact matches only? What if you wanted to put your foot down and make sure that there is no inheritance masquerading, and that your object MUST be a Lamborghini in order to respond to the OpenButterflyDoors message. There is a message for that as well!</p><pre class="crayon-plain-tag">if ([realClass isMemberOfClass:[Lamborghini class]])
{
[someVehiclePointer OpenButterflyDoors];
}</pre><p>Finally, another trick you can do with Class, is use an instance of Class as a type. (Huh?) Meaning whatever you can do by typing the actual class name, it is possible to do with an instance of Class. (Well, except forward declaration, I think.) For example, if I had a VehicleFactory, I could implement a method in the following way:</p><pre class="crayon-plain-tag">- (Vehicle*) createGameObjectOfClass:(Class)type Error:(NSError**)error
{
Vehicle* result = nil;
if ([type isSubclassOfClass:[Vehicle class]])
{
result = [[type alloc] init];
[m_objects addObject:result];
}
return result;
}</pre><p>Hello, Factory! Hello, decoupling! Hello, Class! But wait, there&#8217;s more! Is it necessary to check if you subclass something? Maybe my factory follows a different contract. By convention, people usually think that you need to be of a certain subclass to have any given collection of functions. Not true. You don&#8217;t have to derive from a Car in order to accelerate and decelerate. In fact, a Person does the same thing. If I only care about subclassing because I don&#8217;t want to add objects that can&#8217;t respond to that message there is a more accurate check!</p><pre class="crayon-plain-tag">if ([vehicle respondsToSelector:@selector(accelerate)])
{
// Make the thing go vroom!
}</pre><p>At this point, you may be looking at all of this and thinking it is a bit of black magic. It must have a harsh performance penalty. It seems you would be disappointed. You see, Objective-C sends messages using a similar methodology all the time. You check the isa pointer on a class to see whether or not a selector (method, message) is handled by the class. If it is not, you check the parent class, rinse, repeat. Theoretically, you incur the same cost as calling a function (sending a message.) The only gotcha there is that over time, messages that are commonly called get put into a cache and don&#8217;t incur the walking of the inheritance chain. (<a href="http://objc.toodarkpark.net/coreobjc.html#1522">See link.</a>) If you want faster performance than that&#8230; well, you could turn to <a href="http://www.mulle-kybernetik.com/artikel/Optimization/">these articles</a> and start<a href="http://www.mulle-kybernetik.com/artikel/Optimization/opti-9.html"> implementing your own obj_msgSend function</a>. Personally, I prefer readability. I prefer clarity and reduced maintenance costs. I prefer simply sending messages by using the bracket notation and the simple isKindOfClass message.</p>
<p>Thoughts? Comments? Clever uses for the isa pointer? Drop a comment!</p>
<p style="text-align: right;">See you, Space Cowboy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chadpluspl.us/?feed=rss2&#038;p=727</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Welcome to the New Chad++</title>
		<link>http://www.chadpluspl.us/?p=752</link>
		<comments>http://www.chadpluspl.us/?p=752#comments</comments>
		<pubDate>Fri, 06 Jan 2012 04:14:06 +0000</pubDate>
		<dc:creator>Electric Chad</dc:creator>
				<category><![CDATA[Site]]></category>
		<guid isPermaLink="false">http://chadplusplus.wpengine.com/?p=752</guid>
		<description><![CDATA[&#8220;Out with the old, in with the new,&#8221; they say. (Even if we&#8217;re not quite sure who they are.) You&#8217;ll notice that the old is currently gone and that there&#8217;s only this in it&#8217;s place. It&#8217;s a whole new year, baby. Welcome to my fancy, new blog. I decided to move hosts to WP Engine, [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Out with the old, in with the new,&#8221; they say. (Even if we&#8217;re not quite sure who they are.) You&#8217;ll notice that the old is currently gone and that there&#8217;s only this in it&#8217;s place.</p>
<p>It&#8217;s a whole new year, baby. Welcome to my fancy, new blog. I decided to move hosts to <a href="http://wpengine.com/?a_aid=4f0129a3db723">WP Engine, a dedicated WordPress host</a> who will serve my pages 4.7x faster. (Yes, that&#8217;s a real, measured metric.) As a part of the move, I have decided to do some major house-keeping. I am going to edit the heck out of the previous content I had and re-post it eventually along with plenty of fresh stuff.</p>
<p>So welcome. If I could pretend this was a website from the earlier 90&#8242;s for a moment:</p>
<p><a href="http://chadplusplus.wpengine.com/wp-content/uploads/2012/01/6a00d83451cc8269e201348829df49970c-800wi.gif"><img class="alignnone size-full wp-image-753" title="PardonOurDust" src="http://chadplusplus.wpengine.com/wp-content/uploads/2012/01/6a00d83451cc8269e201348829df49970c-800wi.gif" alt="https://www.google.com/search?q=Pardon+My+Dust" width="649" height="502" /></a></p>
<p>There are exciting things to come. We just launched <a href="http://www.amazon.com/gp/product/B005B8DRVU/ref=as_li_ss_tl?ie=UTF8&amp;tag=binsol-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B005B8DRVU">Star Wars: The Old Republic</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=binsol-20&amp;l=as2&amp;o=1&amp;a=B005B8DRVU" alt="" width="1" height="1" border="0" />. I have news I cannot share, yet. I just got a new toy and I want to get programming. Join me, won&#8217;t you? Until next time&#8230;</p>
<p>&nbsp;</p>
<p style="text-align: right;">See you, Space Cowboy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chadpluspl.us/?feed=rss2&#038;p=752</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

