# \[AS2 OOP\] Best practice? Local vars vs. properties

**URL:** <https://forum.kirupa.com/t/as2-oop-best-practice-local-vars-vs-properties/182043>\
**Category:** flash\
**Created:** [March 15, 2006, 2:22am UTC](https://forum.kirupa.com/t/as2-oop-best-practice-local-vars-vs-properties/182043 "2006-03-15T02:22:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Ford](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matthew\_Ford](https://forum.kirupa.com/u/Matthew_Ford)\
**Post date:** [March 15, 2006, 2:22am UTC](https://forum.kirupa.com/t/as2-oop-best-practice-local-vars-vs-properties/182043/1 "2006-03-15T02:22:08Z")

</div>

[FONT=Arial]Hello to everyone. I love Kirupa![/FONT]

[FONT=Arial]I am wrapping my brain around OOP in AS2 and I need some advice. I am making two posts on two different subjects.[/FONT]

[FONT=Arial]My question here is: when should I use local instance variables, and when should I attach new properties to the instance object? (Feel free to correct my terminology.)[/FONT]

[FONT=Arial]Let’s say I have a class for a scrolling background, which scrolls when I mouse over its edge. I want to put these values somewhere:[/FONT]

[FONT=Arial]1) How wide is the border in which a mouseOver makes it scroll? This is basically a semi-constant—I set it up once and keep it the same, unless the user changes it in an options menu to make the border wider or narrower. Call this “scrollBorderWidth”.[/FONT]

[FONT=Arial]2) How fast is the background scrolling now? I want to track this so I can smoothly change the scroll speed over several frames. This is often changed. Call this “scrollSpeed”.[/FONT]

[FONT=Arial]It seems I have two ways I can store and access these values and darned if I know which is better practice.[/FONT]

[FONT=Arial]\*\*\*METHOD A: [/FONT]  
[FONT=Arial]Make them local variables in the class, so the scrolling background instance has them as local vars. I set them up thusly:[/FONT]

[FONT=Arial]class ScrollingBkgnd {[/FONT]  
[FONT=Arial]var scrollBorderWidth:Number = 50;[/FONT]  
[FONT=Arial]var scrollSpeed:Number = 0;[/FONT]

[FONT=Arial]//constructor etc[/FONT]

[FONT=Arial]function foobar(Void) {[/FONT]  
[FONT=Arial]if(\_xmouse \< scrollBorderWidth) doSomething;[/FONT]  
[FONT=Arial]scrollSpeed++;[/FONT]  
[FONT=Arial]}[/FONT]  
[FONT=Arial]}[/FONT]

[FONT=Arial]\*\*\*METHOD B:[/FONT]  
[FONT=Arial]Make them properties of the instance (and the class dynamic), so the scrolling background instance object has them as new object properties. I set them up thusly:[/FONT]

[FONT=Arial]dynamic class ScrollingBkgnd {[/FONT]

[FONT=Arial]//constructor etc[/FONT]

[FONT=Arial]function init(Void) {[/FONT]  
[FONT=Arial]this.scrollBorderWidth = 50;[/FONT]  
[FONT=Arial]this.scrollSpeed = 0;[/FONT]  
[FONT=Arial]}[/FONT]

[FONT=Arial]function foobar(Void) {[/FONT]  
[FONT=Arial]if(\_xmouse \< this.scrollBorderWidth) doSomething;[/FONT]  
[FONT=Arial]this.scrollSpeed++;[/FONT]  
[FONT=Arial]}[/FONT]  
[FONT=Arial]}[/FONT]

[FONT=Arial]\*\*\*QUESTION:[/FONT]  
[FONT=Arial]Which method should I use, A or B? My main priorities are, in order:[/FONT]  
[FONT=Arial]1) Make code that can scale up to be part of a rather complex application.[/FONT]  
[FONT=Arial]2) Make code which will be easily reusable in other projects.[/FONT]  
[FONT=Arial]3) Make code which is comprehensible to other (real) coders (me being a fake one).[/FONT]

[FONT=Arial]???[/FONT]

[FONT=Arial]Thanks very much for any advice you can give to someone who wants to do good OOP but is lacking much formal training.[/FONT]
