Should as3 constructor be lightweight?

When I work in flashdevelop I usually use the code quality tool (PMD) provided by Adobe, to check on the welfare of my code. It can be found under Tools>Flash Tools>Analyze Project Source Code. Generally I try to stick on its warnings and keep my code as clean as I can by its proposals, although sometimes I ignore it on purpose. But there is a warning that always caught my sight.

Constructor must be as lightweight as possible. No control statement allowed, whereas a cyclomatic complexe of 2 has been detected. The Just-In-Time compiler does not compile constructors. Make them as lightweight as possible, or move the complexity of the code to a method called by the constructor. Then the complexity will be compiled by the JIT

I always wondered how much difference can that make and if I should follow it or not. So today I put that on a test.Here is my code.

package
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.utils.getTimer;

	/**
	 * ...
	 * @author Konstantinos Egkarchos
	 */
	public class Main extends Sprite
	{
		private var console:TrConsole;

		public function Main():void
		{
			init();
		}

		private function init():void
		{
			trace("Starting at " + getTimer());
			for(var i:uint = 0; i < 100;i++)
            {
                var bitmap:BitmapData = new BitmapData(400, 400, true, 0);

                for(var x:uint = 0; x < bitmap.width; x++)
                {
                    for(var y:uint = 0; y < bitmap.height; y++)
                    {
                        bitmap.setPixel32(x, y, 0x96587df2);
                    }

                }

            }

			trace("After bitmap creation: " + getTimer());

			for (var j:uint = 0; j < 10000; j++)
			{
				for (var k:int = 0; k < 10000; k++)
				{
					var total:uint = j * k;
				}
			}

			trace("After multiplication: " + getTimer());
		}

	}

}

You can cut the code from the function and paste it on the constructor to test both ways. Personally I haven’t noticed much difference. My results on code with constructor was 18 | 2457 | 2934 whereas code in init() function gave 17 |2446 | 2932.

So, personally I think that it’s a good idea to keep complex code outside of the constructor but it doesn’t hurt if you let it in. At least not in performance.

Advertisements

Posted on 21 June, 2012, in Actionscript 3, Programming, Uncategorized and tagged , , . Bookmark the permalink. 2 Comments.

  1. It stacks up when you instantiate lots of things at once and things can really choke up if you don’t move init code outside of the constructor with 1000’s of objects.

  2. Θα συμφωνήσω με το quality tool. Στον constructor καλώ συναρτήσεις αρχικοποίησης και τίποτα παραπάνω. Νομίζω η λογική αυτή βοηθάει πιο πολύ στο να διαβάσεις τον κώδικα και να τον κάνω πιο εύκολα κατανοητό σε έναν τρίτο

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: