<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: How would you write this ActionScript code?</title>
	<atom:link href="http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-would-you-write-this-actionscript-code</link>
	<description>Heroku &#124; Java &#124; Scala &#124; Cloud &#124; Open Source &#124; Linux</description>
	<lastBuildDate>Wed, 08 Feb 2012 21:29:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: Matt Cannizzaro</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157551</link>
		<dc:creator>Matt Cannizzaro</dc:creator>
		<pubDate>Mon, 06 Jul 2009 20:13:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157551</guid>
		<description>James,
If you had a constant for each state name and made sure to always use the constant, never a string literal, then you would avoid typos that would cause runtime errors. Using constants for state names is not really a common idiom in Actionscript though - if you look at the source of the Spark classes in the Flex 4 SDK, for example, you&#039;ll see they use literal strings for state names, not constants.

Ariel, Rob,
Neither booleans nor switch statements are suitable in this context, though they may appear more appropriate than the actual generated code at first glance. Please see my above post for the details. Remember that we are trying to generate code that works in the general case, not just this particularly simple case.

While the variable could (and perhaps, *should*) be a const, I think the primary justification would be readability, not performance. Making it clear that we don&#039;t intend to change the value of that variable might help make this code clearer, which is of course a good thing. But this code is not executed often, so any performance benefit derived from using a const would be negligible.</description>
		<content:encoded><![CDATA[<p>James,<br />
If you had a constant for each state name and made sure to always use the constant, never a string literal, then you would avoid typos that would cause runtime errors. Using constants for state names is not really a common idiom in Actionscript though &#8211; if you look at the source of the Spark classes in the Flex 4 SDK, for example, you&#8217;ll see they use literal strings for state names, not constants.</p>
<p>Ariel, Rob,<br />
Neither booleans nor switch statements are suitable in this context, though they may appear more appropriate than the actual generated code at first glance. Please see my above post for the details. Remember that we are trying to generate code that works in the general case, not just this particularly simple case.</p>
<p>While the variable could (and perhaps, *should*) be a const, I think the primary justification would be readability, not performance. Making it clear that we don&#8217;t intend to change the value of that variable might help make this code clearer, which is of course a good thing. But this code is not executed often, so any performance benefit derived from using a const would be negligible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157535</link>
		<dc:creator>Rob</dc:creator>
		<pubDate>Mon, 29 Jun 2009 16:54:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157535</guid>
		<description>Agree with yeah and Matt Cannizzaro - if you just need a toggle, use a boolean; and if you need more conditions use a switch. Either way you should be able to avoid using the extra variable (which should be a const for performance) - definitely easier on memory and the GC.</description>
		<content:encoded><![CDATA[<p>Agree with yeah and Matt Cannizzaro &#8211; if you just need a toggle, use a boolean; and if you need more conditions use a switch. Either way you should be able to avoid using the extra variable (which should be a const for performance) &#8211; definitely easier on memory and the GC.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ariel sommeria</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157534</link>
		<dc:creator>ariel sommeria</dc:creator>
		<pubDate>Mon, 29 Jun 2009 13:42:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157534</guid>
		<description>Hi James,
I go with yeah.
Use a boolean if it&#039;s enough, which seems to be the case.
Ariel</description>
		<content:encoded><![CDATA[<p>Hi James,<br />
I go with yeah.<br />
Use a boolean if it&#8217;s enough, which seems to be the case.<br />
Ariel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Ward</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157530</link>
		<dc:creator>James Ward</dc:creator>
		<pubDate>Sat, 27 Jun 2009 00:53:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157530</guid>
		<description>Hi Matt,

Don&#039;t you get compile errors when you reference an non-existant state in the new state syntax in an MXML tag?  The const doesn&#039;t help avoid state name typos in MXML but should help to keep typos in ActionScript from causing problems that are not picked up by the compiler.

-James</description>
		<content:encoded><![CDATA[<p>Hi Matt,</p>
<p>Don&#8217;t you get compile errors when you reference an non-existant state in the new state syntax in an MXML tag?  The const doesn&#8217;t help avoid state name typos in MXML but should help to keep typos in ActionScript from causing problems that are not picked up by the compiler.</p>
<p>-James</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TimHoff</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157528</link>
		<dc:creator>TimHoff</dc:creator>
		<pubDate>Fri, 26 Jun 2009 21:43:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157528</guid>
		<description>I suppose this would be cleaner for that particular example:

private var quarterDesriptions:Array = [ &quot;&quot;, &quot;1st Quarter&quot;, &quot;2nd Quarter&quot;, &quot;3rd Quarter&quot;, &quot;4th Quarter&quot; ];

quarterDesription = quarterDesriptions[quarter];

But, when comparing ternary, if/else or switch, I&#039;m wondering how you would set this it up like Python.

-TH</description>
		<content:encoded><![CDATA[<p>I suppose this would be cleaner for that particular example:</p>
<p>private var quarterDesriptions:Array = [ "", "1st Quarter", "2nd Quarter", "3rd Quarter", "4th Quarter" ];</p>
<p>quarterDesription = quarterDesriptions[quarter];</p>
<p>But, when comparing ternary, if/else or switch, I&#8217;m wondering how you would set this it up like Python.</p>
<p>-TH</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TimHoff</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157526</link>
		<dc:creator>TimHoff</dc:creator>
		<pubDate>Fri, 26 Jun 2009 16:41:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157526</guid>
		<description>@Adam
I like the Python-isk ideas.  However, when I tried to implement it in Flex, I ran into a brick wall.  Is there a way to make the following function cleaner in AS?

private function getQuarterDescription( quarter:int ):String 
{
    switch(quarter)
    {
        case 1:
            return &quot;1st&quot;;
        case 2:
            return &quot;2nd&quot;;
        case 3:
            return &quot;3rd&quot;;
        case 4:
            return &quot;4th&quot;;
    }

    return &quot;&quot;;
}</description>
		<content:encoded><![CDATA[<p>@Adam<br />
I like the Python-isk ideas.  However, when I tried to implement it in Flex, I ran into a brick wall.  Is there a way to make the following function cleaner in AS?</p>
<p>private function getQuarterDescription( quarter:int ):String<br />
{<br />
    switch(quarter)<br />
    {<br />
        case 1:<br />
            return &#8220;1st&#8221;;<br />
        case 2:<br />
            return &#8220;2nd&#8221;;<br />
        case 3:<br />
            return &#8220;3rd&#8221;;<br />
        case 4:<br />
            return &#8220;4th&#8221;;<br />
    }</p>
<p>    return &#8220;&#8221;;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Cannizzaro</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157525</link>
		<dc:creator>Matt Cannizzaro</dc:creator>
		<pubDate>Fri, 26 Jun 2009 16:16:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157525</guid>
		<description>Hi James,

It would be nice if we could use a single constant for a string name. Typically this is done so that should you ever want to change the value of the string, you need only change it in one place. We would miss out on this benefit because AFAIK you cannot use anything but a literal string in a State tag, and state specific properties are always of the form stateName.propertyName. So even if we were to use constants in the generated Actionscript code, if you wanted to rename a state, you&#039;d still have do something like a search and replace.

- Matt</description>
		<content:encoded><![CDATA[<p>Hi James,</p>
<p>It would be nice if we could use a single constant for a string name. Typically this is done so that should you ever want to change the value of the string, you need only change it in one place. We would miss out on this benefit because AFAIK you cannot use anything but a literal string in a State tag, and state specific properties are always of the form stateName.propertyName. So even if we were to use constants in the generated Actionscript code, if you wanted to rename a state, you&#8217;d still have do something like a search and replace.</p>
<p>- Matt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Ward</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157523</link>
		<dc:creator>James Ward</dc:creator>
		<pubDate>Thu, 25 Jun 2009 22:12:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157523</guid>
		<description>Thanks Matt for the explanation.  With those constraints it seems that the way you&#039;ve chosen is the best way.  It been interesting for me to learn why different people choose different ways depending on their own constraints.

Perhaps the only thing that Catalyst should change is to use consts for the state names instead of strings.  Maybe you could even bind to those consts in the State tags.  I remember hearing at one point that there might be some compiler optimizations done when binding to consts.  If so it would be nice to not have state names as strings everywhere.

-James</description>
		<content:encoded><![CDATA[<p>Thanks Matt for the explanation.  With those constraints it seems that the way you&#8217;ve chosen is the best way.  It been interesting for me to learn why different people choose different ways depending on their own constraints.</p>
<p>Perhaps the only thing that Catalyst should change is to use consts for the state names instead of strings.  Maybe you could even bind to those consts in the State tags.  I remember hearing at one point that there might be some compiler optimizations done when binding to consts.  If so it would be nice to not have state names as strings everywhere.</p>
<p>-James</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Cannizzaro</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157521</link>
		<dc:creator>Matt Cannizzaro</dc:creator>
		<pubDate>Wed, 24 Jun 2009 21:25:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157521</guid>
		<description>Hi James,

I&#039;m the developer working on interactions in Flash Catalyst. Most programmers, myself included, would probably write something similar to the following to toggle state between &quot;open&quot; and &quot;closed&quot;:

currentState = ( currentState == “closed” ) ? “open” : “closed”;

This code is simple, straightforward, and readable. Let me explain why our generated code ends up being more complex:

(For those of you who have not used Flash Catalyst, a brief explanation: an interaction is just a snippet of Actionscript code that either sets the state of a component or plays an effect. It can have a condition attached that causes it to only execute when a component is in a given state).

Interactions in FC are not mutually exclusive - if I have several interactions that fire when a user clicks a button, any number of them could end up executing. As a result, we can&#039;t just use an if/else block, or our interactions would end up being mutually exclusive.

When only a single component and a single property (currentState in this example) are involved, an if/else block would indeed be the most appropriate structure, but that is a special case. For example, consider the following interactions:

On click, play transition to state1, when in state2.
On click, play transition to fooState in someComponent, when someComponent is in barState.

There are two components involved here: the component you&#039;re editing, and someComponent, a child component. We might be tempted to write these interactions like so:

if (currentState == &quot;state2&quot;) {
  currentState = &quot;state1&quot;;
} else if (someComponent.currentState == &quot;barState&quot;) {
  someComponent.currentState = &quot;fooState&quot;;
}

But if we wrote that, we&#039;d have a bug: when our component is in state2, we never execute the second interaction, regardless of the state of someComponent. The correct code, for this case, looks like:

if (currentState == &quot;state2&quot;) {
  currentState = &quot;state1&quot;;
} 
if (someComponent.currentState == &quot;barState&quot;) {
  someComponent.currentState = &quot;fooState&quot;;
}

In the general case, we have to use independent if blocks. This makes sense because interactions in FC are fundamentally independent from one another and the code should reflect this.

Now let&#039;s consider your example, where we want to toggle between open and closed states. Knowing that we should use separate if blocks, we might come up with:

if (currentState == &quot;open&quot;) {
  currentState = &quot;closed&quot;;
}
if (currentState == &quot;closed&quot;) {
  currentState = &quot;open&quot;;
}

Again, we have a bug: if currentState is &quot;open&quot; before this code runs, it will still be &quot;open&quot; after it runs, because both conditions evaluate to true. The problem here is that we&#039;ve written code that assumes the value of the currentState property won&#039;t change until it has finished executing, which is of course a flawed assumption.

Considering that we&#039;ve already determined that if/else doesn&#039;t work in some cases, the general solution is to do what FC does: store the value of currentState before we&#039;ve changed anything and test against this saved value. When an FC user writes interactions, they are probably not considering that the value of their condition might change part way through the handling of a single event. The generated code is probably not what a programmer would write, but it most accurately reflects the intentions of the FC user.

Certainly in this example, this code looks silly. In the general case, however, using the extra variable is necessary. I decided that it was best to keep our code generation consistent, even if that means making the simple cases look slightly more complex than they actually are.

- Matt</description>
		<content:encoded><![CDATA[<p>Hi James,</p>
<p>I&#8217;m the developer working on interactions in Flash Catalyst. Most programmers, myself included, would probably write something similar to the following to toggle state between &#8220;open&#8221; and &#8220;closed&#8221;:</p>
<p>currentState = ( currentState == “closed” ) ? “open” : “closed”;</p>
<p>This code is simple, straightforward, and readable. Let me explain why our generated code ends up being more complex:</p>
<p>(For those of you who have not used Flash Catalyst, a brief explanation: an interaction is just a snippet of Actionscript code that either sets the state of a component or plays an effect. It can have a condition attached that causes it to only execute when a component is in a given state).</p>
<p>Interactions in FC are not mutually exclusive &#8211; if I have several interactions that fire when a user clicks a button, any number of them could end up executing. As a result, we can&#8217;t just use an if/else block, or our interactions would end up being mutually exclusive.</p>
<p>When only a single component and a single property (currentState in this example) are involved, an if/else block would indeed be the most appropriate structure, but that is a special case. For example, consider the following interactions:</p>
<p>On click, play transition to state1, when in state2.<br />
On click, play transition to fooState in someComponent, when someComponent is in barState.</p>
<p>There are two components involved here: the component you&#8217;re editing, and someComponent, a child component. We might be tempted to write these interactions like so:</p>
<p>if (currentState == &#8220;state2&#8243;) {<br />
  currentState = &#8220;state1&#8243;;<br />
} else if (someComponent.currentState == &#8220;barState&#8221;) {<br />
  someComponent.currentState = &#8220;fooState&#8221;;<br />
}</p>
<p>But if we wrote that, we&#8217;d have a bug: when our component is in state2, we never execute the second interaction, regardless of the state of someComponent. The correct code, for this case, looks like:</p>
<p>if (currentState == &#8220;state2&#8243;) {<br />
  currentState = &#8220;state1&#8243;;<br />
}<br />
if (someComponent.currentState == &#8220;barState&#8221;) {<br />
  someComponent.currentState = &#8220;fooState&#8221;;<br />
}</p>
<p>In the general case, we have to use independent if blocks. This makes sense because interactions in FC are fundamentally independent from one another and the code should reflect this.</p>
<p>Now let&#8217;s consider your example, where we want to toggle between open and closed states. Knowing that we should use separate if blocks, we might come up with:</p>
<p>if (currentState == &#8220;open&#8221;) {<br />
  currentState = &#8220;closed&#8221;;<br />
}<br />
if (currentState == &#8220;closed&#8221;) {<br />
  currentState = &#8220;open&#8221;;<br />
}</p>
<p>Again, we have a bug: if currentState is &#8220;open&#8221; before this code runs, it will still be &#8220;open&#8221; after it runs, because both conditions evaluate to true. The problem here is that we&#8217;ve written code that assumes the value of the currentState property won&#8217;t change until it has finished executing, which is of course a flawed assumption.</p>
<p>Considering that we&#8217;ve already determined that if/else doesn&#8217;t work in some cases, the general solution is to do what FC does: store the value of currentState before we&#8217;ve changed anything and test against this saved value. When an FC user writes interactions, they are probably not considering that the value of their condition might change part way through the handling of a single event. The generated code is probably not what a programmer would write, but it most accurately reflects the intentions of the FC user.</p>
<p>Certainly in this example, this code looks silly. In the general case, however, using the extra variable is necessary. I decided that it was best to keep our code generation consistent, even if that means making the simple cases look slightly more complex than they actually are.</p>
<p>- Matt</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Larsen</title>
		<link>http://www.jamesward.com/2009/06/23/how-would-you-write-this-actionscript-code/comment-page-1/#comment-157519</link>
		<dc:creator>Daniel Larsen</dc:creator>
		<pubDate>Wed, 24 Jun 2009 17:31:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.jamesward.com/blog/?p=1018#comment-157519</guid>
		<description>oops, I meant Adam.  :-P

(not to discount David&#039;s ideas...but those are the same ones that I had...Adams idea was totally new to me)</description>
		<content:encoded><![CDATA[<p>oops, I meant Adam.  :-P</p>
<p>(not to discount David&#8217;s ideas&#8230;but those are the same ones that I had&#8230;Adams idea was totally new to me)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

