>>>
and the advantage of a const, which is the speed.
I'm not sure though, if the compiler really replaces
the value back.
>>>

Time to time I see that some developers prefer "const" instead of
"var" for local variables declared inside function implementation,
but as for me it would make real sense if AS3 compiler could
produce optimized code in this case.

Yesterday I tried to compile code listed below with Flex 2, hotfix 2,
played with "-optimized" switch in release build, but no success,
generated function ABC code for a1==a2 and f1==f2. The only
difference I saw is compiler error, preventing me assigning new value
to const. It's possible some kind of special "const" optimization
exists in current compiler, but I don't know anything about it.

<code_snip>
 public class A1
 {
  public function a1():int
  {
   var i1:int = 2;
   var i2:int = 2;
   var i3:int = i1+i2;
    return i3;
  }

  public function a2():int
 {
   const i1:int = 2;
   const i2:int = 2;
   const i3:int = i1+i2;
   return i3;
  }

  public function f1():void
  {
   var b1:B1 = new B1();
   b1.b1();
  }

  public function f2():void
  {
   const b1:B1 = new B1();
   b1.b1();
  }
 }

</code_snip>


For example in C++ it is normal that "a2" can be optimized
by compiler to:

    public function a2():int { return 4; }

or even as just "4" const:

    public const a2:int = 4;

it will depend on function code, compiler, settings etc.

But I don't know how many people programming in ActionScript
would use this feature and did it make sense at all. There are so
many code patterns commonly used in AS and other script environments
that can be optimized without any advanced features:

     for(var i:int = 0; i < menuBar.menus.length; ++i)
      if(menuBar.menus[i])
       ++menuCount;

Just my 2c...

P.S.: "premature optimization is the root of all evil".

--
Thanks,
Vadim.


----- Original Message ----- 
From: Ralf Bokelberg
To: flexcoders@yahoogroups.com
Sent: Thursday, December 20, 2007 6:18 PM
Subject: Re: [flexcoders] Re: const?


Ideally a const inside a function allows you to
use an identifier for a constant value, which is
replaced back to the value by the compiler. So
you have the advantage of using an identifier,
eg. changes are concentrated in one place,
and the advantage of a const, which is the speed.

I'm not sure though, if the compiler really replaces
the value back.

Cheers

On Dec 20, 2007 4:55 PM, Paul Andrews <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
>
> ----- Original Message -----
> From: "reflexactions" <[EMAIL PROTECTED]>
> To: <flexcoders@yahoogroups.com>
> Sent: Thursday, December 20, 2007 3:35 PM
> Subject: [flexcoders] Re: const?
>
> > So your telling me that when you write a function if you have a var
> > that wont change its value for the duration of that function say like:
> > override protected function updateDisplayList(...):void{
> > var count:int=numChildren;
> > ..loop code..
> > ...finished
> > }
>
> I only use constants when the value at declaration time is fixed. I
> wouldn't
> consider your example to be a constant because numChildren can potentially
> change, whether it does or not. It would be misleading to the developer to
> try and describe a value as const when it was based upon a changing 
> value -
> definitely bad practice.
>
>
> > you consider that bad code as count should be declared as a const
>
> No, though the variable may be pointless if numChildren doesn't change and
> it may be poorly named if numChildren does.
>
> Paul
>
> snip
>
>

-- 
Ralf Bokelberg <[EMAIL PROTECTED]>
Flex & Flash Consultant based in Cologne/Germany

 

Reply via email to