Question

I've started using const a lot more in my AS3 code. My reasoning being that if a value does not change then why make it a var.

I heard through the twitter vine that const values are optimized differently than vars. Due to twitter being short and sweet, this is where the details stopped.

Does anyone have more info on the pro/cons of using var and const.

Let me start off by stating a con: in Flash Builder const values are not visible in the debugger. It seems values that do not change are not worth viewing in a debugger.

Was it helpful?

Solution

According to Jackson Dunstan, there's no real difference performance wise when using const instead of var.

There's a pretty big semantical distinction however, which you point out yourself. A const, since it can't be changed, will make the compiler catch any issues where the value is changed in error.

OTHER TIPS

The only con I know for constants is: you can't change the value. That's it. ;-) Constants are constant and variables are variable. If a value is constant then it should be declared as constant and vice versa. That's a BIG hint for the compiler and the developer.

Flash Builder shows the value for constants if you move the mouse pointer over the constant or you can add a constant as expression to the "Expressions" view. Of course they are not listed in the "Variables" view.

Fore more informations read the AS3 Docs: Constants

Regarding your con, if you're using Flash Builder, constants are visible in the Variables view if you go to the menu on the view, select the Flex submenu, and chose Show Constants.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top