<?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>Richard Lord &#187; Learning</title>
	<atom:link href="http://www.richardlord.net/blog/tag/learning/feed" rel="self" type="application/rss+xml" />
	<link>http://www.richardlord.net</link>
	<description>Actionscript/Flex, PHP and Java developer</description>
	<lastBuildDate>Mon, 23 Jan 2012 17:07:46 +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>Garbage Collection</title>
		<link>http://www.richardlord.net/blog/garbage-collection</link>
		<comments>http://www.richardlord.net/blog/garbage-collection#comments</comments>
		<pubDate>Tue, 12 Aug 2008 07:53:08 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Best practice]]></category>
		<category><![CDATA[Flint]]></category>
		<category><![CDATA[Garbage collection]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Particles]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/garbage-collection/</guid>
		<description><![CDATA[<p>I recently received a support query on Flint regarding garbage collection. In discussions on the forum I was reminded that many Actionscript developers don't know how the Flash Player's garbage collection works...</p>]]></description>
			<content:encoded><![CDATA[<p>I recently received a support query on <a href="http://flintparticles.org/">Flint</a> regarding garbage collection. In discussions on the <a href="http://flintparticles.org/forum">forum</a> I was reminded that many Actionscript developers don&#8217;t know how the Flash Player&#8217;s garbage collection works.</p>

<p>For some developers that&#8217;s not a problem since memory use is quite low on lots of web sites, and all the memory is freed when a user leaves the web site anyway. However, with the development of rich applications on the web and in Air, garbage collection is becoming an issue that more developers need to understand.</p>

<p>I was going to write a long post explaining garbage collection, but Grant Skinner got there two years before me. Here&#8217;s his useful series about <a href="http://gskinner.com/blog/archives/2006/06/as3_resource_ma.html">resource management in Flash</a>.</p>

<p>When you&#8217;ve read that, this more recent post by Sean Christmann adds further insight &#8211; <a href="http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/">Kick starting the garbage collector</a>.</p>

<p>Now there&#8217;s no excuse for leaving trash lying around.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/garbage-collection/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The parentheses operator</title>
		<link>http://www.richardlord.net/blog/the-parentheses-operator</link>
		<comments>http://www.richardlord.net/blog/the-parentheses-operator#comments</comments>
		<pubDate>Wed, 16 Jul 2008 08:10:57 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Event handlers]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Learning]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/the-parentheses-operator/</guid>
		<description><![CDATA[<p>It's not uncommon for less experienced Actionscript developers, particularly self-taught developers, to be a little confused about the purpose of the parentheses you put after a function name when calling the function. The most common question is why aren't the parentheses used when assigning a function as an event handler?...</p>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not uncommon for less experienced Actionscript developers, particularly self-taught developers, to be a little confused about the purpose of the parentheses you put after a function name when calling the function. The most common question is why aren&#8217;t the parentheses used when assigning a function as an event handler? i.e. Why are there no parentheses after clickHandler in the first line of
this code?</p>

<pre class="code">addEventListener( MouseEvent.CLICK, clickHandler );

function clickHandler( event:MouseEvent ):void
{
	// function body here
}</pre>

<h3>What the documentation says</h3>

<p>The (usually excellent) Flash and Flex help isn&#8217;t much help here. In the Actionscript 3 language reference, it states that the parentheses &quot;Surrounds one or more parameters and passes them to the function that precedes the parentheses&quot;, which doesn&#8217;t explain why (or even if) the parentheses are necessary when no parameters are passed to the function.</p>

<p>Elsewhere in the help, it states that &quot;If you are calling a function with no parameters, you must use an empty pair of parentheses&quot;. No explanation &#8211; just do it. Which inevitably leads to developers wondering why they mustn&#8217;t do it when assigning event handlers.</p> 

<p>Colin Moock&#8217;s Essential Actionscript 3 (also usually excellent) takes a similar approach, simply stating &quot;Notice the important and mandatory use of the parentheses operator following the method name&quot;.</p>

<p>People learn things better if they understand them, so here&#8217;s my attempt at an explanation. If you&#8217;re confused about when to use the parentheses operator this should help. If not, come back tomorrow when I&#8217;ll post something a lot more advanced about programmatic skins in Flex.</p>

<h3>What the parentheses operator does</h3>

<p>A function is a portion of code that performs a specific task or set of tasks within your application. The function may be something you have defined, or it may be an intrinsic function (a function built-in to the flash player, like the trace function). When a function is a property of an object, it&#8217;s usually called a method. Here&#8217;s a trivial example function definition.</p>

<pre class="code">function sayHello():void
{
	someTextField.text = "Hello";
}</pre>

<p>The code inside the function is run by calling the function, like this.</p>

<pre class="code">sayHello();</pre>

<p>The parentheses could perhaps be better named as the function calling operator since that is what they do. We use the function&#8217;s name to refer to the function, and then we use the parentheses to run the function we have identified.</p>

<p>If the function needs some additional information in order to run (the parameters) then that information goes inside the parentheses.</p>

<pre class="code">function saySomething( thingToSay:String ):void
{
	someTextField.text = thingToSay;
}

saySomething( "This is a boring example" );</pre>

<p>But the parentheses, with or without parameters, are the operator that calls the function.</p>

<h3>What happens without the parentheses</h3>

<p>If that&#8217;s what the parentheses are for, what happens if we leave them out? Like this</p>

<pre class="code">sayHello;</pre>

<p>Well, first of all, you won&#8217;t get an error. sayHello exists (it&#8217;s defined above) so the code is valid. But, it doesn&#8217;t do anything. We have used the name of the function (&#8220;sayHello&#8221;) to refer to the function, then we&#8217;ve done nothing with it. When you put the parenthesis after the function you are indicating that you want to call the function now. Without the parenthesis, you&#8217;re not running the function. But calling a function is not the only thing we can do with it.</p>

<p>For example, we can assign it to a variable, like this</p>

<pre class="code">var myFunction:Function = sayHello;</pre>

<p>Now we can refer to the function via the variable. So if we used the parenthesis operator on the variable, it would call the function, like this</p>

<pre class="code">myFunction();</pre>

<p>Or we could continue to use it&#8217;s original name, like this</p>

<pre class="code">sayHello();</pre>

<h3>Event handlers</h3>

<p>When we set up an event handler, we don&#8217;t want to call the function straight away, we want the event handler to call the function later, when the event happens. So, we define an appropriate function</p>

<pre class="code">function clickHandler( event:MouseEvent ):void
{
	someTextField.text = "The mouse was clicked";
}</pre>

<p>Then we assign the function as a listener for the event</p>

<pre class="code">addEventListener( MouseEvent.CLICK, clickHandler );</pre>

<p>When we assign the function as a listener, we don&#8217;t want to call the function, so we don&#8217;t use the parentheses operator. Later, when the event actually happens, the flash player will call the function for us. There&#8217;s some code inside the EventDispatcher class that uses the parentheses operator to call the function when the event happens.</p>

<p>If you make the mistake of using the parentheses operator when assigning the event handler, like this</p>

<pre class="code">addEventListener( MouseEvent.CLICK, clickHandler() );</pre>

<p>then the event handler will be called immediately (you&#8217;ve told it to do so by using the parentheses). This will usually generate an error since you haven&#8217;t assigned a parameter for the event argument.</p>

<p>So that&#8217;s the parenthesis operator. Personally, I like the way that C++ names the parentheses operator the &quot;function operator&quot; &#8211; it&#8217;s the operator that calls a function.</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/the-parentheses-operator/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using uint as a loop iterator</title>
		<link>http://www.richardlord.net/blog/using-uint-as-a-loop-iterator</link>
		<comments>http://www.richardlord.net/blog/using-uint-as-a-loop-iterator#comments</comments>
		<pubDate>Wed, 21 Nov 2007 09:34:27 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Learning]]></category>

		<guid isPermaLink="false">http://www.bigroom.co.uk/blog/using-uint-as-a-loop-iterator/</guid>
		<description><![CDATA[<p>It took me some time to track down this bug in some code recently. I post it here to help others avoid the error. It's obvious once you see it...</p>]]></description>
			<content:encoded><![CDATA[<p>It took me some time to track down this bug in some code recently. I post it here to help others avoid the error. It&#8217;s obvious once you see it. When does this loop stop?</p>

<pre class="code">for( var i:uint = 5; i >= 0; i-- )
{
    // do something here
}</pre>

<p>The answer, of course, is never. The loop continues indefinitely because a uint can never be less than 0. Doh!</p>]]></content:encoded>
			<wfw:commentRss>http://www.richardlord.net/blog/using-uint-as-a-loop-iterator/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

