<?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>dispatchEvent()™ &#187; actionscript</title>
	<atom:link href="http://dispatchevent.org/tag/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://dispatchevent.org</link>
	<description>Collective thoughts on the Flash Platform, iOS, Unity, and any other technology we use.</description>
	<lastBuildDate>Tue, 07 Feb 2012 19:22:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tip: Adding version checking to your external code library</title>
		<link>http://dispatchevent.org/mims/version-checking/</link>
		<comments>http://dispatchevent.org/mims/version-checking/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 18:14:33 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[KitchenSync]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=446</guid>
		<description><![CDATA[Since version 1.0 of a code library that I&#8217;m sure you&#8217;re tired of me talking about came out, I have been making steady updates, some of which break legacy code. I was also having trouble keeping track of which version &#8230; <a href="http://dispatchevent.org/mims/version-checking/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since version 1.0 of <a href="http://kitchensynclib.googlecode.com" target="_blank">a code library that I&#8217;m sure you&#8217;re tired of me talking about</a> came out, I have been making steady updates, some of which break legacy code. I was also having trouble keeping track of which version of the library a particular demo was written for. In order to make sure that the new code library doesn&#8217;t cause unpredictable results for the old implementations I added a version check to the main class.</p>
<p>This version check is very simple. It checks a version number in the client code against a version number of the library (or external classes) when the main class of the library is initialized. It is also completely optional (so average users don&#8217;t need to mark up their code with version numbers).</p>
<p>Here&#8217;s an example:</p>
<p>Say you normally initialize your library using:</p>
<pre lang="actionscript3">MyLibrary.initialize();</pre>
<p>Which calls the initialize method:</p>
<pre lang="actionscript3">public static function initialize():void {
   // initialize library here
}</pre>
<p>If the external code changes unexpectedly (say through an SVN update) this could cause problems that are difficult to trace. The solution is to modify the initializer to match the client code version with the initializer version.</p>
<pre lang="actionscript3">public static const VERSION:String = "1.6";
public static function initialize(versionCheck:String = VERSION):void {
   if (versionCheck != VERSION) {
       throw new Error ("The version check failed! This library is version " + VERSION + ". Update your code or GTFO!");
   }
   // initialize library here
}</pre>
<p>And when you run the initializer, use the version number you&#8217;re expecting.</p>
<pre lang="actionscript3">MyLibrary.initialize("1.6");</pre>
<p>Some things to notice with this approach:</p>
<ul>
<li>I used a string instead of a number for the version number so that it would allow for sub-sub version numbers and other markers, e.g. &#8220;1.6.24 beta r545&#8243;</li>
<li>Because there is a default value for <code>versionCheck</code> in the initializer, providing a version number is optional. Using <code>MyLibrary.initialize()</code> will still work and throw no errors.</li>
<li>Unfortunately, there is not much you can do if the version numbers don&#8217;t match except to warn the client that the external code has changed. Still, I&#8217;ve found this to be very useful.</li>
</ul>
<p>The code for my initializer in its entirety after the jump&#8230;<br />
<span id="more-446"></span><br />
From <a href="http://code.google.com/p/kitchensynclib/source/browse/trunk/src/org/as3lib/kitchensync/KitchenSync.as">KitchenSync.as</a></p>
<pre lang="actionscript3">/**
* The current version of the library. Use this to verify that the library is the
* version that your software expects.
*/
public static const VERSION:String = "1.6";

/**
* Flag noting whether the engine has been initialized.
*/
private static var _initialized:Boolean = false;

/**
* Initializes the timing core for KitchenSync. Must be called before using any actions.
*
* @param frameRateSeed must be a DisplayObject that is added to the display list.
* @param versionCheck a string for the version you think you're using. e.g. 1.2 This is recommended
*                                         but not required. It will throw an error if you're using the wrong version of KS.
*/
public static function initialize(frameRateSeed:DisplayObject, versionCheck:String = VERSION):void
{       

	if (_initialized) {
		throw new IllegalOperationError("KitchenSync has already been initialized.");
	}
	if (versionCheck != VERSION) {
		throw new Error ("Version check failed. Please update to the correct version or to continue using this version (at your own risk) put the initialize() method inside a try{} block.");
	}

	// Initialization code omitted

	_initialized = true;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/version-checking/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

